forked from Azure-Samples/cognitive-services-speech-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intent_sample.py
243 lines (202 loc) · 11.7 KB
/
intent_sample.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
import time
try:
import azure.cognitiveservices.speech as speechsdk
except ImportError:
print("""
Importing the Speech SDK for Python failed.
Refer to
https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstart-python for
installation instructions.
""")
import sys
sys.exit(1)
# Set up the subscription info for the Language Understanding Service: Note that this is not the
# same subscription as the one for the Speech Service. Replace with your own language understanding
# subscription key and service region (e.g., "westus").
intent_key = "YourLanguageUnderstandingSubscriptionKey"
intent_service_region = "YourLanguageUnderstandingServiceRegion"
language_understanding_app_id = "YourLanguageUnderstandingAppId"
# Specify the path to a audio file containing speech (mono WAV / PCM with a sampling rate of 16
# kHz).
lampfilename = "TurnOnTheLamp.wav"
def recognize_intent_once_from_mic():
"""performs one-shot intent recognition from input from the default microphone"""
# <IntentRecognitionOnceWithMic>
# Set up the config for the intent recognizer (remember that this uses the Language Understanding key, not the Speech Services key)!
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)
# set up the intents that are to be recognized. These can be a mix of simple phrases and
# intents specified through a LanguageUnderstanding Model.
model = speechsdk.intent.LanguageUnderstandingModel(app_id=language_understanding_app_id)
intents = [
(model, "HomeAutomation.TurnOn"),
(model, "HomeAutomation.TurnOff"),
("This is a test.", "test"),
("Switch to channel 34.", "34"),
("what's the weather like", "weather"),
]
intent_recognizer.add_intents(intents)
# Starts intent recognition, and returns after a single utterance is recognized. The end of a
# single utterance is determined by listening for silence at the end or until a maximum of 15
# seconds of audio is processed. It returns the recognition text as result.
# Note: Since recognize_once() returns only a single utterance, it is suitable only for single
# shot recognition like command or query.
# For long-running multi-utterance recognition, use start_continuous_recognition() instead.
intent_result = intent_recognizer.recognize_once()
# Check the results
if intent_result.reason == speechsdk.ResultReason.RecognizedIntent:
print("Recognized: \"{}\" with intent id `{}`".format(intent_result.text, intent_result.intent_id))
elif intent_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(intent_result.text))
elif intent_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(intent_result.no_match_details))
elif intent_result.reason == speechsdk.ResultReason.Canceled:
print("Intent recognition canceled: {}".format(intent_result.cancellation_details.reason))
if intent_result.cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(intent_result.cancellation_details.error_details))
# </IntentRecognitionOnceWithMic>
def recognize_intent_once_from_file():
"""performs one-shot intent recognition from input from an audio file"""
# <IntentRecognitionOnceWithFile>
# Set up the config for the intent recognizer (remember that this uses the Language Understanding key, not the Speech Services key)!
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(filename=lampfilename)
# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)
# set up the intents that are to be recognized. These can be a mix of simple phrases and
# intents specified through a LanguageUnderstanding Model.
model = speechsdk.intent.LanguageUnderstandingModel(app_id=language_understanding_app_id)
intents = [
(model, "HomeAutomation.TurnOn"),
(model, "HomeAutomation.TurnOff"),
("This is a test.", "test"),
("Switch the to channel 34.", "34"),
("what's the weather like", "weather"),
]
intent_recognizer.add_intents(intents)
# Starts intent recognition, and returns after a single utterance is recognized. The end of a
# single utterance is determined by listening for silence at the end or until a maximum of 15
# seconds of audio is processed. It returns the recognition text as result.
# Note: Since recognize_once() returns only a single utterance, it is suitable only for single
# shot recognition like command or query.
# For long-running multi-utterance recognition, use start_continuous_recognition() instead.
intent_result = intent_recognizer.recognize_once()
# Check the results
if intent_result.reason == speechsdk.ResultReason.RecognizedIntent:
print("Recognized: \"{}\" with intent id `{}`".format(intent_result.text, intent_result.intent_id))
elif intent_result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(intent_result.text))
elif intent_result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(intent_result.no_match_details))
elif intent_result.reason == speechsdk.ResultReason.Canceled:
print("Intent recognition canceled: {}".format(intent_result.cancellation_details.reason))
if intent_result.cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(intent_result.cancellation_details.error_details))
# </IntentRecognitionOnceWithFile>
def recognize_intent_once_async_from_mic():
"""performs one-shot asynchronous intent recognition from input from the default microphone"""
# Set up the config for the intent recognizer
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)
# Add callbacks to the recognition events
# Set up a flag to mark when asynchronous recognition is done
done = False
def recognized_callback(evt):
"""
Callback that is called on successful recognition of a full utterance by both speech
processing and intent classification
"""
result = evt.result
print("Recognized: \"{}\" with intent id `{}`".format(result.text, result.intent_id))
nonlocal done
done = True
def canceled_callback(evt):
"""Callback that is called on a failure by either speech or language processing"""
result = evt.result
print("Intent recognition canceled: {}".format(result.cancellation_details.reason))
if result.cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(result.cancellation_details.error_details))
nonlocal done
done = True
def recognizing_callback(evt):
"""Callback that is called on intermediate results from speech transcription"""
result = evt.result
print("Intermediate transcription: \"{}\"".format(result.text))
# Connect the callbacks
intent_recognizer.recognized.connect(recognized_callback)
intent_recognizer.canceled.connect(canceled_callback)
intent_recognizer.recognizing.connect(recognizing_callback)
# set up the intents that are to be recognized. These can be a mix of simple phrases and
# intents specified through a LanguageUnderstanding Model.
model = speechsdk.intent.LanguageUnderstandingModel(app_id=language_understanding_app_id)
intents = [
(model, "HomeAutomation.TurnOn"),
(model, "HomeAutomation.TurnOff"),
("This is a test.", "test"),
("Switch to channel 34.", "34"),
("what's the weather like", "weather"),
]
intent_recognizer.add_intents(intents)
# Starts non-blocking intent recognition and stop after a single utterance has been recognized.
# The end of a single utterance is determined by listening for silence at the end or until a
# maximum of 15 seconds of audio is processed.
# Note: Since recognize_once() stops after a single utterance, it is suitable only for single
# shot recognition like command or query. For long-running multi-utterance recognition, use
# start_continuous_recognition() instead.
intent_recognizer.recognize_once_async()
# wait until recognition is complete
while not done:
time.sleep(1)
def recognize_intent_continuous():
"""performs one-shot intent recognition from input from an audio file"""
# <IntentContinuousRecognitionWithFile>
intent_config = speechsdk.SpeechConfig(subscription=intent_key, region=intent_service_region)
audio_config = speechsdk.audio.AudioConfig(filename=lampfilename)
# Set up the intent recognizer
intent_recognizer = speechsdk.intent.IntentRecognizer(speech_config=intent_config, audio_config=audio_config)
# set up the intents that are to be recognized. These can be a mix of simple phrases and
# intents specified through a LanguageUnderstanding Model.
model = speechsdk.intent.LanguageUnderstandingModel(app_id=language_understanding_app_id)
intents = [
(model, "HomeAutomation.TurnOn"),
(model, "HomeAutomation.TurnOff"),
("This is a test.", "test"),
("Switch the channel to 34.", "34"),
("what's the weather like", "weather"),
]
intent_recognizer.add_intents(intents)
# Connect callback functions to the signals the intent recognizer fires.
done = False
def stop_cb(evt):
"""callback that stops continuous recognition upon receiving an event `evt`"""
print('CLOSING on {}'.format(evt))
intent_recognizer.stop_continuous_recognition()
nonlocal done
done = True
intent_recognizer.session_started.connect(lambda evt: print("SESSION_START: {}".format(evt)))
intent_recognizer.speech_end_detected.connect(lambda evt: print("SPEECH_END_DETECTED: {}".format(evt)))
# event for intermediate results
intent_recognizer.recognizing.connect(lambda evt: print("RECOGNIZING: {}".format(evt)))
# event for final result
intent_recognizer.recognized.connect(lambda evt: print(
"RECOGNIZED: {}\n\tText: {} (Reason: {})\n\tIntent Id: {}\n\tIntent JSON: {}".format(
evt, evt.result.text, evt.result.reason, evt.result.intent_id, evt.result.intent_json)))
# cancellation event
intent_recognizer.canceled.connect(lambda evt: print("CANCELED: {} ({})".format(evt.cancellation_details, evt.reason)))
# stop continuous recognition on session stopped, end of speech or canceled events
intent_recognizer.session_stopped.connect(stop_cb)
intent_recognizer.speech_end_detected.connect(stop_cb)
intent_recognizer.canceled.connect(stop_cb)
# And finally run the intent recognizer. The output of the callbacks should be printed to the console.
intent_recognizer.start_continuous_recognition()
while not done:
time.sleep(.5)
# </IntentContinuousRecognitionWithFile>