Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from TTS.api import TTS
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
# Load multilingual TTS model (supports English and Arabic, with voice options)
|
| 6 |
+
model_name = "tts_models/multilingual/multi-dataset/your_tts"
|
| 7 |
+
tts = TTS(model_name)
|
| 8 |
+
|
| 9 |
+
# Get available speakers from model
|
| 10 |
+
available_speakers = tts.speakers
|
| 11 |
+
|
| 12 |
+
def text_to_speech(text, language, speaker_name):
|
| 13 |
+
try:
|
| 14 |
+
# Save to temporary WAV file
|
| 15 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 16 |
+
tts.tts_to_file(text=text, speaker=speaker_name, language=language, file_path=f.name)
|
| 17 |
+
return f.name
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Error: {str(e)}"
|
| 20 |
+
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=text_to_speech,
|
| 23 |
+
inputs=[
|
| 24 |
+
gr.Textbox(label="Enter text (English or Arabic)"),
|
| 25 |
+
gr.Dropdown(choices=["en", "ar"], label="Language"),
|
| 26 |
+
gr.Dropdown(choices=available_speakers, label="Voice (Male/Female)"),
|
| 27 |
+
],
|
| 28 |
+
outputs=gr.Audio(type="filepath", label="Generated Audio"),
|
| 29 |
+
title="English & Arabic Text-to-Speech App",
|
| 30 |
+
description="Type your text, choose the language and voice, and listen!"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
interface.launch()
|