[英]expected an indented block in python with flask
当我尝试从训练数据中获取响应时出现此错误,但我不确定为什么:
“预期缩进块(第 55 行)pylint(语法错误)”
这是代码
app = Flask(__name__);
CORS(app)
@app.route("/bot", methods=["POST"])
#response
def response():
query = dict(request.form)['query']
# res = query + " "
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
# print(f"{bot_name}: {random.choice(intent['responses'])}")
return jsonify({"response" : random.choice(intent['responses'])}) #HERE IS THE ERROR
else:
return jsonify({"response" : "I do not understand..."})
if __name__=="__main__":
app.run(host="0.0.0.0",)
随机选择的想法是得到一个包含我之前定义的一些特征的响应......
因此,您创建了一个 if 条件并且没有在其中编写任何语句。 这就是它导致错误的原因。 行return jsonify({"response": random.choice(intent['responses'])})
应该在 if 条件内if tag == intent["tag"]
。
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
# print(f"{bot_name}: {random.choice(intent['responses'])}")
return jsonify({"response": random.choice(intent['responses'])}) # HERE IS THE ERROR
else:
return jsonify({"response": "I do not understand..."})
问题似乎出在这一行,您能否检查一下该块中的缩进是否合适
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.