Skip to content

Commit bf7511c

Browse files
authored
docs: add streaming detect intent with partial response sample (#414)
* docs: add streaming detect intent with partial response sample * linting
1 parent 5695e6e commit bf7511c

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"""Tests for detect_intent_texts."""
15+
16+
from __future__ import absolute_import
17+
18+
import os
19+
20+
from streaming_detect_intent_partial_response import streaming_detect_intent_partial_response
21+
22+
23+
DIRNAME = os.path.realpath(os.path.dirname(__file__))
24+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
25+
AGENT_ID = os.getenv("AGENT_ID")
26+
AUDIO_PATH = os.getenv("AUDIO_PATH")
27+
AUDIO = f"{DIRNAME}/{AUDIO_PATH}"
28+
29+
30+
def test_streaming_detect_intent_partial_response(capsys):
31+
32+
encoding = 'AUDIO_ENCODING_LINEAR_16'
33+
sample_rate_hertz = 24000
34+
35+
streaming_detect_intent_partial_response(
36+
PROJECT_ID,
37+
'global',
38+
AGENT_ID,
39+
AUDIO,
40+
encoding,
41+
sample_rate_hertz,
42+
"en-US",
43+
)
44+
out, _ = capsys.readouterr()
45+
46+
assert "Intermediate transcript:" in out
47+
assert "Response text: Hi! I'm the virtual flights agent." in out

0 commit comments

Comments
 (0)