7
7
from datetime import datetime , timedelta
8
8
import logging
9
9
from fuzzysearch import find_near_matches
10
+ import uvicorn
10
11
11
12
# Set up logging
12
13
logging .basicConfig (level = logging .INFO )
17
18
# In-memory cache for voice data
18
19
cache = {}
19
20
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
+
22
42
23
43
class Voice (BaseModel ):
24
44
id : str
@@ -38,24 +58,22 @@ def find_geo_info(language_code, geo_data):
38
58
return 0.0 , 0.0 , 'Unknown' # Default values if no match is found
39
59
40
60
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"
43
62
voices = []
44
63
geo_data = load_geo_data () # Load geographical data
45
64
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 :
48
69
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 ]
54
71
else :
55
72
tts = get_tts (engine )
56
73
if tts :
57
74
try :
58
75
voices_raw = tts .get_voices ()
76
+ print (voices_raw )
59
77
voices = [{"engine" : engine , ** voice } for voice in voices_raw ]
60
78
except Exception as e :
61
79
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
208
226
209
227
return [Voice (** voice ) for voice in paginated_voices ]
210
228
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__" :
212
253
is_development = os .getenv ('DEVELOPMENT' ) == 'True'
213
254
if is_development :
214
255
print ("Loading credentials" )
215
256
from load_credentials import load_credentials
216
257
load_credentials ()
217
258
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