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

Commit ac6aae4

Browse files
authored
docs: add detect intent with text-to-speech synthesized output snippet (#419)
chore: Update test-samples-impl.sh python3.6 --> python3.9 (#418) Source-Link: googleapis/synthtool@1f07110 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:a5d81b61dfd1a432d3c03f51a25d2e71b37be24da509966d50724aea7c57c5c2 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: nicain <[email protected]> fix test
1 parent 41fd1d3 commit ac6aae4

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Detects intent and returns a synthesized Text-to-Speech (TTS) response
18+
19+
# [START dialogflow_cx_v3_detect_intent_synthesize_tts_response_async]
20+
import uuid
21+
22+
from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
23+
from google.cloud.dialogflowcx_v3.types import audio_config
24+
from google.cloud.dialogflowcx_v3.types import session
25+
26+
27+
def run_sample():
28+
# TODO(developer): Update these values when running the function
29+
project_id = "YOUR-PROJECT-ID"
30+
location = "YOUR-LOCATION-ID"
31+
agent_id = "YOUR-AGENT-ID"
32+
text = "YOUR-TEXT"
33+
audio_encoding = "YOUR-AUDIO-ENCODING"
34+
language_code = "YOUR-LANGUAGE-CODE"
35+
output_file = "YOUR-OUTPUT-FILE"
36+
37+
detect_intent_synthesize_tts_response(
38+
project_id,
39+
location,
40+
agent_id,
41+
text,
42+
audio_encoding,
43+
language_code,
44+
output_file,
45+
)
46+
47+
48+
def detect_intent_synthesize_tts_response(
49+
project_id,
50+
location,
51+
agent_id,
52+
text,
53+
audio_encoding,
54+
language_code,
55+
output_file,
56+
):
57+
"""Returns the result of detect intent with synthesized response."""
58+
client_options = None
59+
if location != "global":
60+
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
61+
print(f"API Endpoint: {api_endpoint}\n")
62+
client_options = {"api_endpoint": api_endpoint}
63+
session_client = SessionsClient(client_options=client_options)
64+
session_id = str(uuid.uuid4())
65+
66+
# Constructs the audio query request
67+
session_path = session_client.session_path(
68+
project=project_id,
69+
location=location,
70+
agent=agent_id,
71+
session=session_id,
72+
)
73+
text_input = session.TextInput(text=text)
74+
query_input = session.QueryInput(
75+
text=text_input,
76+
language_code=language_code
77+
)
78+
synthesize_speech_config = audio_config.SynthesizeSpeechConfig(
79+
speaking_rate=1.25,
80+
pitch=10.0,
81+
)
82+
output_audio_config = audio_config.OutputAudioConfig(
83+
synthesize_speech_config=synthesize_speech_config,
84+
audio_encoding=audio_config.OutputAudioEncoding[
85+
audio_encoding],
86+
)
87+
request = session.DetectIntentRequest(
88+
session=session_path,
89+
query_input=query_input,
90+
output_audio_config=output_audio_config,
91+
)
92+
93+
response = session_client.detect_intent(request=request)
94+
print(
95+
'Speaking Rate: '
96+
f'{response.output_audio_config.synthesize_speech_config.speaking_rate}')
97+
print(
98+
'Pitch: '
99+
f'{response.output_audio_config.synthesize_speech_config.pitch}')
100+
with open(output_file, 'wb') as fout:
101+
fout.write(response.output_audio)
102+
print(f'Audio content written to file: {output_file}')
103+
# [END dialogflow_cx_v3_detect_intent_synthesize_tts_response_async]
104+
105+
106+
if __name__ == "__main__":
107+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_with_sentiment_analysis.py"""
15+
16+
from __future__ import absolute_import
17+
18+
import os
19+
20+
from detect_intent_synthesize_tts_response import detect_intent_synthesize_tts_response
21+
22+
23+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
24+
AGENT_ID = os.getenv("AGENT_ID")
25+
26+
27+
def test_detect_intent_positive(capsys, tmp_path_factory):
28+
29+
output_file = tmp_path_factory.mktemp("data") / "tmp.wav"
30+
31+
detect_intent_synthesize_tts_response(
32+
PROJECT_ID,
33+
'global',
34+
AGENT_ID,
35+
"Perfect!",
36+
'OUTPUT_AUDIO_ENCODING_LINEAR_16',
37+
"en-us",
38+
output_file,
39+
)
40+
out, _ = capsys.readouterr()
41+
assert f'Audio content written to file: {output_file}' in out

0 commit comments

Comments
 (0)