MAZALA2024 commited on
Commit
2c87986
·
verified ·
1 Parent(s): 334a9cd

Update app_parallel.py

Browse files
Files changed (1) hide show
  1. 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
- 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()
 
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()