MK-316 commited on
Commit
ea87857
·
verified ·
1 Parent(s): efaf579

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import libraries
2
+ import streamlit as st
3
+ from gtts import gTTS
4
+ import os
5
+
6
+ # Function to handle the Text-to-Speech conversion
7
+ def text_to_speech(text, lang='en', tld='com'):
8
+ # Handle the language and TLD options
9
+ if lang == 'ko':
10
+ tts = gTTS(text, lang='ko')
11
+ else:
12
+ tts = gTTS(text, lang=lang, tld=tld)
13
+ audio_file = 'output.mp3'
14
+ tts.save(audio_file) # Save the output as an MP3 file
15
+ return audio_file
16
+
17
+ # Streamlit interface
18
+ def main():
19
+ st.title("Text-to-Speech Converter")
20
+
21
+ # User input for text
22
+ user_input = st.text_area("Enter the text you want to convert to speech:", "Hello World")
23
+
24
+ # Language selection
25
+ language = st.selectbox("Choose Language", options=['en', 'ko'], format_func=lambda x: 'English' if x == 'en' else 'Korean')
26
+
27
+ # TLD selection based on language
28
+ if language == 'en':
29
+ tld = st.selectbox("Choose Accent", options=[('AmE', 'com'), ('BrE', 'co.uk')], format_func=lambda x: x[0])
30
+ tld = tld[1] # Get the TLD value from the tuple
31
+ else:
32
+ tld = 'none'
33
+
34
+ # Button to convert text to speech
35
+ if st.button("Convert"):
36
+ audio_path = text_to_speech(user_input, lang=language, tld=tld)
37
+ audio_file = open(audio_path, 'rb')
38
+ audio_bytes = audio_file.read()
39
+ st.audio(audio_bytes, format='audio/mp3')
40
+
41
+ if __name__ == '__main__':
42
+ main()