File size: 1,389 Bytes
ea87857 | 1 2 3 4 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 | # 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()
|