Spaces:
Running
Running
import gradio as gr | |
import requests | |
import tempfile | |
TTS_API = "http://216.48.191.137:8081/tts" | |
# Voice label mapping | |
voice_map = { | |
"Female": "0 speaker", | |
"Male": "1 speaker" | |
} | |
def get_tts_audio(prompt, voice_label): | |
if len(prompt.strip()) > 100 or len(prompt.strip().split()) > 15: | |
return "⚠️ Please enter only one short Tamil phrase." | |
try: | |
voice = voice_map.get(voice_label, "0 speaker") | |
params = { | |
"prompt": prompt, | |
"voice": voice | |
} | |
response = requests.get(TTS_API, params=params) | |
if response.status_code == 200 and response.content: | |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") | |
temp_file.write(response.content) | |
temp_file.flush() | |
return temp_file.name | |
else: | |
return "⚠️ Speech generation failed. Please try again." | |
except Exception as e: | |
print("Error:", e) | |
return "⚠️ Could not connect to the server. Please try again." | |
# Tamil examples related to AI | |
examples = [ | |
[" வணக்கம்! இந்த பயன்பாட்டை பயன்படுத்தி உங்கள் உரையை குரலாக்கலாம்.", "Female"], | |
[" தன்னாட்சி ரோபோட்கள் தொழில்துறையில் பெரும் மாற்றத்தை ஏற்படுத்துகின்றன.", "Female"], | |
[" தகவல் தொழில்நுட்பம் மனித உடலை புரிந்து கொள்கிறது.", "Female"], | |
[" அறிவீனம் கொண்ட கணினிகள் தீர்வுகளை முன்மொழிகின்றன.", "Male"], | |
[" புதிய கண்டுபிடிப்புகள் எதிர்காலத்தை மாற்றும்.", "Female"] | |
] | |
with gr.Blocks(title="தமிழ் உரை -> குரல் மாற்றி") as demo: | |
gr.Markdown("## 🗣️ தமிழ் உரையை குரலாக்கு (TTS)\nஒரே ஒரு சிறிய சொற்றொடரை உள்ளிடுங்கள் மற்றும் குரலை தேர்வு செய்யுங்கள்.") | |
with gr.Row(): | |
input_text = gr.Textbox( | |
label="உரை (தமிழில் மட்டும்)", | |
placeholder="உதாரணம்: செயற்கை நுண்ணறிவு உலகை மாற்றுகிறது", | |
lines=1, | |
max_lines=1, | |
max_length=100 | |
) | |
with gr.Row(): | |
voice_select = gr.Dropdown(label="குரல் (Voice)", choices=["Female", "Male"], value="Female") | |
with gr.Row(): | |
output_audio = gr.Audio(label="உங்கள் குரல்", type="filepath") | |
with gr.Row(): | |
submit_btn = gr.Button("🔊 உரையை பேசு", interactive=False) | |
# Enable/disable button based on whether input has valid text | |
def toggle_button_state(text): | |
return gr.update(interactive=bool(text.strip())) | |
input_text.change(fn=toggle_button_state, inputs=input_text, outputs=submit_btn) | |
input_text.input(fn=toggle_button_state, inputs=input_text, outputs=submit_btn) | |
submit_btn.click(fn=get_tts_audio, inputs=[input_text, voice_select], outputs=output_audio) | |
gr.Examples(examples=examples, inputs=[input_text, voice_select]) | |
demo.launch(share=True) | |