Update app_parallel.py
Browse files- app_parallel.py +24 -42
app_parallel.py
CHANGED
@@ -7,48 +7,32 @@ 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 |
-
edge_tts_voice = voice_mapping.get(selected_voice)
|
14 |
-
if not edge_tts_voice:
|
15 |
-
print(f"Invalid voice selected: {selected_voice}")
|
16 |
-
return {"error": f"Invalid voice '{selected_voice}'."}, None
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
voice_upload_file = f.read()
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
timeout=60
|
28 |
-
)
|
29 |
-
print("TTS function call completed.")
|
30 |
|
31 |
-
|
32 |
-
print(f"Error returned from TTS function: {info['error']}")
|
33 |
-
return info, None
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
os.remove(edge_output_filename)
|
41 |
-
|
42 |
-
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
43 |
-
return {"info": info}, audio_data_uri
|
44 |
-
|
45 |
-
except asyncio.TimeoutError:
|
46 |
-
return {"error": "Operation timed out"}, None
|
47 |
-
except asyncio.CancelledError:
|
48 |
-
return {"error": "Operation was cancelled"}, None
|
49 |
-
except Exception as e:
|
50 |
-
print(f"Error in convert_tts: {str(e)}")
|
51 |
-
return {"error": str(e)}, None
|
52 |
|
53 |
def get_models():
|
54 |
return get_model_names()
|
@@ -68,11 +52,9 @@ iface = gr.Interface(
|
|
68 |
],
|
69 |
outputs=[
|
70 |
gr.JSON(label="Info"),
|
71 |
-
gr.
|
72 |
],
|
73 |
title="Text-to-Speech Conversion"
|
74 |
-
).queue(
|
75 |
-
|
76 |
-
iface.launch()
|
77 |
-
|
78 |
|
|
|
|
7 |
import asyncio
|
8 |
|
9 |
async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
10 |
+
edge_tts_voice = voice_mapping.get(selected_voice)
|
11 |
+
if not edge_tts_voice:
|
12 |
+
return {"error": f"Invalid voice '{selected_voice}'."}, None
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
voice_upload_file = None
|
15 |
+
if use_uploaded_voice and voice_upload is not None:
|
16 |
+
with open(voice_upload.name, 'rb') as f:
|
17 |
+
voice_upload_file = f.read()
|
|
|
18 |
|
19 |
+
info, edge_tts_output_path, tts_output_data, edge_output_file = await tts(
|
20 |
+
model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file
|
21 |
+
)
|
|
|
|
|
|
|
22 |
|
23 |
+
_, audio_output = tts_output_data
|
|
|
|
|
24 |
|
25 |
+
audio_bytes = None
|
26 |
+
if isinstance(audio_output, np.ndarray):
|
27 |
+
byte_io = BytesIO()
|
28 |
+
wavfile.write(byte_io, 40000, audio_output)
|
29 |
+
byte_io.seek(0)
|
30 |
+
audio_bytes = byte_io.read()
|
31 |
+
else:
|
32 |
+
audio_bytes = audio_output
|
33 |
|
34 |
+
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
35 |
+
return {"info": info}, audio_data_uri
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def get_models():
|
38 |
return get_model_names()
|
|
|
52 |
],
|
53 |
outputs=[
|
54 |
gr.JSON(label="Info"),
|
55 |
+
gr.Textbox(label="Audio URI")
|
56 |
],
|
57 |
title="Text-to-Speech Conversion"
|
58 |
+
).queue(default_concurrency_limit=6) # Set concurrency limit to 6 based on your hardware
|
|
|
|
|
|
|
59 |
|
60 |
+
iface.launch()
|