Rahul-Crudcook's picture
Update app.py
f932b29 verified
from deep_translator import GoogleTranslator
import streamlit as st
from gtts import gTTS
import tempfile
# Title of the app
st.title("Multilingual Text-to-Speech Converter")
# User input text
text_input = st.text_area("Enter the text you want to convert to speech:")
# Language selection
language_options = {"English": "en", "Hindi": "hi", "Spanish": "es", "French": "fr", "German": "de"}
selected_language = st.selectbox("Select output language", options=language_options.keys())
selected_lang_code = language_options[selected_language]
# Generate audio if text is provided
if st.button("Convert to Speech"):
if text_input.strip() == "":
st.warning("Please enter some text.")
else:
# Translate text to the selected language using deep-translator
translator = GoogleTranslator(source='auto', target=selected_lang_code)
translated_text = translator.translate(text_input)
# Convert translated text to speech using gTTS
tts = gTTS(text=translated_text, lang=selected_lang_code, slow=False)
# Save the output to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tts.save(tmp_file.name)
audio_file_path = tmp_file.name
# Load and play the audio file in the app
st.audio(audio_file_path, format="audio/mp3")
# Provide download link
st.success("Speech synthesis complete!")
with open(audio_file_path, "rb") as file:
st.download_button(label="Download MP3", data=file, file_name="translated_speech_output.mp3", mime="audio/mp3")