I am trying to build a Streamlit application using Azure Speech Services. I would like to show the Live Recognized Content on the UI.
If I am using recognize_once() logic mentioned below, I am able to see the output in the app UI, whereas when I am trying to use start_continuous_recognition(), somehow the st.write() logic is being skipped.
Recognizing Once Logic:
def speech_recognize_once_from_mic():
# Set up the speech config and audio config
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=service_region)
audio_config = speechsdk.AudioConfig(use_default_microphone=True)
# Create a speech recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
st.write("Speak into your microphone.")
result = speech_recognizer.recognize_once_async().get()
# Check the result
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
return f"Recognized: {result.text}"
elif result.reason == speechsdk.ResultReason.NoMatch:
return "No speech could be recognized"
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
return f"Speech Recognition canceled: {cancellation_details.reason}"
else:
return "Unknown error"
# Simple UI for processing the audio input
st.title("Azure Speech Service with Streamlit")
if st.button('Start speech recognition'):
recognition_result = speech_recognize_once_from_mic()
st.write(recognition_result)
Continuous Recognition Logic:
def recognized_callback(evt):
# Callback function that appends recognized speech to chunks
global chunks
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
chunks.append(evt.result.text)
print(f"Recognized: {evt.result.text}")
st.write(chunks)
print('Done')
def process_audio():
# Set up the recognizer
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
recognizer.recognized.connect(recognized_callback)
recognizer.session_stopped.connect(session_stopped_callback)
# Store the recognizer in the session state to access it later
st.session_state.recognizer = recognizer
# Start continuous recognition
recognizer.start_continuous_recognition()
How can I fix this?
Python - 3.11.1
Streamlit - 1.32.0
Thanks in advance!