Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import tempfile
|
| 2 |
import edge_tts
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 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
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
for result in results:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Provide path for downloading the combined file
|
| 76 |
-
return combined_file_path
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
)
|
| 93 |
-
return interface
|
| 94 |
|
| 95 |
# Launch the Gradio interface
|
| 96 |
-
|
| 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()
|
|
|