Text-to-Speech / app.py
mjawad17's picture
Create app.py
200f5a0 verified
raw
history blame
3.63 kB
import streamlit as st
from gtts import gTTS
from io import BytesIO
# Dictionary to map language codes to full names
LANGUAGES = {
"ar": "Arabic",
"bn": "Bengali",
"zh": "Chinese",
"nl": "Dutch",
"en": "English",
"fa": "Persian",
"fr": "French",
"de": "German",
"el": "Greek",
"he": "Hebrew",
"hi": "Hindi",
"it": "Italian",
"ja": "Japanese",
"ko": "Korean",
"ms": "Malay",
"no": "Norwegian",
"pl": "Polish",
"pt": "Portuguese",
"ru": "Russian",
"es": "Spanish",
"sv": "Swedish",
"th": "Thai",
"tr": "Turkish",
"ur": "Urdu",
"vi": "Vietnamese",
}
# Function to convert text to speech
def text_to_speech(text, lang='en'):
tts = gTTS(text=text, lang=lang, slow=False)
mp3_fp = BytesIO()
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0)
return mp3_fp
# # Custom CSS for styling
# st.markdown(
# """
# <style>
# /* Background color for the whole app */
# .stApp {
# background-color: #f0f2f6;
# color: #333333;
# font-family: 'Arial', sans-serif;
# }
# /* Style for markdown headings */
# h1, h2, h3, h4, h5, h6 {
# color: #4B0082; /* Indigo color */
# font-family: 'Verdana', sans-serif;
# }
# /* Style for the text area */
# .stTextArea {
# background-color: #ffffff; /* White background */
# color: #333333; /* Dark text color */
# border-radius: 10px;
# border: 1px solid #cccccc;
# }
# /* Style for buttons */
# .stButton button {
# background-color: #4B0082; /* Indigo background */
# color: #ffffff; /* White text */
# border-radius: 5px;
# border: none;
# padding: 10px 20px;
# font-size: 16px;
# }
# /* Style for selectbox */
# .stSelectbox {
# background-color: #ffffff;
# color: #333333;
# border-radius: 5px;
# border: 1px solid #cccccc;
# }
# /* Divider line */
# .stCaption {
# border-top: 1px solid #cccc87;
# }
# </style>
# """,
# unsafe_allow_html=True
# )
# Custom CSS for gradient background
st.markdown(
"""
<style>
.stApp {
background: linear-gradient(to bottom right, #6a11cb, #2575fc);
}
</style>
""",
unsafe_allow_html=True
)
# Streamlit app layout
st.title("Text-to-Speech App")
# Instruction for using the app in markdown
st.markdown("### Instructions For Using App")
st.markdown(
"""
1. Enter the text you want to convert to speech in the text box below.
2. Select the language from the dropdown menu.
3. Click the 'Convert to Speech' button to generate the audio file.
4. Click the 'Download' button to download the audio file.
"""
)
st.caption("---")
# Text input
user_text = st.text_area("Enter your text:", "Hello, world!")
# Language selection
lang_code = st.selectbox(
"Choose language",
options=list(LANGUAGES.keys()),
format_func=lambda x: LANGUAGES[x] # Display full name
)
# Initialize file variable
audio_file = None
# Convert text to speech and provide download button
if st.button("Convert to Speech"):
if user_text:
audio_file = text_to_speech(user_text, lang_code)
st.audio(audio_file, format='audio/mp3')
# Make the audio file downloadable
st.download_button(
label="Download Audio",
data=audio_file,
file_name="output.mp3",
mime="audio/mp3"
)
else:
st.error("Please enter some text!")
st.markdown("Developed by Muhammad Jawad.")