TextToSpeech / app.py
EliSafina's picture
Update app.py
77e7ccc verified
raw
history blame
1.59 kB
import streamlit as st
from gtts import gTTS
from io import BytesIO
import time
st.title("Text to Speech Converter 💭")
# Text input
text_input = st.text_area("Enter text to convert to speech📝", height=150)
# File uploader for .txt files
st.sidebar.title("Upload Text File📁")
uploaded_file = st.sidebar.file_uploader("Choose a .txt file", type="txt")
if uploaded_file is not None:
# Read the file
file_text = uploaded_file.read().decode("utf-8")
# Display the content of the file
st.subheader("Text from Uploaded File")
st.text(file_text)
# Append the content of the file to the text input
text_input += "\n\n" + file_text
# Language selection
language = st.selectbox("Select language", ["en", "fr", "es", "ru", "hi"])
# Generate speech button
if st.button("Generate Speech 🎤"):
if text_input:
# Display spinner while generating speech
with st.spinner("Generating speech..."):
# Create a gTTS object
tts = gTTS(text_input, lang=language)
# Create a BytesIO object to store the audio data
audio_stream = BytesIO()
# Save speech to the BytesIO object
tts.write_to_fp(audio_stream)
# Simulate delay to mimic speech generation time
time.sleep(2)
# Display success message and audio player
st.success("Speech generated successfully!")
st.audio(audio_stream)
else:
# Display error message if no text input is provided
st.warning("Please enter text or upload a text file.")