-2

I am using flask to deploy my chatbot deep learning model. The model will run well when I run locally on python console. However when I try to deploy with flask, I get this exception:

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

Here is my code:

# 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)
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
ganz
  • 7
  • 1
  • 6
  • 1
    `request.json` is `None`, so `request.json['message']` fails. You want to test first: `if request.json:` to ensure it's not an empty value! – Martijn Pieters Aug 12 '18 at 16:21
  • You're probably dealing with [this issue](https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask). Use `request.get_json()` instead, or be specific in the request. – Abdou Aug 12 '18 at 16:21
  • @Abdou `request.get_json()` give `TypeError: 'method' object is not subscriptable` – ganz Aug 12 '18 at 17:43
  • @MartijnPieters I have change it but the exception has changet to `TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.` – ganz Aug 12 '18 at 17:45

1 Answers1

1

Give this a try an see if that print statement prints anything.

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)
Yash Kumar Atri
  • 786
  • 1
  • 9
  • 27
  • Stil not work, I got this `json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)` ,and it doesn't print anything – ganz Aug 13 '18 at 13:32
  • If the `print(request.data)` isnt printing anything then you are not sending a valid request, So the JSON is throwing keyerror, Send a valid request then try or else tell us how you are sending the POST or GET request – Yash Kumar Atri Aug 14 '18 at 06:58
  • This is how I send the POST `
    `
    – ganz Aug 14 '18 at 09:04
  • Give this style a try first, Change the url and port number accordingly `

    Text

    `
    – Yash Kumar Atri Aug 14 '18 at 10:32
  • Thanks, but `Method Not Allowed The method is not allowed for the requested URL.` – ganz Aug 14 '18 at 16:55