Audio-Testing / app.py
ThatOneCoder's picture
Create app.py
9b86a45 verified
raw
history blame
1.57 kB
import streamlit as st
from pocketsphinx import LiveSpeech, get_model_path
import os
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
# Function to capture audio from the mic
def record_audio(filename='temp.wav', duration=5, fs=16000):
st.text("Recording Audio...")
with st.spinner(f'Recording for {duration} seconds...'):
myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
sd.wait() # Waits until recording is finished
wav.write(filename, fs, myrecording) # Save as WAV file
# Get the model path for pocketsphinx
model_path = get_model_path()
config = {
'verbose': False,
'hmm': os.path.join(model_path, 'en-us'),
'lm': os.path.join(model_path, 'en-us.lm.bin'),
'dict': os.path.join(model_path, 'cmudict-en-us.dict')
}
# Streamlit UI
st.title("Simple Speech Recognition with Streamlit and PocketSphinx")
button = st.button("Press to Speak")
# Store the state of the recording
if 'recording_done' not in st.session_state:
st.session_state.recording_done = False
# When the button is pressed
if button:
# Record the audio
record_audio()
st.session_state.recording_done = True
# If an audio was recorded, process it with PocketSphinx
if st.session_state.recording_done:
audio = LiveSpeech(**config)
st.text("Processing Audio...")
with st.spinner('Recognizing...'):
for phrase in audio:
st.write(phrase)
break # We'll stop after the first phrase
# Reset the state
st.session_state.recording_done = False