Spaces:
Sleeping
Sleeping
File size: 1,465 Bytes
d0e0126 |
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 |
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() |