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
|
| 5 |
+
# Dictionary to map language codes to full names
|
| 6 |
+
LANGUAGES = {
|
| 7 |
+
"ar": "Arabic",
|
| 8 |
+
"bn": "Bengali",
|
| 9 |
+
"zh": "Chinese",
|
| 10 |
+
"nl": "Dutch",
|
| 11 |
+
"en": "English",
|
| 12 |
+
"fa": "Persian",
|
| 13 |
+
"fr": "French",
|
| 14 |
+
"de": "German",
|
| 15 |
+
"el": "Greek",
|
| 16 |
+
"he": "Hebrew",
|
| 17 |
+
"hi": "Hindi",
|
| 18 |
+
"it": "Italian",
|
| 19 |
+
"ja": "Japanese",
|
| 20 |
+
"ko": "Korean",
|
| 21 |
+
"ms": "Malay",
|
| 22 |
+
"no": "Norwegian",
|
| 23 |
+
"pl": "Polish",
|
| 24 |
+
"pt": "Portuguese",
|
| 25 |
+
"ru": "Russian",
|
| 26 |
+
"es": "Spanish",
|
| 27 |
+
"sv": "Swedish",
|
| 28 |
+
"th": "Thai",
|
| 29 |
+
"tr": "Turkish",
|
| 30 |
+
"ur": "Urdu",
|
| 31 |
+
"vi": "Vietnamese",
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# Function to convert text to speech
|
| 35 |
+
def text_to_speech(text, lang='en'):
|
| 36 |
+
tts = gTTS(text=text, lang=lang, slow=False)
|
| 37 |
+
mp3_fp = BytesIO()
|
| 38 |
+
tts.write_to_fp(mp3_fp)
|
| 39 |
+
mp3_fp.seek(0)
|
| 40 |
+
return mp3_fp
|
| 41 |
+
|
| 42 |
+
# # Custom CSS for styling
|
| 43 |
+
# st.markdown(
|
| 44 |
+
# """
|
| 45 |
+
# <style>
|
| 46 |
+
# /* Background color for the whole app */
|
| 47 |
+
# .stApp {
|
| 48 |
+
# background-color: #f0f2f6;
|
| 49 |
+
# color: #333333;
|
| 50 |
+
# font-family: 'Arial', sans-serif;
|
| 51 |
+
# }
|
| 52 |
+
|
| 53 |
+
# /* Style for markdown headings */
|
| 54 |
+
# h1, h2, h3, h4, h5, h6 {
|
| 55 |
+
# color: #4B0082; /* Indigo color */
|
| 56 |
+
# font-family: 'Verdana', sans-serif;
|
| 57 |
+
# }
|
| 58 |
+
|
| 59 |
+
# /* Style for the text area */
|
| 60 |
+
# .stTextArea {
|
| 61 |
+
# background-color: #ffffff; /* White background */
|
| 62 |
+
# color: #333333; /* Dark text color */
|
| 63 |
+
# border-radius: 10px;
|
| 64 |
+
# border: 1px solid #cccccc;
|
| 65 |
+
# }
|
| 66 |
+
|
| 67 |
+
# /* Style for buttons */
|
| 68 |
+
# .stButton button {
|
| 69 |
+
# background-color: #4B0082; /* Indigo background */
|
| 70 |
+
# color: #ffffff; /* White text */
|
| 71 |
+
# border-radius: 5px;
|
| 72 |
+
# border: none;
|
| 73 |
+
# padding: 10px 20px;
|
| 74 |
+
# font-size: 16px;
|
| 75 |
+
# }
|
| 76 |
+
|
| 77 |
+
# /* Style for selectbox */
|
| 78 |
+
# .stSelectbox {
|
| 79 |
+
# background-color: #ffffff;
|
| 80 |
+
# color: #333333;
|
| 81 |
+
# border-radius: 5px;
|
| 82 |
+
# border: 1px solid #cccccc;
|
| 83 |
+
# }
|
| 84 |
+
|
| 85 |
+
# /* Divider line */
|
| 86 |
+
# .stCaption {
|
| 87 |
+
# border-top: 1px solid #cccc87;
|
| 88 |
+
# }
|
| 89 |
+
# </style>
|
| 90 |
+
# """,
|
| 91 |
+
# unsafe_allow_html=True
|
| 92 |
+
# )
|
| 93 |
+
|
| 94 |
+
# Custom CSS for gradient background
|
| 95 |
+
st.markdown(
|
| 96 |
+
"""
|
| 97 |
+
<style>
|
| 98 |
+
.stApp {
|
| 99 |
+
background: linear-gradient(to bottom right, #6a11cb, #2575fc);
|
| 100 |
+
}
|
| 101 |
+
</style>
|
| 102 |
+
""",
|
| 103 |
+
unsafe_allow_html=True
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Streamlit app layout
|
| 107 |
+
st.title("Text-to-Speech App")
|
| 108 |
+
|
| 109 |
+
# Instruction for using the app in markdown
|
| 110 |
+
st.markdown("### Instructions For Using App")
|
| 111 |
+
st.markdown(
|
| 112 |
+
"""
|
| 113 |
+
1. Enter the text you want to convert to speech in the text box below.
|
| 114 |
+
2. Select the language from the dropdown menu.
|
| 115 |
+
3. Click the 'Convert to Speech' button to generate the audio file.
|
| 116 |
+
4. Click the 'Download' button to download the audio file.
|
| 117 |
+
"""
|
| 118 |
+
)
|
| 119 |
+
st.caption("---")
|
| 120 |
+
|
| 121 |
+
# Text input
|
| 122 |
+
user_text = st.text_area("Enter your text:", "Hello, world!")
|
| 123 |
+
|
| 124 |
+
# Language selection
|
| 125 |
+
lang_code = st.selectbox(
|
| 126 |
+
"Choose language",
|
| 127 |
+
options=list(LANGUAGES.keys()),
|
| 128 |
+
format_func=lambda x: LANGUAGES[x] # Display full name
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Initialize file variable
|
| 132 |
+
audio_file = None
|
| 133 |
+
|
| 134 |
+
# Convert text to speech and provide download button
|
| 135 |
+
if st.button("Convert to Speech"):
|
| 136 |
+
if user_text:
|
| 137 |
+
audio_file = text_to_speech(user_text, lang_code)
|
| 138 |
+
st.audio(audio_file, format='audio/mp3')
|
| 139 |
+
|
| 140 |
+
# Make the audio file downloadable
|
| 141 |
+
st.download_button(
|
| 142 |
+
label="Download Audio",
|
| 143 |
+
data=audio_file,
|
| 144 |
+
file_name="output.mp3",
|
| 145 |
+
mime="audio/mp3"
|
| 146 |
+
)
|
| 147 |
+
else:
|
| 148 |
+
st.error("Please enter some text!")
|
| 149 |
+
|
| 150 |
+
st.markdown("Developed by Muhammad Jawad.")
|