temprary / app.py
utkarsh2299's picture
Create app.py
82642cd verified
import streamlit as st
import requests
import json
import base64
# Streamlit UI
st.title("TTS")
# User inputs
text = st.text_area("Enter the text to convert to speech")
# Language selection
lang_options = ['Hindi', 'Malayalam', 'Manipuri', 'Marathi', 'Kannada', 'English', 'Assamese', 'Tamil', 'Odia', 'Rajasthani', 'Telugu', 'Bengali', 'Gujarati']
lang = st.selectbox("Select language", lang_options)
# Gender selection
gender_options = ['Male', 'Female']
gender = st.selectbox("Select gender", gender_options)
# Dictionary mapping language and gender to API URLs
api_urls = {
('Hindi', 'Female'): "https://asr.iitm.ac.in/tts/hindi/female",
('Hindi', 'Male'): "https://asr.iitm.ac.in/tts/hindi/male",
('English', 'Male'): "https://asr.iitm.ac.in/tts/english/male",
('English', 'Female'): "https://asr.iitm.ac.in/tts/english/female",
('Malayalam', 'Male'): "https://asr.iitm.ac.in/tts/malayalam/male",
('Malayalam', 'Female'): "https://asr.iitm.ac.in/tts/malayalam/female",
('Tamil', 'Male'): "https://asr.iitm.ac.in/tts/tamil/male",
('Tamil', 'Female'): "https://asr.iitm.ac.in/tts/tamil/female",
('Telugu', 'Male'): "https://asr.iitm.ac.in/tts/telugu/male",
('Telugu', 'Female'): "https://asr.iitm.ac.in/tts/telugu/female",
('Punjabi', 'Male'): "https://asr.iitm.ac.in/tts/punjabi/male",
('Punjabi', 'Female'): "https://asr.iitm.ac.in/tts/punjabi/female",
('Urdu', 'Male'): "https://asr.iitm.ac.in/tts/urdu/male",
('Urdu', 'Female'): "https://asr.iitm.ac.in/tts/urdu/female",
('Marathi', 'Male'): "https://asr.iitm.ac.in/tts/marathi/male",
('Marathi', 'Female'): "https://asr.iitm.ac.in/tts/marathi/female",
('Gujarati', 'Male'): "https://asr.iitm.ac.in/tts/gujarati/male",
('Gujarati', 'Female'): "https://asr.iitm.ac.in/tts/gujarati/female",
('Assamese', 'Male'): "https://asr.iitm.ac.in/tts/assamese/male",
('Assamese', 'Female'): "https://asr.iitm.ac.in/tts/assamese/female",
('Bengali', 'Male'): "https://asr.iitm.ac.in/tts/bengali/male",
('Bengali', 'Female'): "https://asr.iitm.ac.in/tts/bengali/female",
('Bodo', 'Male'): "https://asr.iitm.ac.in/tts/bodo/male",
('Bodo', 'Female'): "https://asr.iitm.ac.in/tts/bodo/female",
('Kannada', 'Male'): "https://asr.iitm.ac.in/tts/kannada/male",
('Kannada', 'Female'): "https://asr.iitm.ac.in/tts/kannada/female",
('Manipuri', 'Male'): "https://asr.iitm.ac.in/tts/Manipuri/male",
('Manipuri', 'Female'): "https://asr.iitm.ac.in/tts/Manipuri/female",
('Odia', 'Male'): "https://asr.iitm.ac.in/tts/odia/male",
('Odia', 'Female'): "https://asr.iitm.ac.in/tts/odia/female",
('Rajasthani', 'Male'): "https://asr.iitm.ac.in/tts/rajasthani/male",
('Rajasthani', 'Female'): "https://asr.iitm.ac.in/tts/rajasthani/female",
# Add more combinations as needed
}
# Get the URL based on language and gender selection
selected_url = api_urls.get((lang, gender))
if selected_url:
# Button to trigger TTS
if st.button("Generate Speech"):
# Prepare payload
payload = {
"input": text,
"gender": gender.lower(),
"lang": lang.lower(),
"alpha": 1,
"segmentwise": "True"
}
# Make API request
response = requests.post(selected_url, json=payload)
# Handle response
if response.status_code == 200:
# Decode audio
audio = response.json()['audio']
audio_bytes = base64.b64decode(audio)
st.audio(audio_bytes, format='audio/mp3')
else:
st.error("Failed to generate speech. Please check your input and try again.")
else:
st.error("Selected language and gender combination is not supported.")