YORUBA-TTS / app.py
ayscript's picture
Update app.py
d663b53 verified
import gradio as gr
import requests
import os
# Define the API URLs for both languages
MODELS = {
"English": "https://api-inference.huggingface.co/models/facebook/mms-tts-eng",
"Yoruba": "https://api-inference.huggingface.co/models/facebook/mms-tts-yor"
}
# Get token from environment variables
hf_token = os.getenv("HF_TOKEN")
headers = {"Authorization": f"Bearer {hf_token}"}
def text_to_speech(text, language):
if not text.strip():
return None # Do nothing if text is empty
api_url = MODELS[language]
try:
response = requests.post(api_url, headers=headers, json={"inputs": text})
response.raise_for_status() # Check for errors
except Exception as e:
# This creates a red error popup in the UI instead of crashing
raise gr.Error(f"API Error: {str(e)}")
# Save audio to file
output_file = "output.wav"
with open(output_file, "wb") as f:
f.write(response.content)
# RETURN ONLY THE FILE PATH (Single value)
return output_file
# Create the Interface
with gr.Blocks() as demo:
gr.Markdown("# πŸ‡³πŸ‡¬ English & Yoruba Text-to-Speech")
with gr.Row():
txt_input = gr.Textbox(label="Enter Text", placeholder="Type something here...")
lang_dropdown = gr.Dropdown(choices=["English", "Yoruba"], value="English", label="Select Language")
btn = gr.Button("Generate Audio")
audio_out = gr.Audio(label="Output Audio")
# Ensure inputs and outputs match the function signature
btn.click(fn=text_to_speech, inputs=[txt_input, lang_dropdown], outputs=audio_out)
demo.launch()