# Import libraries import streamlit as st from gtts import gTTS import os # Function to handle the Text-to-Speech conversion def text_to_speech(text, lang='en', tld='com'): # Handle the language and TLD options if lang == 'ko': tts = gTTS(text, lang='ko') else: tts = gTTS(text, lang=lang, tld=tld) audio_file = 'output.mp3' tts.save(audio_file) # Save the output as an MP3 file return audio_file # Streamlit interface def main(): st.title("Text-to-Speech Converter") # User input for text user_input = st.text_area("Enter the text you want to convert to speech:", "Hello World") # Language selection language = st.selectbox("Choose Language", options=['en', 'ko'], format_func=lambda x: 'English' if x == 'en' else 'Korean') # TLD selection based on language if language == 'en': tld = st.selectbox("Choose Accent", options=[('AmE', 'com'), ('BrE', 'co.uk')], format_func=lambda x: x[0]) tld = tld[1] # Get the TLD value from the tuple else: tld = 'none' # Button to convert text to speech if st.button("Convert"): audio_path = text_to_speech(user_input, lang=language, tld=tld) audio_file = open(audio_path, 'rb') audio_bytes = audio_file.read() st.audio(audio_bytes, format='audio/mp3') if __name__ == '__main__': main()