我正在使用flask部署我的chatbot深度学习模型。当我在python控制台上本地运行时,该模型将运行良好。但是,当我尝试使用Flask进行部署时,出现此异常:
Traceback (most recent call last): File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response) File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e) File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb) File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request() File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e) File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb) File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request() File "C:\Users\tab\AppData\Local\Programs\Python\Python35\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args) File "C:\Chatbot-Flask-Server-master\Chatbot-Flask-Server-master\app-.py", line 60, in prediction
response = pred(str(request.json['message'])) TypeError: 'NoneType' object is not subscriptable
这是我的代码:
# webapp
app = Flask(__name__, template_folder='./')
@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
response = pred(str(request.json['message']))
return jsonify(response)
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
答案 0 :(得分:0)
尝试一下,看看该打印语句是否打印出任何内容。
import json
app = Flask(__name__, template_folder='./')
@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
text = str(request.data)
print(request.data) #To check what you are sending
text = json.loads(text)
response = pred(str(text['message']))
return jsonify(response)
@app.route('/')
def main():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)