rinormaloku/k8s-mastery

sa-logic not working

agorkiy opened this issue · 4 comments

Hi
sa-logic python app is not working in my case.
Started it on local computer and posted a request:
curl -X POST localhost:5000/analyse/sentiment --data '{"sentence": "test"}'
got this answer:

<title>500 Internal Server Error</title>

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

And here is what have in terminal tab where python is running:
agorkiy@home:~/k8s-mastery/sa-logic/sa$ sudo python3 sentiment_analysis.py

  • Serving Flask app "sentiment_analysis" (lazy loading)
  • Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    [2020-03-27 17:07:22,604] ERROR in app: Exception on /analyse/sentiment [POST]
    Traceback (most recent call last):
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functionsrule.endpoint
    File "sentiment_analysis.py", line 9, in analyse_sentiment
    sentence = request.get_json()['sentence']
    TypeError: 'NoneType' object is not subscriptable
    127.0.0.1 - - [27/Mar/2020 17:07:22] "POST /analyse/sentiment HTTP/1.1" 500 -

Any thoughts - am I doing something wrong or there is a bug somewhere here?

In k8s-mastery/sa-logic/sa/requirements.txt file, change Flask version to 0.12.5 and do python3 -m pip install -r requirements.txt.
Make sure you're using Python version 3. Check by doing python --version or python3 --version.

Did, looks for me that nothing changed.

agorkiy@home:/k8s-mastery/sa-logic/sa$ python3 -m pip install -r requirements.txt
Collecting Flask==0.12.5 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/a4/36/756c34af4523bb0dfa77d3c83455bc4d5d01d6f03b20d8414f3e4deb8669/Flask-0.12.5-py2.py3-none-any.whl (81kB)
100% |████████████████████████████████| 81kB 1.7MB/s
Collecting textblob==0.15.0 (from -r requirements.txt (line 2))
Using cached https://files.pythonhosted.org/packages/56/6d/ebe0b935eaf1f9f35cdf8446882608fbda4f3bfcc1c3648e50606c7476e7/textblob-0.15.0-py2.py3-none-any.whl
Collecting Jinja2>=2.4 (from Flask==0.12.5->-r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/27/24/4f35961e5c669e96f6559760042a55b9bcfcdb82b9bdb3c8753dbe042e35/Jinja2-2.11.1-py2.py3-none-any.whl
Collecting Werkzeug<1.0,>=0.7 (from Flask==0.12.5->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/c2/e4/a859d2fe516f466642fa5c6054fd9646271f9da26b0cac0d2f37fc858c8f/Werkzeug-0.16.1-py2.py3-none-any.whl (327kB)
100% |████████████████████████████████| 327kB 3.1MB/s
Collecting itsdangerous>=0.21 (from Flask==0.12.5->-r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=2.0 (from Flask==0.12.5->-r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/dd/c0/4d8f43a9b16e289f36478422031b8a63b54b6ac3b1ba605d602f10dd54d6/click-7.1.1-py2.py3-none-any.whl
Collecting nltk>=3.1 (from textblob==0.15.0->-r requirements.txt (line 2))
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.12.5->-r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting six (from nltk>=3.1->textblob==0.15.0->-r requirements.txt (line 2))
Using cached https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, Flask, six, nltk, textblob
Successfully installed Flask-1.1.1 Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 click-7.1.1 itsdangerous-1.1.0 nltk-3.4.5 six-1.14.0 textblob-0.15.0
agorkiy@home:/k8s-mastery/sa-logic/sa$ python --version
Python 2.7.17
agorkiy@home:/k8s-mastery/sa-logic/sa$ python3 --version
Python 3.6.9
agorkiy@home:/k8s-mastery/sa-logic/sa$ sudo python3 sentiment_analysis.py

  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    [2020-03-29 12:34:40,976] ERROR in app: Exception on /analyse/sentiment [POST]
    Traceback (most recent call last):
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
    File "/home/agorkiy/.local/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functionsrule.endpoint
    File "sentiment_analysis.py", line 9, in analyse_sentiment
    sentence = request.get_json()['sentence']
    TypeError: 'NoneType' object is not subscriptable
    127.0.0.1 - - [29/Mar/2020 12:34:40] "POST /analyse/sentiment HTTP/1.1" 500 -

@agorkiy Are you passing request body correctly? Make sure you're passing request body with JSON object and sentence key. Can you share your cURL request?

@dhruvdutt
Yes, found mistake - forgot to add -H "Content-Type: application/json" in POST request.
Now it's working:
agorkiy@home:/k8s-mastery/sa-logic/sa$ curl -d '{"sentence": "I hate you!"}' -H "Content-Type: application/json" -X POST http://localhost:5000/analyse/sentiment
{
"polarity": -1.0,
"sentence": "I hate you!"
}
Thanks for help.