Spaces:
Sleeping
Sleeping
Added code
Browse files- app.py +88 -0
- output_346429d9-80be-40d8-a304-f6b1ada10ae6.mp3 +0 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import edge_tts
|
| 2 |
+
import asyncio
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import uuid
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
+
|
| 7 |
+
# Voice mapping by language and gender
|
| 8 |
+
voice_options = {
|
| 9 |
+
"English - US": {
|
| 10 |
+
"Female": "en-US-AriaNeural",
|
| 11 |
+
"Male": "en-US-GuyNeural"
|
| 12 |
+
},
|
| 13 |
+
"English - India": {
|
| 14 |
+
"Female": "en-IN-NeerjaNeural",
|
| 15 |
+
"Male": "en-IN-PrabhatNeural"
|
| 16 |
+
},
|
| 17 |
+
"Hindi": {
|
| 18 |
+
"Female": "hi-IN-SwaraNeural",
|
| 19 |
+
"Male": "hi-IN-MadhurNeural"
|
| 20 |
+
},
|
| 21 |
+
"Tamil": {
|
| 22 |
+
"Female": "ta-IN-PallaviNeural",
|
| 23 |
+
"Male": "ta-IN-ValluvarNeural"
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
async def generate_tts(text, voice):
|
| 28 |
+
if not text.strip():
|
| 29 |
+
raise ValueError("Input text is empty")
|
| 30 |
+
filename = f"output_{uuid.uuid4()}.mp3"
|
| 31 |
+
communicate = edge_tts.Communicate(text, voice=voice)
|
| 32 |
+
await communicate.save(filename)
|
| 33 |
+
return filename
|
| 34 |
+
|
| 35 |
+
def tts_wrapper(text, language, gender, translation_direction):
|
| 36 |
+
try:
|
| 37 |
+
original_text = text.strip()
|
| 38 |
+
if not original_text:
|
| 39 |
+
return "Input text is empty", None
|
| 40 |
+
|
| 41 |
+
# Translation if selected
|
| 42 |
+
if translation_direction != "None":
|
| 43 |
+
if translation_direction == "English to Hindi":
|
| 44 |
+
text = GoogleTranslator(source='en', target='hi').translate(original_text)
|
| 45 |
+
elif translation_direction == "Hindi to English":
|
| 46 |
+
text = GoogleTranslator(source='hi', target='en').translate(original_text)
|
| 47 |
+
else:
|
| 48 |
+
text = original_text
|
| 49 |
+
else:
|
| 50 |
+
text = original_text
|
| 51 |
+
|
| 52 |
+
# Select voice
|
| 53 |
+
voice = voice_options.get(language, {}).get(gender)
|
| 54 |
+
if not voice:
|
| 55 |
+
return "Voice not found for the selection", None
|
| 56 |
+
|
| 57 |
+
print(f"Text for TTS: {text}")
|
| 58 |
+
print(f"Using voice: {voice}")
|
| 59 |
+
|
| 60 |
+
filename = asyncio.run(generate_tts(text, voice))
|
| 61 |
+
return text, filename
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error: {str(e)}", None
|
| 65 |
+
|
| 66 |
+
# Gradio interface
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=tts_wrapper,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Textbox(label="Enter Text", placeholder="Type something to convert to voice..."),
|
| 71 |
+
gr.Dropdown(choices=list(voice_options.keys()), label="Select Language", value="Hindi"),
|
| 72 |
+
gr.Radio(choices=["Female", "Male"], label="Voice Gender", value="Female"),
|
| 73 |
+
gr.Dropdown(
|
| 74 |
+
choices=["None", "English to Hindi", "Hindi to English"],
|
| 75 |
+
label="Translation Direction",
|
| 76 |
+
value="None"
|
| 77 |
+
)
|
| 78 |
+
],
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.Textbox(label="Translated Text or Error"),
|
| 81 |
+
gr.Audio(label="Generated Voice")
|
| 82 |
+
],
|
| 83 |
+
title="🎙️ AI Voice Generator with English ↔ Hindi Translation",
|
| 84 |
+
description="Choose voice, gender, and translation direction. Text is translated and spoken aloud!"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Launch with share=True for public link
|
| 88 |
+
iface.launch(share=True)
|
output_346429d9-80be-40d8-a304-f6b1ada10ae6.mp3
ADDED
|
Binary file (7.49 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
edge-tts
|
| 3 |
+
deep-translator
|