Search code examples
pythongoogle-apidialogflow-es

dialogflow - ImportError: cannot import name 'AgentsClient'


I'm a noob with dialogue flow. Here is the audio-to-text code that I'm using. I googled the error but couldn't find a solution. Any help is appreciated. Thank you!

import os
import dialogflow_v2 as dialogflow
from dialogflow_v2 import AgentsClient
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"D:\Resume building\gcloudtstuff24Jan2020\testing-302710-cc7fef4033ff.json"
project_id = '*******'
session_id = "ayesha"
audio_file_path = r'C:\Users\ayesha\Downloads\sample - mp3 inserted.wav'
language_code = 'en'
def detect_intent_audio(project_id, session_id, audio_file_path, language_code):
    """Returns the result of detect intent with an audio file as input.

    Using the same `session_id` between requests allows continuation
    of the conversation."""
    from google.cloud import dialogflow

    session_client = dialogflow.SessionsClient()

    # Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
    audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16
    sample_rate_hertz = 16000

    session = session_client.session_path(project_id, session_id)
    print("Session path: {}\n".format(session))

    with open(audio_file_path, "rb") as audio_file:
        input_audio = audio_file.read()

    audio_config = dialogflow.InputAudioConfig(
        audio_encoding=audio_encoding,
        language_code=language_code,
        sample_rate_hertz=sample_rate_hertz,
    )
    query_input = dialogflow.QueryInput(audio_config=audio_config)

    request = dialogflow.DetectIntentRequest(
        session=session,
        query_input=query_input,
        input_audio=input_audio,
    )
    response = session_client.detect_intent(request=request)

    print("=" * 20)
    print("Query text: {}".format(response.query_result.query_text))
    print(
        "Detected intent: {} (confidence: {})\n".format(
            response.query_result.intent.display_name,
            response.query_result.intent_detection_confidence,
        )
    )
    print("Fulfillment text: {}\n".format(response.query_result.fulfillment_text))
    
detect_intent_audio(project_id, session_id, audio_file_path, language_code)    dialogflow services.

And I am getting

ImportError: cannot import name 'AgentsClient'


Solution

  • I was creating my json file directly from the project I already have in my google cloud. I created a new project in the dialogue flow and it worked.