MAZALA2024 commited on
Commit
0826e3b
·
verified ·
1 Parent(s): f72b616

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -4,40 +4,48 @@ import numpy as np
4
  from scipy.io import wavfile
5
  from voice_processing import parallel_tts, get_model_names, voice_mapping
6
  from io import BytesIO
7
- import asyncio # Import asyncio
 
 
 
 
8
 
9
- # Define an asynchronous function for the Gradio interface
10
  async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
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
- # Create task for parallel processing
21
- task = (
22
- model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file
23
- )
24
-
25
- # Asynchronous call to your tts processing function using parallel processing
26
- result = await asyncio.get_event_loop().run_in_executor(None, parallel_tts, [task])
27
- info, _, (tgt_sr, audio_output) = result[0]
 
28
 
29
- # Process audio output to bytes
30
- audio_bytes = None
31
- if isinstance(audio_output, np.ndarray):
32
- byte_io = BytesIO()
33
- wavfile.write(byte_io, tgt_sr, audio_output)
34
- byte_io.seek(0)
35
- audio_bytes = byte_io.read()
36
- else:
37
- audio_bytes = audio_output
38
 
39
- audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
40
- return {"info": info}, audio_data_uri
 
41
 
42
  def get_models():
43
  return get_model_names()
@@ -58,10 +66,11 @@ iface = gr.Interface(
58
  ],
59
  outputs=[
60
  gr.JSON(label="Info"),
61
- gr.Textbox(label="Audio URI")
62
  ],
63
  title="Text-to-Speech Conversion"
64
- )
65
 
66
  # Launch the interface
67
- iface.launch(debug=True) # Set share=True to create a public link
 
 
4
  from scipy.io import wavfile
5
  from voice_processing import parallel_tts, get_model_names, voice_mapping
6
  from io import BytesIO
7
+ import asyncio
8
+ import logging
9
+
10
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
11
+ logger = logging.getLogger(__name__)
12
 
 
13
  async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
14
+ try:
15
+ edge_tts_voice = voice_mapping.get(selected_voice)
16
+ if not edge_tts_voice:
17
+ raise ValueError(f"Invalid voice '{selected_voice}'.")
18
+
19
+ voice_upload_file = None
20
+ if use_uploaded_voice and voice_upload is not None:
21
+ with open(voice_upload.name, 'rb') as f:
22
+ voice_upload_file = f.read()
23
 
24
+ # Create task for parallel processing
25
+ task = (
26
+ model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file
27
+ )
28
+
29
+ # Asynchronous call to your tts processing function using parallel processing
30
+ result = await asyncio.get_event_loop().run_in_executor(None, parallel_tts, [task])
31
+ info, _, (tgt_sr, audio_output) = result[0]
32
 
33
+ # Process audio output to bytes
34
+ audio_bytes = None
35
+ if isinstance(audio_output, np.ndarray):
36
+ byte_io = BytesIO()
37
+ wavfile.write(byte_io, tgt_sr, audio_output)
38
+ byte_io.seek(0)
39
+ audio_bytes = byte_io.read()
40
+ else:
41
+ audio_bytes = audio_output
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 Exception as e:
47
+ logger.exception("Error in convert_tts")
48
+ return {"error": str(e)}, None
49
 
50
  def get_models():
51
  return get_model_names()
 
66
  ],
67
  outputs=[
68
  gr.JSON(label="Info"),
69
+ gr.Audio(label="Generated Audio", type="uri")
70
  ],
71
  title="Text-to-Speech Conversion"
72
+ ).queue(concurrency_count=16) # Adjust based on your server's capacity
73
 
74
  # Launch the interface
75
+ if __name__ == "__main__":
76
+ iface.launch(debug=True)