Skip to content

Commit 3f2e777

Browse files
committed
refactor to dynamically add more engines based on json
1 parent afaa93e commit 3f2e777

9 files changed

+1587
-1526
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Also a streamlit app to demo this.
66

7+
Note: tts-data/**vendor**-**software**.json is a list of tts engines not supported by tts-wrapper. Add to that if you wish
8+
79
To-Do
810

911
- Add Ivona, Nuance etc voices

main.py

+65-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime, timedelta
88
import logging
99
from fuzzysearch import find_near_matches
10+
import uvicorn
1011

1112
# Set up logging
1213
logging.basicConfig(level=logging.INFO)
@@ -17,8 +18,27 @@
1718
# In-memory cache for voice data
1819
cache = {}
1920

20-
# List of engines for dropdown
21-
engines_list = ["polly", "google", "microsoft", "watson", "elevenlabs", "witai", "mms", "acapela", "other"]
21+
# List of engines for dropdown - we update this im main
22+
engines_list = ["polly", "google", "microsoft", "watson", "elevenlabs", "witai", "mms"]
23+
24+
25+
def load_tts_engines(directory):
26+
tts_engines = {}
27+
for filename in os.listdir(directory):
28+
if filename.endswith(".json"):
29+
filepath = os.path.join(directory, filename)
30+
with open(filepath, 'r') as file:
31+
engine_data = json.load(file)
32+
engine_name = filename.replace('.json', '')
33+
tts_engines[engine_name] = engine_data
34+
return tts_engines
35+
36+
def update_engines_list(engines_list, tts_engines):
37+
for engine_name in tts_engines.keys():
38+
if engine_name not in engines_list:
39+
engines_list.append(engine_name)
40+
return engines_list
41+
2242

2343
class Voice(BaseModel):
2444
id: str
@@ -38,24 +58,22 @@ def find_geo_info(language_code, geo_data):
3858
return 0.0, 0.0, 'Unknown' # Default values if no match is found
3959

4060
def load_voices_from_source(engine: str):
41-
other_voices_file = 'misc-misc.json'
42-
acapela_voices_file = 'acapela-acapela.json'
61+
tts_engines_directory = "./tts-data"
4362
voices = []
4463
geo_data = load_geo_data() # Load geographical data
4564

46-
if engine == 'other':
47-
with open(other_voices_file, 'r') as file:
65+
# Load the specific engine's JSON file if it exists
66+
engine_file_path = os.path.join(tts_engines_directory, f"{engine}.json")
67+
if os.path.isfile(engine_file_path):
68+
with open(engine_file_path, 'r') as file:
4869
voices_raw = json.load(file)
49-
voices = [{"engine": item.get("engine", engine), **item} for item in voices_raw]
50-
elif engine == 'acapela':
51-
with open(acapela_voices_file, 'r') as file:
52-
voices_raw = json.load(file)
53-
voices = [{"engine": 'acapela', **voice} for voice in voices_raw]
70+
voices = [{"engine": engine, **item} for item in voices_raw]
5471
else:
5572
tts = get_tts(engine)
5673
if tts:
5774
try:
5875
voices_raw = tts.get_voices()
76+
print(voices_raw)
5977
voices = [{"engine": engine, **voice} for voice in voices_raw]
6078
except Exception as e:
6179
logging.info(f"Failed to get voices for engine {engine}: {e}")
@@ -208,12 +226,45 @@ def get_voices(engine: Optional[str] = Query(None, enum=engines_list), lang_code
208226

209227
return [Voice(**voice) for voice in paginated_voices]
210228

211-
if __name__ == '__main__':
229+
@app.get("/engines", response_model=List[str])
230+
def get_available_engines():
231+
return engines_list
232+
233+
234+
def load_tts_engines(directory):
235+
tts_engines = {}
236+
for filename in os.listdir(directory):
237+
if filename.endswith(".json"):
238+
filepath = os.path.join(directory, filename)
239+
with open(filepath, 'r') as file:
240+
engine_data = json.load(file)
241+
engine_name = filename.replace('.json', '')
242+
tts_engines[engine_name] = engine_data
243+
return tts_engines
244+
245+
def update_engines_list(engines_list, tts_engines):
246+
for engine_name in tts_engines.keys():
247+
if engine_name not in engines_list:
248+
engines_list.append(engine_name)
249+
return engines_list
250+
251+
252+
if __name__ == "__main__":
212253
is_development = os.getenv('DEVELOPMENT') == 'True'
213254
if is_development:
214255
print("Loading credentials")
215256
from load_credentials import load_credentials
216257
load_credentials()
217258

218-
import uvicorn
219-
uvicorn.run(app, host='127.0.0.1', port=8000)
259+
260+
tts_engines_directory = "./tts-data"
261+
tts_engines = load_tts_engines(tts_engines_directory)
262+
engines_list = update_engines_list(engines_list, tts_engines)
263+
264+
print("Updated Engines List:", engines_list)
265+
266+
for engine_name, engine_data in tts_engines.items():
267+
print(f"Loaded TTS Engine: {engine_name} with data: {engine_data}")
268+
269+
# Run the Uvicorn server
270+
uvicorn.run(app, host='127.0.0.1', port=8000)

0 commit comments

Comments
 (0)