tools/api_debugging.ipynb (609 lines of code) (raw):

{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Setup\n", "\n", "Go through this section before starting the tests." ], "metadata": { "id": "JuqRAEyb63n0" } }, { "cell_type": "code", "source": [ "#@title Install dependecy libraries\n", "\n", "#@markdown Run this block and you may need to restart the Colab runtime,\n", "#@markdown after which skip to the next block.\n", "\n", "!pip install --upgrade pip\n", "!pip install --upgrade google-cloud-dialogflow\n", "!pip install --upgrade google-cloud-dialogflow-cx\n", "!pip install --upgrade pydub" ], "metadata": { "id": "C7zSWp-HIJGq", "cellView": "form" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Common library { display-mode: \"form\" }\n", "\n", "from datetime import datetime\n", "from google.cloud import dialogflow_v2beta1\n", "from google.cloud import dialogflowcx_v3beta1\n", "from math import floor\n", "from pydub import AudioSegment\n", "from time import sleep\n", "\n", "\n", "def YieldAudioChunks(audio_file, sample_rate, sample_width, chunk_duration):\n", " clip = AudioSegment.from_raw(\n", " audio_file,\n", " frame_rate=sample_rate,\n", " sample_width=sample_width,\n", " channels=1)\n", " duration = len(clip)\n", " start_offset = 0\n", " while start_offset < duration:\n", " yield clip[start_offset:start_offset + chunk_duration].raw_data\n", " start_offset += chunk_duration\n", " sleep(chunk_duration / 1000)\n", "\n", "\n", "def CallUnaryAPI(method, request):\n", " print('[%s] Sending request:' % datetime.now())\n", " print(request)\n", " response = method(request=request)\n", " print('[%s] Received response:' % datetime.now())\n", " print(response)\n", "\n", "\n", "def CallStreamingAPI(method, request_generator):\n", " def send_requests():\n", " for request in request_generator():\n", " print('[%s] Sending request:' % datetime.now())\n", " print(request)\n", " yield request\n", "\n", " for response in method(requests=send_requests()):\n", " print('[%s] Received response:' % datetime.now())\n", " print(response)\n", "\n" ], "metadata": { "id": "8Z8Had0BrtS4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Authentication { display-mode: \"form\" }\n", "\n", "#@markdown Run this block to authenticate yourself.\n", "#\n", "#@markdown This will be the credential to call GCP API. Make sure you have the permission for accessing the resources of the project you're going to test.\n", "\n", "from google import auth as google_auth\n", "from google.colab import auth\n", "\n", "auth.authenticate_user()\n", "credentials, project_id = google_auth.default(\n", " scopes=['https://www.googleapis.com/auth/cloud-platform'])" ], "metadata": { "id": "AEHNDD5LCSIf" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Mount Google Drive { display-mode: \"form\" }\n", "\n", "#@markdown Mount your Google Drive to `/content/gdrive` folder. When this is done, find\n", "#@markdown your folder in Colab's Files tab to the left.\n", "\n", "from google.colab import drive\n", "drive.mount('/content/gdrive', force_remount=True)\n", "\n", "#@markdown Later, you may refer to your files in the Google Driver as testing\n", "#@markdown data." ], "metadata": { "id": "PISglEEIFcKC" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Gather Basic Info { display-mode: \"form\", run: \"auto\" }\n", "\n", "#@markdown GCP project you're going to test\n", "PROJECT_ID = 'tianzhu-test' #@param {type:\"string\"}\n", "#@markdown GCP location, e.g. 'global' or 'us-east1'\n", "LOCATION_ID = 'global' #@param {type:\"string\"}" ], "metadata": { "id": "0r-Tk3Gg_Ndt" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Test *DetectIntent\n", "\n", "This API does not require creating up the resources for the session: simply specify a unique session ID and start the talk with the virtual agent." ], "metadata": { "id": "xAANqhpN8JaC" } }, { "cell_type": "code", "source": [ "#@title DetectIntent { display-mode: \"form\" }\n", "\n", "#@markdown Dialogflow version, ES or CX.\n", "DF_VERSION = 'CX' #@param [\"ES\", \"CX\"]\n", "#@markdown Agent ID (CX only)\n", "AGENT_ID = '38c93a4f-37cd-4123-a75d-e0ad78120c97' #@param { type: \"string\" }\n", "#@markdown Environment ID\n", "ENVIRONMENT_ID = 'draft' #@param { type: \"string\" }\n", "#@markdown User ID (ES only)\n", "USER_ID = '-' #@param { type: \"string\" }\n", "#@markdown Session ID, may use any one unique for the purpose of testing.\n", "SESSION_ID = 'test' #@param { type: \"string\" }\n", "#@markdown Input type, text or audio.\n", "INPUT_TYPE = 'AUDIO' #@param [\"TEXT\", \"AUDIO\"]\n", "#@markdown Language of the input, e.g. 'en-us'\n", "LANGUAGE_CODE = 'en-us' #@param { type: \"string\" }\n", "#@markdown Input, the text query or the file of audio clip\n", "INPUT = '/content/gdrive/MyDrive/hello.raw' #@param { type: \"string\" }\n", "#@markdown audio format, MULAW or LINEAR16\n", "AUDIO_FORMAT = 'LINEAR16' #@param [\"MULAW\", \"LINEAR16\"]\n", "#@markdown Sample rate, e.g. 16000\n", "SAMPLE_RATE = 8000 #@param { type: \"integer\" }\n", "#@markdown Speech model, e.g. 'phone_call'\n", "SPEECH_MODEL = 'phone_call' #@param { type: \"string\" }\n", "#@markdown Whether to use enhanced speech model. (Must enable speech logging in\n", "#@markdown the agent settings.)\n", "USE_ENHANCED_SPEECH_MODEL = True #@param { type: \"boolean\" }\n", "\n", "\n", "if DF_VERSION == 'ES':\n", " client = dialogflow_v2beta1.SessionsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", " session=(\n", " 'projects/%s/locations/%s/agent/environments/%s/users/%s/sessions/%s' %\n", " (PROJECT_ID, LOCATION_ID, ENVIRONMENT_ID, USER_ID, SESSION_ID))\n", "\n", " if INPUT_TYPE == 'TEXT':\n", " request = dialogflow_v2beta1.DetectIntentRequest(\n", " session=session,\n", " query_input = dialogflow_v2beta1.QueryInput(\n", " text=dialogflow_v2beta1.TextInput(\n", " language_code=LANGUAGE_CODE,\n", " text=INPUT)))\n", " else:\n", " request = dialogflow_v2beta1.DetectIntentRequest(\n", " session=session,\n", " query_input=dialogflow_v2beta1.QueryInput(\n", " audio_config=dialogflow_v2beta1.InputAudioConfig(\n", " audio_encoding=(\n", " dialogflow_v2beta1.AudioEncoding.AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflow_v2beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " language_code=LANGUAGE_CODE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflow_v2beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflow_v2beta1.SpeechModelVariant.USE_STANDARD))),\n", " input_audio=open(INPUT, 'rb').read())\n", "\n", "else:\n", " client = dialogflowcx_v3beta1.SessionsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", " session='projects/%s/locations/%s/agents/%s/environments/%s/sessions/%s' % (\n", " PROJECT_ID, LOCATION_ID, AGENT_ID, ENVIRONMENT_ID, SESSION_ID)\n", "\n", " if INPUT_TYPE == 'TEXT':\n", " request = dialogflowcx_v3beta1.DetectIntentRequest(\n", " session=session,\n", " query_input = dialogflowcx_v3beta1.QueryInput(\n", " text=dialogflowcx_v3beta1.TextInput(text=INPUT),\n", " language_code=LANGUAGE_CODE))\n", " else:\n", " request = dialogflowcx_v3beta1.DetectIntentRequest(\n", " session=session,\n", " query_input=dialogflowcx_v3beta1.QueryInput(\n", " audio=dialogflowcx_v3beta1.AudioInput(\n", " config=dialogflowcx_v3beta1.InputAudioConfig(\n", " audio_encoding=(\n", " dialogflowcx_v3beta1.AudioEncoding\n", " .AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflowcx_v3beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflowcx_v3beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflowcx_v3beta1.SpeechModelVariant\n", " .USE_STANDARD)),\n", " audio=open(INPUT, 'rb').read()),\n", " language_code=LANGUAGE_CODE))\n", "\n", "CallUnaryAPI(client.detect_intent, request)\n" ], "metadata": { "id": "gBgIK9Qcegmp" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title StreamingDetectIntent { display-mode: \"form\" }\n", "\n", "#@markdown Dialogflow version, ES or CX.\n", "DF_VERSION = 'CX' #@param [\"ES\", \"CX\"]\n", "#@markdown Agent ID (CX only)\n", "AGENT_ID = '38c93a4f-37cd-4123-a75d-e0ad78120c97' #@param { type: \"string\" }\n", "#@markdown Environment ID\n", "ENVIRONMENT_ID = 'draft' #@param { type: \"string\" }\n", "#@markdown User ID (ES only)\n", "USER_ID = '-' #@param { type: \"string\" }\n", "#@markdown Session ID, may use any one unique for the purpose of testing.\n", "SESSION_ID = 'test' #@param { type: \"string\" }\n", "#@markdown Input type, text or audio.\n", "INPUT_TYPE = 'TEXT' #@param [\"TEXT\", \"AUDIO\"]\n", "#@markdown Language of the input, e.g. 'en-us'\n", "LANGUAGE_CODE = 'en-us' #@param { type: \"string\" }\n", "#@markdown Input, the text query or the file of audio clip\n", "INPUT = '/content/gdrive/MyDrive/hello.raw' #@param { type: \"string\" }\n", "#@markdown audio format, MULAW or LINEAR16\n", "AUDIO_FORMAT = 'LINEAR16' #@param [\"MULAW\", \"LINEAR16\"]\n", "#@markdown Sample rate, e.g. 16000\n", "SAMPLE_RATE = 8000 #@param { type: \"integer\" }\n", "#@markdown Speech model, e.g. 'phone_call'\n", "SPEECH_MODEL = 'phone_call' #@param { type: \"string\" }\n", "#@markdown Whether to use enhanced speech model. (Must enable speech logging in\n", "#@markdown the agent settings.)\n", "USE_ENHANCED_SPEECH_MODEL = True #@param { type: \"boolean\" }\n", "#@markdown Whether to use single utterance mode\n", "USE_SINGLE_UTTERANCE_MODE = True #@param { type: \"boolean\" }\n", "#@markdown Audio chunk duration in ms.\n", "CHUNK_DURATION = 100 #@param { type: \"integer\" }\n", "\n", "\n", "if DF_VERSION == 'ES':\n", " client = dialogflow_v2beta1.SessionsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", " session=(\n", " 'projects/%s/locations/%s/agent/environments/%s/users/%s/sessions/%s' %\n", " (PROJECT_ID, LOCATION_ID, ENVIRONMENT_ID, USER_ID, SESSION_ID))\n", "\n", " if INPUT_TYPE == 'TEXT':\n", " def yield_requests():\n", " yield dialogflow_v2beta1.StreamingDetectIntentRequest(\n", " session=session,\n", " query_input = dialogflow_v2beta1.QueryInput(\n", " text=dialogflow_v2beta1.TextInput(\n", " language_code=LANGUAGE_CODE,\n", " text=INPUT)))\n", " else:\n", " def yield_requests():\n", " yield dialogflow_v2beta1.StreamingDetectIntentRequest(\n", " session=session,\n", " query_input=dialogflow_v2beta1.QueryInput(\n", " audio_config=dialogflow_v2beta1.InputAudioConfig(\n", " audio_encoding=(\n", " dialogflow_v2beta1.AudioEncoding.AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflow_v2beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " language_code=LANGUAGE_CODE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflow_v2beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflow_v2beta1.SpeechModelVariant\n", " .USE_STANDARD),\n", " single_utterance=USE_SINGLE_UTTERANCE_MODE)))\n", "\n", " for chunk in YieldAudioChunks(\n", " open(INPUT, 'rb'),\n", " sample_rate=SAMPLE_RATE,\n", " sample_width=1 if AUDIO_FORMAT == 'MULAW' else 2,\n", " chunk_duration=CHUNK_DURATION):\n", " yield dialogflow_v2beta1.StreamingDetectIntentRequest(\n", " input_audio=chunk)\n", "\n", "else:\n", " client = dialogflowcx_v3beta1.SessionsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", " session='projects/%s/locations/%s/agents/%s/environments/%s/sessions/%s' % (\n", " PROJECT_ID, LOCATION_ID, AGENT_ID, ENVIRONMENT_ID, SESSION_ID)\n", "\n", " if INPUT_TYPE == 'TEXT':\n", " def yield_requests():\n", " yield dialogflowcx_v3beta1.StreamingDetectIntentRequest(\n", " session=session,\n", " query_input = dialogflowcx_v3beta1.QueryInput(\n", " text=dialogflowcx_v3beta1.TextInput(text=INPUT),\n", " language_code=LANGUAGE_CODE))\n", " else:\n", " def yield_requests():\n", " yield dialogflowcx_v3beta1.StreamingDetectIntentRequest(\n", " session=session,\n", " query_input=dialogflowcx_v3beta1.QueryInput(\n", " audio=dialogflowcx_v3beta1.AudioInput(\n", " config=dialogflowcx_v3beta1.InputAudioConfig(\n", " audio_encoding=(\n", " dialogflowcx_v3beta1.AudioEncoding\n", " .AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflowcx_v3beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflowcx_v3beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflowcx_v3beta1.SpeechModelVariant\n", " .USE_STANDARD))),\n", " language_code=LANGUAGE_CODE))\n", "\n", " for chunk in YieldAudioChunks(\n", " open(INPUT, 'rb'),\n", " sample_rate=SAMPLE_RATE,\n", " sample_width=1 if AUDIO_FORMAT == 'MULAW' else 2,\n", " chunk_duration=CHUNK_DURATION):\n", " yield dialogflowcx_v3beta1.StreamingDetectIntentRequest(\n", " query_input=dialogflowcx_v3beta1.QueryInput(\n", " audio=dialogflowcx_v3beta1.AudioInput(audio=chunk),\n", " language_code=LANGUAGE_CODE))\n", "\n", "CallStreamingAPI(client.streaming_detect_intent, yield_requests)" ], "metadata": { "id": "RLbezTEElVvv" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Test *AnalyzeContent\n", "\n", "To use this API, we first have to set up Conversation and Participant resources. Then call the API on the created participant resource." ], "metadata": { "id": "zlPZc0AK1CFR" } }, { "cell_type": "code", "source": [ "#@title Set up a new conversation { display-mode: \"form\" }\n", "\n", "#@markdown Dialogflow conversation profile to use to create the conversation\n", "CONVERSATION_PROFILE_ID = '2rjtTcLFRAmqCiNDh5gQzQ' #@param {type:\"string\"}\n", "\n", "conversations_client = dialogflow_v2beta1.ConversationsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", "conversation_name = conversations_client.create_conversation(\n", " request=dialogflow_v2beta1.CreateConversationRequest(\n", " parent='projects/%s/locations/%s' % (PROJECT_ID, LOCATION_ID),\n", " conversation=dialogflow_v2beta1.Conversation(\n", " conversation_profile=\n", " 'projects/%s/locations/%s/conversationProfiles/%s' %\n", " (PROJECT_ID, LOCATION_ID, CONVERSATION_PROFILE_ID)))).name\n", "participants_client = dialogflow_v2beta1.ParticipantsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", "participant_name = participants_client.create_participant(\n", " request=dialogflow_v2beta1.CreateParticipantRequest(\n", " parent=conversation_name,\n", " participant=dialogflow_v2beta1.Participant(\n", " role=dialogflow_v2beta1.Participant.Role.END_USER))).name\n", "\n", "print('Created conversation: %s' % conversation_name)\n", "print('Created participant: %s' % participant_name)\n", "\n" ], "metadata": { "id": "Sb0RyAla1VEv" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title AnalyzeContent { display-mode: \"form\" }\n", "\n", "#@markdown Input type, text or audio.\n", "INPUT_TYPE = 'TEXT' #@param [\"TEXT\", \"AUDIO\", \"EVENT\"]\n", "#@markdown Language of the input, e.g. 'en-us'\n", "LANGUAGE_CODE = 'en-us' #@param { type: \"string\" }\n", "#@markdown Input, the text query or the file of audio clip\n", "INPUT = '/content/gdrive/MyDrive/hello.raw' #@param { type: \"string\" }\n", "#@markdown audio format, MULAW or LINEAR16\n", "AUDIO_FORMAT = 'LINEAR16' #@param [\"MULAW\", \"LINEAR16\"]\n", "#@markdown Sample rate, e.g. 16000\n", "SAMPLE_RATE = 8000 #@param { type: \"integer\" }\n", "#@markdown Speech model, e.g. 'phone_call'\n", "SPEECH_MODEL = 'phone_call' #@param { type: \"string\" }\n", "#@markdown Whether to use enhanced speech model.\n", "USE_ENHANCED_SPEECH_MODEL = True #@param { type: \"boolean\" }\n", "\n", "client = dialogflow_v2beta1.ParticipantsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", "\n", "if INPUT_TYPE == 'TEXT':\n", " request = dialogflow_v2beta1.AnalyzeContentRequest(\n", " participant=participant_name,\n", " text_input=dialogflow_v2beta1.TextInput(\n", " text=INPUT, language_code=LANGUAGE_CODE))\n", "elif INPUT_TYPE == 'EVENT':\n", " request = dialogflow_v2beta1.AnalyzeContentRequest(\n", " participant=participant_name,\n", " event_input=dialogflow_v2beta1.EventInput(\n", " name=INPUT, language_code=LANGUAGE_CODE))\n", "else:\n", " request = dialogflow_v2beta1.AnalyzeContentRequest(\n", " participant=participant_name,\n", " audio_input=dialogflow_v2beta1.AudioInput(\n", " config=dialogflow_v2beta1.InputAudioConfig(\n", " language_code=LANGUAGE_CODE,\n", " audio_encoding=(\n", " dialogflow_v2beta1.AudioEncoding.AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflow_v2beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflow_v2beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflow_v2beta1.SpeechModelVariant.USE_STANDARD)),\n", " audio=open(INPUT, 'rb').read()))\n", "\n", "CallUnaryAPI(client.analyze_content, request)" ], "metadata": { "id": "iiy9PIjT9smg" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title StreamingAnalyzeContent { display-mode: \"form\" }\n", "\n", "#@markdown Input type, text or audio.\n", "INPUT_TYPE = 'TEXT' #@param [\"TEXT\", \"AUDIO\"]\n", "#@markdown Language of the input, e.g. 'en-us'\n", "LANGUAGE_CODE = 'en-us' #@param { type: \"string\" }\n", "#@markdown Input, the text query or the file of audio clip\n", "INPUT = '/content/gdrive/MyDrive/hello.raw' #@param { type: \"string\" }\n", "#@markdown audio format, MULAW or LINEAR16\n", "AUDIO_FORMAT = 'LINEAR16' #@param [\"MULAW\", \"LINEAR16\"]\n", "#@markdown Sample rate, e.g. 16000\n", "SAMPLE_RATE = 8000 #@param { type: \"integer\" }\n", "#@markdown Speech model, e.g. 'phone_call'\n", "SPEECH_MODEL = 'phone_call' #@param { type: \"string\" }\n", "#@markdown Whether to use enhanced speech model.\n", "USE_ENHANCED_SPEECH_MODEL = True #@param { type: \"boolean\" }\n", "#@markdown Whether to use single utterance mode\n", "USE_SINGLE_UTTERANCE_MODE = True #@param { type: \"boolean\" }\n", "#@markdown Audio chunk duration in ms.\n", "CHUNK_DURATION = 100 #@param { type: \"integer\" }\n", "\n", "client = dialogflow_v2beta1.ParticipantsClient(\n", " credentials=credentials,\n", " client_options={\n", " 'quota_project_id': PROJECT_ID\n", " })\n", "\n", "if INPUT_TYPE == 'TEXT':\n", " def yield_requests():\n", " yield dialogflow_v2beta1.StreamingAnalyzeContentRequest(\n", " participant=participant_name,\n", " text_config=dialogflow_v2beta1.InputTextConfig(\n", " language_code=LANGUAGE_CODE))\n", " yield dialogflow_v2beta1.StreamingAnalyzeContentRequest(input_text=INPUT)\n", "else:\n", " def yield_requests():\n", " yield dialogflow_v2beta1.StreamingAnalyzeContentRequest(\n", " participant=participant_name,\n", " audio_config=dialogflow_v2beta1.InputAudioConfig(\n", " language_code=LANGUAGE_CODE,\n", " audio_encoding=(\n", " dialogflow_v2beta1.AudioEncoding.AUDIO_ENCODING_MULAW\n", " if AUDIO_FORMAT == 'MULAW'\n", " else dialogflow_v2beta1.AudioEncoding\n", " .AUDIO_ENCODING_LINEAR_16),\n", " sample_rate_hertz=SAMPLE_RATE,\n", " model=SPEECH_MODEL,\n", " model_variant=(\n", " dialogflow_v2beta1.SpeechModelVariant.USE_ENHANCED\n", " if USE_ENHANCED_SPEECH_MODEL\n", " else dialogflow_v2beta1.SpeechModelVariant.USE_STANDARD),\n", " single_utterance=USE_SINGLE_UTTERANCE_MODE))\n", "\n", " for chunk in YieldAudioChunks(\n", " open(INPUT, 'rb'),\n", " sample_rate=SAMPLE_RATE,\n", " sample_width=1 if AUDIO_FORMAT == 'MULAW' else 2,\n", " chunk_duration=CHUNK_DURATION):\n", " yield dialogflow_v2beta1.StreamingAnalyzeContentRequest(input_audio=chunk)\n", "\n", "CallStreamingAPI(client.streaming_analyze_content, yield_requests)" ], "metadata": { "id": "Fy24-KZvDlN0" }, "execution_count": null, "outputs": [] } ] }