Skip to content

Commit cef03ee

Browse files
committed
wip
1 parent d591f90 commit cef03ee

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

apps/99_streamlit_examples/pages/13_Azure_AI_Speech.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from os import getenv
23

34
import azure.cognitiveservices.speech as speechsdk
@@ -59,13 +60,55 @@ def get_speech_config():
5960

6061

6162
def from_mic():
62-
speech_recognizer = speechsdk.SpeechRecognizer(
63+
conversation_transcriber = speechsdk.transcription.ConversationTranscriber(
6364
speech_config=get_speech_config(),
6465
)
6566

66-
print("Speak into your microphone.")
67-
speech_recognition_result = speech_recognizer.recognize_once_async().get()
68-
print(speech_recognition_result.text)
67+
transcribing_stop = False
68+
69+
def stop_cb(evt: speechsdk.SessionEventArgs):
70+
# """callback that signals to stop continuous recognition upon receiving an event `evt`"""
71+
print(f"CLOSING on {evt}")
72+
nonlocal transcribing_stop
73+
transcribing_stop = True
74+
75+
# Connect callbacks to the events fired by the conversation transcriber
76+
conversation_transcriber.transcribed.connect(conversation_transcriber_transcribed_cb)
77+
conversation_transcriber.session_started.connect(conversation_transcriber_session_started_cb)
78+
conversation_transcriber.session_stopped.connect(conversation_transcriber_session_stopped_cb)
79+
conversation_transcriber.canceled.connect(conversation_transcriber_recognition_canceled_cb)
80+
# stop transcribing on either session stopped or canceled events
81+
conversation_transcriber.session_stopped.connect(stop_cb)
82+
conversation_transcriber.canceled.connect(stop_cb)
83+
84+
conversation_transcriber.start_transcribing_async()
85+
86+
# Waits for completion.
87+
while not transcribing_stop:
88+
time.sleep(0.5)
89+
90+
conversation_transcriber.stop_transcribing_async()
91+
92+
93+
def conversation_transcriber_recognition_canceled_cb(evt: speechsdk.SessionEventArgs):
94+
print("Canceled event")
95+
96+
97+
def conversation_transcriber_session_stopped_cb(evt: speechsdk.SessionEventArgs):
98+
print("SessionStopped event")
99+
100+
101+
def conversation_transcriber_transcribed_cb(evt: speechsdk.SpeechRecognitionEventArgs):
102+
print("TRANSCRIBED:")
103+
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
104+
print(f"\tText={evt.result.text}")
105+
print(f"\tSpeaker ID={evt.result.speaker_id}")
106+
elif evt.result.reason == speechsdk.ResultReason.NoMatch:
107+
print(f"\tNOMATCH: Speech could not be TRANSCRIBED: {evt.result.no_match_details}")
108+
109+
110+
def conversation_transcriber_session_started_cb(evt: speechsdk.SessionEventArgs):
111+
print("SessionStarted event")
69112

70113

71114
st.title("Azure AI Speech Services")

0 commit comments

Comments
 (0)