类型错误:使用 FastText 模型时'NoneType'对象在 Flask 中不可下标



我是烧瓶新手。我想用FastText做语言检测。然后把它包在烧瓶里。

language_predict.py

import fasttext
class Language:
def __init__(self):
model = "lid.176.bin"
self.model = fasttext.load_model(model)
def predict_lang(self, text):
predictions = self.model.predict(text, k=1)
return predictions

应用程序

from flask import Flask, request
from language_predict import Language
app = Flask(__name__)
LANGUAGE = Language()
@app.route('/detect', methods=['POST'])
def add_message():
content = request.json
lang = LANGUAGE.predict_lang(content['text'])
return {"lang":lang[0][0], "score": lang[1][0]}
if __name__ == '__main__':
app.run(host= '127.0.0.1', port= 5000)

当我运行app.py并在cmd:中发送请求时

curl -X POST http://127.0.0.1:5000/detect_language -d "{"text":"hello"}" -H 'Content-Type: application/json'

它具有TypeError:"NoneType"对象不是可下标的

我该怎么解决?

这是堆栈跟踪

File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflaskapp.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflaskapp.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflaskapp.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflask_compat.py", line 39, in reraise
raise value
File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflaskapp.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:UsersAppDataLocalProgramsPythonPython39libsite-packagesflaskapp.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersDesktopPythonlanguage-identification-masterlanguage-identification-masterapp.py", line 16, in add_message
lang = LANGUAGE.predict_lang(content['text'])
TypeError: 'NoneType' object is not subscriptable
127.0.0.1 - - [07/Dec/2020 18:07:58] "POST /detect_language HTTP/1.1" 500 -

request.json()不是request.json.json不是属性,即content具有值NoneType

最新更新