Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,53 +3,49 @@ from gtts import gTTS
|
|
3 |
from io import BytesIO
|
4 |
import time
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Display error message if no text input is provided
|
53 |
-
st.warning("Please enter text or upload a text file.")
|
54 |
-
|
55 |
-
main()
|
|
|
3 |
from io import BytesIO
|
4 |
import time
|
5 |
|
6 |
+
st.title("Text to Speech Converter 💭")
|
7 |
+
|
8 |
+
# Text input
|
9 |
+
text_input = st.text_area("Enter text to convert to speech📝", height=150)
|
10 |
+
|
11 |
+
# File uploader for .txt files
|
12 |
+
st.sidebar.title("Upload Text File📁")
|
13 |
+
uploaded_file = st.sidebar.file_uploader("Choose a .txt file", type="txt")
|
14 |
+
|
15 |
+
if uploaded_file is not None:
|
16 |
+
# Read the file
|
17 |
+
file_text = uploaded_file.read().decode("utf-8")
|
18 |
+
|
19 |
+
# Display the content of the file
|
20 |
+
st.subheader("Text from Uploaded File")
|
21 |
+
st.text(file_text)
|
22 |
+
|
23 |
+
# Append the content of the file to the text input
|
24 |
+
text_input += "\n\n" + file_text
|
25 |
+
|
26 |
+
# Language selection
|
27 |
+
language = st.selectbox("Select language", ["en", "fr", "es", "ru", "hi"])
|
28 |
+
|
29 |
+
# Generate speech button
|
30 |
+
if st.button("Generate Speech 🎤"):
|
31 |
+
if text_input:
|
32 |
+
# Display spinner while generating speech
|
33 |
+
with st.spinner("Generating speech..."):
|
34 |
+
# Create a gTTS object
|
35 |
+
tts = gTTS(text_input, lang=language)
|
36 |
+
|
37 |
+
# Create a BytesIO object to store the audio data
|
38 |
+
audio_stream = BytesIO()
|
39 |
+
|
40 |
+
# Save speech to the BytesIO object
|
41 |
+
tts.write_to_fp(audio_stream)
|
42 |
+
|
43 |
+
# Simulate delay to mimic speech generation time
|
44 |
+
time.sleep(2)
|
45 |
+
|
46 |
+
# Display success message and audio player
|
47 |
+
st.success("Speech generated successfully!")
|
48 |
+
st.audio(audio_stream)
|
49 |
+
else:
|
50 |
+
# Display error message if no text input is provided
|
51 |
+
st.warning("Please enter text or upload a text file.")
|
|
|
|
|
|
|
|