fakeyou 0.1.1

Library to use FakeYou's AI TTS services
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! An easy, synchronous API to access FakeYou's AI TTS services
//!
//! **This is an unofficial API with no connection to storyteller.ai**<br/>
//! **An account with <a href="http://www.fakeyou.com">FakeYou</a> is required to use this API.**
//!
//! It has not been tested on lower tiers, and is still missing features.<br/>
//! At the moment, it is the minimum necessary to query voices, categories, and generate audio using them.
//!
//!
//! # Examples
//! Using only two lines of code, it is possible to generate usable audio.<br/>
//! In these examples, we are using a model token which is already known to us.<br/>
//! **These will take some time to finish, due to the API's queue**
//! ```
//! use fakeyou;
//!
//! fn main() {
//!     let fake_you = fakeyou::authenticate("user_name", "password").unwrap();
//!     fake_you.generate_file_from_token("Hello!", "TM:mc2kebvfwr1p", "hello.wav").unwrap();
//! }
//!
//! ```
//!
//! You may also stream the resulting audio directly to an audio playback library, such as `rodio`:
//! ```
//! use std::io::Cursor;
//! use rodio::{Decoder, OutputStream, source::Source, Sink};
//! use fakeyou;
//!
//! fn main() {
//!     // rodio setup
//!     let (_stream, stream_handle) = OutputStream::try_default().unwrap();
//!     let sink = Sink::try_new(&stream_handle).unwrap();
//!
//!     // actual API use
//!     let fake_you = fakeyou::authenticate("user_name", "password").unwrap();
//!     let bytes = fake_you.generate_bytes_from_token("Hello!", "TM:mc2kebvfwr1p").unwrap();
//!
//!     // play resulting audio
//!     let cursor = Cursor::new(bytes);
//!     let decoder = Decoder::new(cursor).unwrap();
//!     sink.append(decoder);
//!     sink.sleep_until_end();
//! }
//! ```
//! # Session token:
//! Once authenticated, your session token is valid for 20 years, so re-authenticating is practically
//! unnecessary, however, re-caching may be necessary using [FakeYouClient::invalidate_cache] to
//! keep the list of [Category] and [Voice] up to date.

use std::time::{Duration};
use std::fs;

use bytes::Bytes;
use chrono::{DateTime, Utc};
use reqwest;
use thiserror::Error;
use serde::{Serialize, Deserialize};
use serde_json;

use uuid::Uuid;


trait SerdeString {
    fn to_string_handled(&self) -> Result<String, Error>;
}

impl SerdeString for serde_json::Value {
    fn to_string_handled(&self) -> Result<String, Error> {
        let string = self.as_str();
        let string = match string {
            Some(string) => { string.to_string() },
            None => { return Err(Error::ImproperResponse) }
        };
        Ok(string)
    }
}

#[derive(Error, Debug)]
pub enum Error {
    #[error("Invalid credentials supplied")]
    InvalidCredentials,
    #[error("Undefined HTTP response")]
    UndefinedResponse,
    #[error("Denied due to too many requests")]
    TooManyRequests,
    #[error("Improper response structure")]
    ImproperResponse,
    #[error("Job failed")]
    JobFailed,
    #[error("File read/write error")]
    IOError,
    #[error("Error making request.")]
    ReqwestError(reqwest::Error),
    #[error("Error serializing JSON")]
    SerializationError(serde_json::Error)
}

#[derive(Debug, Deserialize, Serialize)]
struct TtsJobRequest {
    uuid_idempotency_token: String,
    tts_model_token: String,
    inference_text: String
}

#[derive(Debug, Deserialize, Serialize)]
struct TtsJobResponse {
    success: bool,
    inference_job_token: String,
    inference_job_token_type: String
}


/// Representation of a single voice
#[derive(Clone)]
pub struct Voice {
    pub title: String,
    pub model_token: String,
    pub category_tokens: Vec<String>
}

/// Representation of a category of voices
#[derive(Clone)]
pub struct Category {
    pub title: String,
    pub category_token: String,
    pub model_type: String
}

struct TtsJobResult {
    audio_path: String
}

/// Requests a FakeYouClient using a valid username and password
pub fn authenticate(user_name: &str, password: &str) -> Result<FakeYouClient, Error> {

    let client = reqwest::blocking::Client::builder()
        .cookie_store(true)
        .build();
    let client = match client {
        Ok(resp) => resp,
        Err(e) => return Err(Error::ReqwestError(e)),
    };

    // format! does NOT like curly brackets -- i can probably use json! but whatever
    let body = "{\"username_or_email\": \"".to_owned() + user_name + "\", \"password\": \"" + password + "\"}";

    let response = client
        .post("https://api.fakeyou.com/login")
        .header("Content-Type", "application/json")
        .body(body)
        .send();
    let response = match response {
        Ok(resp) => resp,
        Err(e) => return Err(Error::ReqwestError(e)),
    };

    match response.status().as_u16() {
        200 => {
            let json = match response.text() {
                Ok(j) => j,
                Err(e) => return Err(Error::ReqwestError(e)),
            };
            if json != "{\"success\":true}" {
                return Err(Error::InvalidCredentials);
            }
        },
        401 => { return Err(Error::InvalidCredentials); },
        429 => { return Err(Error::TooManyRequests); },
        _ => { return Err(Error::UndefinedResponse); }
    }

    Ok(FakeYouClient::new(client))
}

fn serialize<T:Serialize>(obj: T) -> Result<serde_json::Value, Error>  {
    let val = match serde_json::to_value(&obj) {
        Ok(v) => v,
        Err(e) => return Err(Error::SerializationError(e)),
    };
    Ok(val)
}

/// An authenticated FakeYou client capable of making requests and directly downloading voice samples.
pub struct FakeYouClient {
    client: reqwest::blocking::Client,
    category_cache: Vec<Category>,
    voice_cache: Vec<Voice>,
    cache_generated: DateTime<Utc>
}

impl FakeYouClient {
    fn new(client: reqwest::blocking::Client) -> FakeYouClient {
        let mut client = FakeYouClient{
            client,
            category_cache: Vec::new(),
            voice_cache: Vec::new(),
            cache_generated: Utc::now()
        };
        client.invalidate_cache().expect("failed to build cache");
        client
    }

    /// Create a request and save the resulting .wav to `filename`
    pub fn generate_file(&self, text: &str, voice: &Voice, filename: &str) -> Result<(), Error> {
        self.generate_file_from_token(text, &voice.model_token, filename)
    }

    /// Create a request and return the .wav data as [Bytes]
    pub fn generate_bytes(&self, text: &str, voice: &Voice) -> Result<Bytes, Error> {
        self.generate_bytes_from_token(text, &voice.model_token)
    }

    /// Return a copy of all cached voices
    pub fn list_voices(&self) -> Vec<Voice> {
        self.voice_cache.to_vec()
    }

    /// Return a copy of all cached categories
    pub fn list_categories(&self) -> Vec<Category> {
        self.category_cache.to_vec()
    }

    /// Return a copy of all [Voice] which correspond to a given [Category]
    pub fn list_voices_by_category(&self, category: &Category) -> Vec<Voice> {
        self.list_voices_by_category_token(&category.category_token)
    }

    /// Return a copy of all [Voice] which correspond to a given category token
    pub fn list_voices_by_category_token(&self, category_token: &str) -> Vec<Voice> {
        self.voice_cache.iter()
            .filter(|voice| voice.category_tokens.contains(&category_token.to_string()))
            .cloned()
            .collect()
    }

    /// Create a request and return the .wav data as [Bytes] using a known model token
    pub fn generate_bytes_from_token(&self, text: &str, tts_model_token: &str) -> Result<Bytes, Error> {
        let job = TtsJobRequest{
            uuid_idempotency_token: Uuid::new_v4().to_string(),
            tts_model_token: tts_model_token.to_string(),
            inference_text: text.to_string()
        };
        let job = self.make_tts_job(job)?;
        let job = self.tts_poll(job)?;
        let data = self.get_bytes(&job.audio_path)?;
        Ok(data)
    }

    /// Create a request and save the resulting .wav to `filename` using a known model token
    pub fn generate_file_from_token(&self, text: &str, tts_model_token: &str, filename: &str) -> Result<(), Error> {
        let data = self.generate_bytes_from_token(text, tts_model_token)?;
        let result = fs::write(filename, data);
        let _result = match result {
            Ok(_) => { Ok(()) }
            Err(_) => { Err(Error::IOError) }
        };
        Ok(())
    }

    /// Refresh cache of Voices and Categories
    pub fn invalidate_cache(&mut self) -> Result<(), Error>  {
        let categories_json = self.get_categories()?;
        let mut categories: Vec<Category> = Vec::new();
        let mut voices: Vec<Voice> = Vec::new();

        for object in categories_json["categories"].as_array().unwrap() {
            let category = Category{
                title: object["name"].to_string_handled()?,
                category_token: object["category_token"].to_string_handled()?,
                model_type: object["model_type"].to_string_handled()?
            };
            categories.push(category);
        }
        let voices_json = self.get_voices()?;
        for object in voices_json["models"].as_array().unwrap() {
            // print!("{:#?}", object);
            let mut category_tokens: Vec<String> = Vec::new();
            for category in object["category_tokens"].as_array().unwrap() {
                let category = category.as_str().ok_or(Error::ImproperResponse)?;
                category_tokens.push(category.to_string());
            }
            let voice = Voice{
                title: object["title"].to_string_handled()?,
                model_token: object["model_token"].to_string_handled()?,
                category_tokens
            };
            voices.push(voice);
        }
        self.category_cache = categories;
        self.voice_cache = voices;
        self.cache_generated = Utc::now();
        Ok(())
    }

    fn get_categories(&self) -> Result<serde_json::Value, Error> {
        let resp = self.get_json("https://api.fakeyou.com/category/list/tts")?;
        Ok(resp)
    }

    fn get_voices(&self) -> Result<serde_json::Value, Error> {
        let resp = self.get_json("https://api.fakeyou.com/tts/list")?;
        Ok(resp)
    }

    fn tts_poll(&self, job: TtsJobResponse) -> Result<TtsJobResult, Error> {
        let mut url = "https://api.fakeyou.com/tts/job/".to_string();
        url.push_str(&job.inference_job_token.to_string());
        let data: TtsJobResult;
        loop {
            let resp = self.get_json(&url)?;
            match &resp["state"]["status"] {
                serde_json::Value::String(status) => {
                    match status.as_str() {
                        "started" => {}
                        "pending" => {}
                        "attempt_failed" => {return Err(Error::JobFailed)}
                        "dead" => {return Err(Error::JobFailed)}
                        "complete_success" => {
                            let path = resp["state"]["maybe_public_bucket_wav_audio_path"].to_string_handled()?;
                            data = TtsJobResult{
                                audio_path: "https://storage.googleapis.com/vocodes-public".to_string()
                                    + &path
                            };
                            break;
                        }
                        &_ => {return Err(Error::ImproperResponse)}
                    }
                }
                serde_json::Value::Null => {return Err(Error::ImproperResponse)}
                serde_json::Value::Bool(_) => {return Err(Error::ImproperResponse)}
                serde_json::Value::Number(_) => {return Err(Error::ImproperResponse)}
                serde_json::Value::Array(_) => {return Err(Error::ImproperResponse)}
                serde_json::Value::Object(_) => {return Err(Error::ImproperResponse)}
            }
            std::thread::sleep(Duration::from_secs(2)); // Ensure we do not make too many requests
        }
        Ok(data)
    }

    fn make_tts_job(&self, job_request: TtsJobRequest) -> Result<TtsJobResponse, Error> {
        let response = self.post("https://api.fakeyou.com/tts/inference", job_request)?;
        let json = match response.text() {
            Ok(json) => json,
            Err(e) => return Err(Error::ReqwestError(e))
        };
        let response = serde_json::from_str(&json);
        let response: TtsJobResponse = match response {
            Ok(response) => response,
            Err(e) => return Err(Error::SerializationError(e))
        };
        Ok(response)
    }

    fn get_json(&self, url: &str) -> Result<serde_json::Value, Error> {
        let response = self.get(url)?;
        let json = match response.text() {
            Ok(json) => json,
            Err(e) => return Err(Error::ReqwestError(e)),
        };
        let json = serde_json::from_str(&*json);
        let json = match json {
            Ok(json) => json,
            Err(e) => return Err(Error::SerializationError(e)),
        };
        Ok(json)
    }

    fn get_bytes(&self, url: &str) -> Result<Bytes, Error> {
        let response = self.get(url)?;
        let data = response.bytes();
        let data = match data {
            Ok(data) => data,
            Err(e) => return Err(Error::ReqwestError(e)),
        };
        Ok(data)
    }

    fn get(&self, url: &str) -> Result<reqwest::blocking::Response, Error> {
        let response = self.client
            .get(url)
            .send();
        let response = match response {
            Ok(resp) => resp,
            Err(e) => return Err(Error::ReqwestError(e)),
        };
        if response.status() == 429 {
            return Err(Error::TooManyRequests);
        }
        Ok(response)
    }

    fn post<T:Serialize>(&self, url: &str, json: T) -> Result<reqwest::blocking::Response, Error> {
        let json = serialize(json)?;
        let response = self.client
            .post(url)
            .json(&json)
            .send();
        let response = match response {
            Ok(resp) => resp,
            Err(e) => return Err(Error::ReqwestError(e)),
        };
        if response.status() == 429 {
            return Err(Error::TooManyRequests);
        }
        Ok(response)
    }
}