hivecorp commited on
Commit
d928d82
·
verified ·
1 Parent(s): 3a891b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import tempfile
2
  import edge_tts
3
  import gradio as gr
4
- import asyncio
5
 
 
6
  language_dict = {
7
  "Hindi": {
8
  "Madhur": "hi-IN-MadhurNeural",
@@ -47,51 +48,49 @@ language_dict = {
47
  "Leah": "en-ZA-LeahNeural",
48
  "Luke": "en-ZA-LukeNeural"
49
  },
50
- # Add more languages here as needed...
51
  }
52
 
53
- async def text_to_speech_async(text, voice, lang):
54
- communicate = edge_tts.Communicate(text, voice, lang)
55
- return await communicate.save("output.mp3")
56
 
57
- def process_input_text(input_text, voice, lang):
58
- # Split input into parts of 5000 characters
59
- parts = [input_text[i:i+5000] for i in range(0, len(input_text), 5000)]
60
-
61
- # Process parts concurrently
62
- tasks = [text_to_speech_async(part, voice, lang) for part in parts]
63
-
64
- # Run all tasks concurrently and await results
65
- results = asyncio.run(asyncio.gather(*tasks))
66
-
67
- # Combine the resulting files into a single audio
68
- with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as combined_file:
69
- combined_file_path = combined_file.name
70
- with open(combined_file_path, 'wb') as output_file:
 
 
 
71
  for result in results:
72
- with open(result, 'rb') as input_file:
73
- output_file.write(input_file.read())
74
-
75
- # Provide path for downloading the combined file
76
- return combined_file_path
77
 
78
- def create_audio_interface(input_text, voice, lang):
79
- output_file = process_input_text(input_text, voice, lang)
80
- return output_file
 
81
 
82
- def create_gradio_interface():
83
- interface = gr.Interface(
84
- fn=create_audio_interface,
85
- inputs=[
86
- gr.Textbox(lines=4, label="Enter Text"),
87
- gr.Dropdown(choices=["Madhur", "Swara", "Jenny", "Guy", "Ana", "Aria", "Brian"], label="Select Voice"),
88
- gr.Dropdown(choices=["Hindi", "English"], label="Select Language")
89
- ],
90
- outputs=gr.File(label="Download Audio"),
91
- title="Text to Speech with Edge TTS"
92
- )
93
- return interface
94
 
95
  # Launch the Gradio interface
96
- gradio_interface = create_gradio_interface()
97
- gradio_interface.launch()
 
1
  import tempfile
2
  import edge_tts
3
  import gradio as gr
4
+ from concurrent.futures import ThreadPoolExecutor
5
 
6
+ # Language and voice selection dictionary
7
  language_dict = {
8
  "Hindi": {
9
  "Madhur": "hi-IN-MadhurNeural",
 
48
  "Leah": "en-ZA-LeahNeural",
49
  "Luke": "en-ZA-LukeNeural"
50
  },
51
+ # Add other languages...
52
  }
53
 
54
+ # Function to chunk text into parts of max 5000 characters
55
+ def chunk_text(text, max_length=5000):
56
+ return [text[i:i + max_length] for i in range(0, len(text), max_length)]
57
 
58
+ # Function to generate speech for each chunk using edge_tts
59
+ async def generate_speech(text_chunk, language, voice):
60
+ communicate = edge_tts.Communicate(text_chunk, voice=language_dict[language][voice])
61
+ audio_data = await communicate.save()
62
+ return audio_data
63
+
64
+ # Function to process text and generate speech
65
+ def process_text_to_speech(text, language, voice):
66
+ chunks = chunk_text(text)
67
+ with ThreadPoolExecutor() as executor:
68
+ futures = [executor.submit(generate_speech, chunk, language, voice) for chunk in chunks]
69
+ results = [future.result() for future in futures]
70
+
71
+ # Combine all audio parts into a single file
72
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as output_file:
73
+ output_filename = output_file.name
74
+ with open(output_filename, "wb") as f:
75
  for result in results:
76
+ f.write(result)
77
+ return output_filename
 
 
 
78
 
79
+ # Gradio interface function
80
+ def gradio_interface(text, language, voice):
81
+ audio_filename = process_text_to_speech(text, language, voice)
82
+ return audio_filename
83
 
84
+ # Gradio UI setup
85
+ iface = gr.Interface(
86
+ fn=gradio_interface,
87
+ inputs=[
88
+ gr.Textbox(label="Enter Text"),
89
+ gr.Dropdown(choices=list(language_dict.keys()), label="Select Language"),
90
+ gr.Dropdown(choices=["Madhur", "Swara", "Jenny", "Guy", "Ana", "Aria", "Brian"], label="Select Voice")
91
+ ],
92
+ outputs=gr.File(label="Download Audio File")
93
+ )
 
 
94
 
95
  # Launch the Gradio interface
96
+ iface.launch()