MAZALA2024 commited on
Commit
32fb87d
·
verified ·
1 Parent(s): fa72cc7

Update app_parallel.py

Browse files
Files changed (1) hide show
  1. 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
- try:
11
- print(f"Starting TTS for text: {tts_text[:50]}...") # Log the start of processing
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
- voice_upload_file = None
19
- if use_uploaded_voice and voice_upload is not None:
20
- print("Processing uploaded voice file...")
21
- with open(voice_upload.name, 'rb') as f:
22
- voice_upload_file = f.read()
23
 
24
- print("Calling TTS function...")
25
- info, edge_output_filename, tts_output_data = await asyncio.wait_for(
26
- tts(model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file),
27
- timeout=60
28
- )
29
- print("TTS function call completed.")
30
 
31
- if isinstance(info, dict) and "error" in info:
32
- print(f"Error returned from TTS function: {info['error']}")
33
- return info, None
34
 
35
- print("Processing TTS output...")
36
- tgt_sr, audio_output = tts_output_data
 
 
 
 
 
 
37
 
38
- # Clean up the temporary EdgeTTS output file if it exists
39
- if edge_output_filename and os.path.exists(edge_output_filename):
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.Audio(label="Generated Audio", type="numpy")
72
  ],
73
  title="Text-to-Speech Conversion"
74
- ).queue(concurrency_limit=6, max_batch_size=1)
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()