Update app_parallel.py
Browse files- app_parallel.py +43 -26
app_parallel.py
CHANGED
@@ -7,32 +7,49 @@ from io import BytesIO
|
|
7 |
import asyncio
|
8 |
|
9 |
async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
audio_bytes =
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def get_models():
|
38 |
return get_model_names()
|
|
|
7 |
import asyncio
|
8 |
|
9 |
async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
10 |
+
try:
|
11 |
+
edge_tts_voice = voice_mapping.get(selected_voice)
|
12 |
+
if not edge_tts_voice:
|
13 |
+
return {"error": f"Invalid voice '{selected_voice}'."}, None
|
14 |
+
|
15 |
+
voice_upload_file = None
|
16 |
+
if use_uploaded_voice and voice_upload is not None:
|
17 |
+
with open(voice_upload.name, 'rb') as f:
|
18 |
+
voice_upload_file = f.read()
|
19 |
+
|
20 |
+
info, edge_output_filename, tts_output_data = await asyncio.wait_for(
|
21 |
+
tts(model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file),
|
22 |
+
timeout=60 # Adjust timeout as needed
|
23 |
+
)
|
24 |
+
|
25 |
+
if isinstance(info, dict) and "error" in info:
|
26 |
+
return info, None
|
27 |
+
|
28 |
+
tgt_sr, audio_output = tts_output_data
|
29 |
+
|
30 |
+
audio_bytes = None
|
31 |
+
if isinstance(audio_output, np.ndarray):
|
32 |
+
byte_io = BytesIO()
|
33 |
+
wavfile.write(byte_io, tgt_sr, audio_output.astype(np.int16))
|
34 |
+
byte_io.seek(0)
|
35 |
+
audio_bytes = byte_io.getvalue()
|
36 |
+
else:
|
37 |
+
audio_bytes = audio_output
|
38 |
+
|
39 |
+
# Clean up the temporary EdgeTTS output file if it exists
|
40 |
+
if edge_output_filename and os.path.exists(edge_output_filename):
|
41 |
+
os.remove(edge_output_filename)
|
42 |
+
|
43 |
+
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
44 |
+
return {"info": info}, audio_data_uri
|
45 |
+
|
46 |
+
except asyncio.TimeoutError:
|
47 |
+
return {"error": "Operation timed out"}, None
|
48 |
+
except asyncio.CancelledError:
|
49 |
+
return {"error": "Operation was cancelled"}, None
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Error in convert_tts: {str(e)}")
|
52 |
+
return {"error": str(e)}, None
|
53 |
|
54 |
def get_models():
|
55 |
return get_model_names()
|