Audio-Testing / app.py
ThatOneCoder's picture
Create app.py
d0e0126 verified
raw
history blame
1.47 kB
import streamlit as st
import speech_recognition as sr
from threading import Timer
import time
def listen_for_speech():
# Initialize recognizer class
r = sr.Recognizer()
# Starts listening in the background (non-blocking)
with sr.Microphone() as source:
# Adjust for ambient noise for 1 second
r.adjust_for_ambient_noise(source, duration=1)
st.write("Listening...")
try:
# Listen for 5 seconds, then this turns into pumpkin (stops listening)
audio = r.listen(source, timeout=5, phrase_time_limit=5)
# Use Google Web Speech API to convert audio to text
text = r.recognize_google(audio)
st.write("You said: " + text)
except sr.WaitTimeoutError:
st.warning("Hmm... It seems the airwaves were silent. Please press the button and speak up!")
except Exception as e:
# Handle all possible exceptions from the recognizer
st.error(f"Oops! Something went wrong. Here's what: {e}")
def start_listening():
# This is where the magic happens... or starts happening
t = Timer(0, listen_for_speech)
t.start()
# We'll give it 10 seconds total before killing this quest for words
# Just enough to go "Anybody home?" or "Going once, going twice..."
time.sleep(10)
t.cancel()
# Streamlit app begins
st.title('The Blabber Be-Gone!')
if st.button('Press me and speak!'):
start_listening()