razaAhmed commited on
Commit
d5fe7dc
·
1 Parent(s): c829d22

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from gtts import gTTS
3
+ from io import BytesIO
4
+ import base64
5
+
6
+ # Function to convert text to speech
7
+ def text_to_speech(text):
8
+ tts = gTTS(text)
9
+ audio_buffer = BytesIO()
10
+ tts.write_to_fp(audio_buffer)
11
+ audio_buffer.seek(0)
12
+ return audio_buffer
13
+
14
+ # Function to set download link for audio file
15
+ def get_audio_download_link(buffer, filename="audio.mp3"):
16
+ buffer.seek(0)
17
+ b64_data = base64.b64encode(buffer.read()).decode()
18
+ href = f'<a href="data:audio/mp3;base64,{b64_data}" download="{filename}">Download audio file</a>'
19
+ return href
20
+
21
+ # Create Streamlit application
22
+ st.title('Text to Speech Converter')
23
+
24
+ # Input for text from user
25
+ user_input_text = st.text_area("Enter the text you want to convert to speech:")
26
+
27
+ if st.button('Convert'):
28
+ if user_input_text:
29
+ # Call function to convert text to speech
30
+ audio_buffer = text_to_speech(user_input_text)
31
+
32
+ # Display audio player and download link
33
+ st.audio(audio_buffer, format="audio/mp3", start_time=0)
34
+ st.markdown(get_audio_download_link(audio_buffer), unsafe_allow_html=True)
35
+ else:
36
+ st.warning('Please input some text to convert.')
37
+
38
+
39
+