File size: 2,052 Bytes
3d981be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import streamlit as st
import speech_recognition as sr
import pyttsx3
from streamlit.components.v1 import html

# Initialize the recognizer
recognizer = sr.Recognizer()

# Initialize the text-to-speech engine
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

def speech_to_text():
    try:
        # Use microphone as the audio source
        with sr.Microphone() as source:
            # Adjust for ambient noise
            recognizer.adjust_for_ambient_noise(source, duration=0.2)

            st.info("Speak something...")
            # Listen for user input
            audio = recognizer.listen(source)

            # Use Google's Speech Recognition
            text = recognizer.recognize_google(audio)
            return text
    except sr.RequestError as e:
        st.error("Could not request results; {0}".format(e))
    except sr.UnknownValueError:
        st.error("Unknown error occurred")

def text_to_speech(text):
    # Convert text to speech
    engine.save_to_file(text, "output.mp3")
    engine.runAndWait()

# Streamlit app layout
st.title("Speech-to-Text and Text-to-Speech App")

# Text input box for user input
user_input = st.text_input("Enter text:", "")

# Main content
if st.button("Convert to Speech"):
    if user_input:
        text_to_speech(user_input)
        st.audio("output.mp3", format="audio/mp3", start_time=0)
        st.markdown("<script>document.getElementsByTagName('audio')[0].play()</script>", unsafe_allow_html=True)
    else:
        st.warning("Please enter some text.")

import base64

def autoplay_audio(file_path: str):
    with open(file_path, "rb") as f:
        data = f.read()
        b64 = base64.b64encode(data).decode()
        md = f"""
            <audio controls autoplay="true">
            <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
            </audio>
            """
        st.markdown(
            md,
            unsafe_allow_html=True,
        )

st.write("# Auto-playing Audio!")
autoplay_audio("output.mp3")