|
| 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() |
0 commit comments