File size: 1,586 Bytes
614f4a3
 
 
a11cb67
614f4a3
77e7ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
import streamlit as st
from gtts import gTTS
from io import BytesIO
import time

st.title("Text to Speech Converter 💭")

# Text input
text_input = st.text_area("Enter text to convert to speech📝", height=150)

# File uploader for .txt files
st.sidebar.title("Upload Text File📁")
uploaded_file = st.sidebar.file_uploader("Choose a .txt file", type="txt")

if uploaded_file is not None:
    # Read the file
    file_text = uploaded_file.read().decode("utf-8")
    
    # Display the content of the file
    st.subheader("Text from Uploaded File")
    st.text(file_text)
    
    # Append the content of the file to the text input
    text_input += "\n\n" + file_text

# Language selection
language = st.selectbox("Select language", ["en", "fr", "es", "ru", "hi"])

# Generate speech button
if st.button("Generate Speech 🎤"):
    if text_input:
        # Display spinner while generating speech
        with st.spinner("Generating speech..."):
            # Create a gTTS object
            tts = gTTS(text_input, lang=language)

            # Create a BytesIO object to store the audio data
            audio_stream = BytesIO()

            # Save speech to the BytesIO object
            tts.write_to_fp(audio_stream)

            # Simulate delay to mimic speech generation time
            time.sleep(2)

        # Display success message and audio player
        st.success("Speech generated successfully!")
        st.audio(audio_stream)
    else:
        # Display error message if no text input is provided
        st.warning("Please enter text or upload a text file.")