|
| 1 | +import time |
1 | 2 | from os import getenv
|
2 | 3 |
|
3 | 4 | import azure.cognitiveservices.speech as speechsdk
|
@@ -59,13 +60,55 @@ def get_speech_config():
|
59 | 60 |
|
60 | 61 |
|
61 | 62 | def from_mic():
|
62 |
| - speech_recognizer = speechsdk.SpeechRecognizer( |
| 63 | + conversation_transcriber = speechsdk.transcription.ConversationTranscriber( |
63 | 64 | speech_config=get_speech_config(),
|
64 | 65 | )
|
65 | 66 |
|
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") |
69 | 112 |
|
70 | 113 |
|
71 | 114 | st.title("Azure AI Speech Services")
|
|
0 commit comments