EliSafina commited on
Commit
77e7ccc
1 Parent(s): 542ef8c

Update app.py

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