我一直在研究以下教程,以构建一个简单的烧瓶聊天机器人。
https://pusher.com/tutorials/chatbot-flask-dialogflow
我的Google凭据存储在.env中:
DIALOGFLOW_PROJECT_ID =电影机器人-twersf GOOGLE_APPLICATION_CREDENTIALS =电影机器人-*。json
但是,当我在Mac上本地运行该应用程序时,出现以下错误:
DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.
代码
from flask import Flask, request, jsonify, render_template
import os
import dialogflow
import requests
import json
def detect_intent_texts(project_id, session_id, text, language_code):
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
if text:
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
return response.query_result.fulfillment_text
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
# run Flask app
if __name__ == "__main__":
app.run()
@app.route('/get_movie_detail', methods=['POST'])
def get_movie_detail():
data = request.get_json(silent=True)
movie = data['queryResult']['parameters']['movie']
api_key = os.getenv('OMDB_API_KEY')
movie_detail = requests.get('http://www.omdbapi.com/?t={0}&apikey={1}'.format(movie, api_key)).content
movie_detail = json.loads(movie_detail)
response = """
Title : {0}
Released: {1}
Actors: {2}
Plot: {3}
""".format(movie_detail['Title'], movie_detail['Released'], movie_detail['Actors'], movie_detail['Plot'])
reply = {
"fulfillmentText": response,
}
return jsonify(reply)
@app.route('/send_message', methods=['POST'])
def send_message():
message = request.form['message']
project_id = os.getenv('DIALOGFLOW_PROJECT_ID')
fulfillment_text = detect_intent_texts(project_id, "unique", message, 'en')
response_text = { "message": fulfillment_text }
return jsonify(response_text)
我最初是从Spyder IDE运行该应用程序的,在研究了Stack Overflow上的其他线程之后,我认为问题可能出在IDE,因为从IDE运行的终端显然会丢失env变量。
因此,我从命令行运行,但是再次出现相同的错误。
有人可以帮忙吗?