Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
docs: add streaming detect intent with partial response sample (#414)
Browse files Browse the repository at this point in the history
* docs: add streaming detect intent with partial response sample

* linting
  • Loading branch information
nicain committed Jul 1, 2022
1 parent f27a585 commit 57a0e16
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .kokoro/test-samples-impl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export PYTHONUNBUFFERED=1
env | grep KOKORO

# Install nox
python3.6 -m pip install --upgrade --quiet nox
python3.10 -m pip install --upgrade --quiet nox

# Use secrets acessor service account to get secrets
if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then
Expand Down Expand Up @@ -76,7 +76,7 @@ for file in samples/**/requirements.txt; do
echo "------------------------------------------------------------"

# Use nox to execute the tests for the project.
python3.6 -m nox -s "$RUN_TESTS_SESSION"
python3.10 -m nox -s "$RUN_TESTS_SESSION"
EXIT=$?

# If this is a periodic build, send the test log to the FlakyBot.
Expand Down
123 changes: 123 additions & 0 deletions samples/snippets/streaming_detect_intent_partial_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright 2022, Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# [START dialogflow_cx_streaming_detect_intent_enable_partial_response]
import uuid

from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
from google.cloud.dialogflowcx_v3.types import audio_config
from google.cloud.dialogflowcx_v3.types import InputAudioConfig
from google.cloud.dialogflowcx_v3.types import session


def run_sample():
"""
TODO(developer): Modify these variables before running the sample.
"""
project_id = "YOUR-PROJECT-ID"
location = "YOUR-LOCATION-ID"
agent_id = "YOUR-AGENT-ID"
audio_file_name = "YOUR-AUDIO-FILE-PATH"
encoding = 'AUDIO_ENCODING_LINEAR_16'
sample_rate_hertz = 16000
language_code = 'en'

streaming_detect_intent_partial_response(
project_id,
location,
agent_id,
audio_file_name,
encoding,
sample_rate_hertz,
language_code,
)


def streaming_detect_intent_partial_response(
project_id,
location,
agent_id,
audio_file_name,
encoding,
sample_rate_hertz,
language_code,
):

client_options = None
if location != "global":
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
print(f"API Endpoint: {api_endpoint}\n")
client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)
session_id = str(uuid.uuid4())

session_path = session_client.session_path(
project=project_id,
location=location,
agent=agent_id,
session=session_id,
)

def request_generator():
audio_encoding = audio_config.AudioEncoding[encoding]
config = InputAudioConfig(
audio_encoding=audio_encoding,
sample_rate_hertz=sample_rate_hertz,
single_utterance=True,
)
audio_input = session.AudioInput(config=config)
query_input = session.QueryInput(audio=audio_input, language_code=language_code)
yield session.StreamingDetectIntentRequest(
session=session_path,
query_input=query_input,
enable_partial_response=True,
)
# Here we are reading small chunks of audio data from a local
# audio file. In practice these chunks should come from
# an audio input device.
with open(audio_file_name, "rb") as audio_file:
while True:
chunk = audio_file.read(4096)
if not chunk:
break
# The later requests contains audio data.
audio_input = session.AudioInput(audio=chunk, config=config)
query_input = session.QueryInput(audio=audio_input, language_code=language_code)
yield session.StreamingDetectIntentRequest(
session=session_path,
query_input=query_input,
enable_partial_response=True,
)

responses = session_client.streaming_detect_intent(
requests=request_generator()
)

print("=" * 20)
for response in responses:
print(f'Intermediate transcript: "{response.recognition_result.transcript}".')

# Note: The result from the last response is the final transcript along
# with the detected content.
response = response.detect_intent_response
print(f"Query text: {response.query_result.transcript}")
response_messages = [
" ".join(msg.text.text) for msg in response.query_result.response_messages
]
print(f"Response text: {' '.join(response_messages)}\n")
# [END dialogflow_cx_streaming_detect_intent_enable_partial_response]


if __name__ == "__main__":
run_sample()
47 changes: 47 additions & 0 deletions samples/snippets/streaming_detect_intent_partial_response_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2022, Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for detect_intent_texts."""

from __future__ import absolute_import

import os

from streaming_detect_intent_partial_response import streaming_detect_intent_partial_response


DIRNAME = os.path.realpath(os.path.dirname(__file__))
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
AGENT_ID = os.getenv("AGENT_ID")
AUDIO_PATH = os.getenv("AUDIO_PATH")
AUDIO = f"{DIRNAME}/{AUDIO_PATH}"


def test_streaming_detect_intent_partial_response(capsys):

encoding = 'AUDIO_ENCODING_LINEAR_16'
sample_rate_hertz = 24000

streaming_detect_intent_partial_response(
PROJECT_ID,
'global',
AGENT_ID,
AUDIO,
encoding,
sample_rate_hertz,
"en-US",
)
out, _ = capsys.readouterr()

assert "Intermediate transcript:" in out
assert "Response text: Hi! I'm the virtual flights agent." in out

0 comments on commit 57a0e16

Please sign in to comment.