|
| 1 | +# Copyright 2022, Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | + |
| 15 | +# [START dialogflow_cx_streaming_detect_intent_enable_partial_response] |
| 16 | +import uuid |
| 17 | + |
| 18 | +from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient |
| 19 | +from google.cloud.dialogflowcx_v3.types import audio_config |
| 20 | +from google.cloud.dialogflowcx_v3.types import InputAudioConfig |
| 21 | +from google.cloud.dialogflowcx_v3.types import session |
| 22 | + |
| 23 | + |
| 24 | +def run_sample(): |
| 25 | + """ |
| 26 | + TODO(developer): Modify these variables before running the sample. |
| 27 | + """ |
| 28 | + project_id = "YOUR-PROJECT-ID" |
| 29 | + location = "YOUR-LOCATION-ID" |
| 30 | + agent_id = "YOUR-AGENT-ID" |
| 31 | + audio_file_name = "YOUR-AUDIO-FILE-PATH" |
| 32 | + encoding = 'AUDIO_ENCODING_LINEAR_16' |
| 33 | + sample_rate_hertz = 16000 |
| 34 | + language_code = 'en' |
| 35 | + |
| 36 | + streaming_detect_intent_partial_response( |
| 37 | + project_id, |
| 38 | + location, |
| 39 | + agent_id, |
| 40 | + audio_file_name, |
| 41 | + encoding, |
| 42 | + sample_rate_hertz, |
| 43 | + language_code, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def streaming_detect_intent_partial_response( |
| 48 | + project_id, |
| 49 | + location, |
| 50 | + agent_id, |
| 51 | + audio_file_name, |
| 52 | + encoding, |
| 53 | + sample_rate_hertz, |
| 54 | + language_code, |
| 55 | +): |
| 56 | + |
| 57 | + client_options = None |
| 58 | + if location != "global": |
| 59 | + api_endpoint = f"{location}-dialogflow.googleapis.com:443" |
| 60 | + print(f"API Endpoint: {api_endpoint}\n") |
| 61 | + client_options = {"api_endpoint": api_endpoint} |
| 62 | + session_client = SessionsClient(client_options=client_options) |
| 63 | + session_id = str(uuid.uuid4()) |
| 64 | + |
| 65 | + session_path = session_client.session_path( |
| 66 | + project=project_id, |
| 67 | + location=location, |
| 68 | + agent=agent_id, |
| 69 | + session=session_id, |
| 70 | + ) |
| 71 | + |
| 72 | + def request_generator(): |
| 73 | + audio_encoding = audio_config.AudioEncoding[encoding] |
| 74 | + config = InputAudioConfig( |
| 75 | + audio_encoding=audio_encoding, |
| 76 | + sample_rate_hertz=sample_rate_hertz, |
| 77 | + single_utterance=True, |
| 78 | + ) |
| 79 | + audio_input = session.AudioInput(config=config) |
| 80 | + query_input = session.QueryInput(audio=audio_input, language_code=language_code) |
| 81 | + yield session.StreamingDetectIntentRequest( |
| 82 | + session=session_path, |
| 83 | + query_input=query_input, |
| 84 | + enable_partial_response=True, |
| 85 | + ) |
| 86 | + # Here we are reading small chunks of audio data from a local |
| 87 | + # audio file. In practice these chunks should come from |
| 88 | + # an audio input device. |
| 89 | + with open(audio_file_name, "rb") as audio_file: |
| 90 | + while True: |
| 91 | + chunk = audio_file.read(4096) |
| 92 | + if not chunk: |
| 93 | + break |
| 94 | + # The later requests contains audio data. |
| 95 | + audio_input = session.AudioInput(audio=chunk, config=config) |
| 96 | + query_input = session.QueryInput(audio=audio_input, language_code=language_code) |
| 97 | + yield session.StreamingDetectIntentRequest( |
| 98 | + session=session_path, |
| 99 | + query_input=query_input, |
| 100 | + enable_partial_response=True, |
| 101 | + ) |
| 102 | + |
| 103 | + responses = session_client.streaming_detect_intent( |
| 104 | + requests=request_generator() |
| 105 | + ) |
| 106 | + |
| 107 | + print("=" * 20) |
| 108 | + for response in responses: |
| 109 | + print(f'Intermediate transcript: "{response.recognition_result.transcript}".') |
| 110 | + |
| 111 | + # Note: The result from the last response is the final transcript along |
| 112 | + # with the detected content. |
| 113 | + response = response.detect_intent_response |
| 114 | + print(f"Query text: {response.query_result.transcript}") |
| 115 | + response_messages = [ |
| 116 | + " ".join(msg.text.text) for msg in response.query_result.response_messages |
| 117 | + ] |
| 118 | + print(f"Response text: {' '.join(response_messages)}\n") |
| 119 | +# [END dialogflow_cx_streaming_detect_intent_enable_partial_response] |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + run_sample() |
0 commit comments