forked from wafflecomposite/15.ai-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifteen_api_usage_example.py
57 lines (46 loc) · 2.01 KB
/
fifteen_api_usage_example.py
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
import os
from fifteen_api import FifteenAPI
# initialization
tts_api = FifteenAPI(show_debug=True)
# be aware that there is a serverside max text length. If text is too long, it will be trimmed.
print(tts_api.max_text_len)
### valid usage examples
# get tts raw bytes (well, assuming that Fluttershy is not currently disabled)
response = tts_api.get_tts_raw("Fluttershy", "Neutral", "This is a test")
assert response["status"] == "OK"
assert len(response["data"]) > 100000 # those are .wav audiofile bytes
# save tts to file with generated filename
response = tts_api.save_to_file("Fluttershy", "Neutral", "This is another test")
assert response["status"] == "OK"
assert response["filename"] != None # this is a generated filename of TTS file
print(response)
os.remove(response["filename"])
# save tts to file with target filename.
response = tts_api.save_to_file("Fluttershy", "Neutral", "One more test", "tts.wav")
assert response["status"] == "OK"
assert response["filename"] == "tts.wav"
print(response)
os.remove("tts.wav")
# if filename doesn't end with '.wav', it will be added automatically
response = tts_api.save_to_file("Fluttershy", "Neutral", "Last one valid test", "randomfilename")
assert response["status"] == "OK"
assert response["filename"] == "randomfilename.wav"
print(response)
os.remove("randomfilename.wav")
### invalid usage examples
# unavailable character
response = tts_api.save_to_file("random character or an incorrect name", "Neutral", "Test?", "tts.wav")
assert response["status"] != "OK"
assert response["filename"] == None
print(response)
# emotion that doesn't exist
response = tts_api.save_to_file("Fluttershy", "Super extra angry!", "Angry test!!!", "tts.wav")
assert response["status"] != "OK"
assert response["filename"] == None
print(response)
# assume that 15.ai api is currently broken
tts_api.tts_url = "https://example.com/brokenapi"
response = tts_api.save_to_file("Fluttershy", "Neutral", "...test?", "tts.wav")
assert response["status"] != "OK"
assert response["filename"] == None
print(response)