Forums

Page shows but nothing works...

Finally managed to launch the web-app but although the page is presented correctly nothing works. I get the following in the error logs. Can someone tell me what is the problem and how to fix it?

2018-04-16 08:46:45,910: [2018-04-16 08:46:45,908] ERROR in app: Exception on /get/answer [GET]

2018-04-16 08:46:45,911: Traceback (most recent call last):

2018-04-16 08:46:45,911: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app

2018-04-16 08:46:45,912: response = self.full_dispatch_request()

2018-04-16 08:46:45,912: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request

2018-04-16 08:46:45,912: rv = self.handle_user_exception(e)

2018-04-16 08:46:45,912: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception

2018-04-16 08:46:45,912: reraise(exc_type, exc_value, tb)

2018-04-16 08:46:45,912: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request

2018-04-16 08:46:45,912: rv = self.dispatch_request()

2018-04-16 08:46:45,912: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request

2018-04-16 08:46:45,912: return self.view_functionsrule.endpoint

2018-04-16 08:46:45,913: File "/home/Stylianos/Improved-Dynamic-Memory-Networks-DMN-plus/webapp.py", line 94, in get_answer

2018-04-16 08:46:45,913: "memory_probs": memory_probs.T.tolist()

2018-04-16 08:46:45,913: AttributeError: 'NoneType' object has no attribute 'T'

2018-04-16 08:46:45,908: Exception on /get/answer [GET]#012Traceback (most recent call last):#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012 response = self.full_dispatch_request()#012

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request#012 rv = self.handle_user_exception(e)#012

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception#012 reraise(exc_type, exc_value, tb)#012

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request#012 rv = self.dispatch_request()#012

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request#012 return self.view_functionsrule.endpoint#012

File "/home/Stylianos/Improved-Dynamic-Memory-Networks-DMN-plus/webapp.py", line 94, in get_answer#012 "memory_probs": memory_probs.T.tolist()#012

AttributeError: 'NoneType' object has no attribute 'T'

This is the Webapp.py file of the project

""" Webapp """

import glob
import flask
import numpy as np

from main import *

app = flask.Flask(__name__)

seed_val = 42
np.random.seed(seed_val)

task = args = dmn = network_name = test_input = test_q = test_answer = pred_answer = pred_prob = memory_probs = None

@app.route('/')
def index():
    return flask.render_template("index.html")

@app.route('/get/taskType', methods=['GET'])
def get_taskType():
    global task, args, dmn, network_name, test_input, test_q, test_answer, pred_answer, pred_prob, memory_probs

    task = flask.request.args.get('taskType_i')
    task = ''.join(task.split())

    args = dmn_start()

    if task == '2':
        args.babi_id = '2'
        args.load_state = 'states/dmn_smooth.mh3.n80.bs10.d0.01.babi2.epoch129.test0.96979.state'
    elif task == '3':
        args.babi_id = '3'
        args.load_state = 'states/dmn_smooth.mh3.n80.bs10.d0.01.babi3.epoch5.test2.28914.state'
    elif task == '6':
        args.babi_id = '6'
        args.load_state = 'states/dmn_smooth.mh3.n80.bs10.d0.01.babi6.epoch90.test0.01423.state'
    elif task == '17':
        args.babi_id = '17'
        args.load_state = 'states/dmn_smooth.mh3.n80.bs10.d0.1.babi17.epoch498.test1.19528.state'
    else:
        args.babi_id = '2'
        args.load_state = 'states/dmn_smooth.mh3.n80.bs10.d0.01.babi2.epoch129.test0.96979.state'

    args, network_name, dmn = dmn_mid(args)

@app.route('/get/story', methods=['GET'])
def get_story():
    global task, args, dmn, network_name, test_input, test_q, test_answer, pred_answer, pred_prob, memory_probs

    test_input = dmn.test_input
    test_q = dmn.test_q
    test_answer = dmn.test_answer

    question_idx  = np.random.randint(test_q.shape[0])

    return_data = dmn.step(question_idx, 'test')

    story_txt = [dmn.ivocab[k] for i in return_data["inp"] for j in i for k in j]
    question_txt = [dmn.ivocab[j] for i in return_data["q"] for j in i]
    correct_answer = [dmn.ivocab[i] for i in return_data["answers"]]
    pred_answer = [dmn.ivocab[i] for i in return_data["prediction"].argmax(axis=1)]
    pred_prob = [return_data["prediction"][0][i] for i in return_data["prediction"].argmax(axis=1)]

    story_txt = ' '.join(filter(None, story_txt))
    question_txt = ' '.join(filter(None, question_txt))
    correct_answer = correct_answer[0]
    pred_answer = pred_answer[0]
    pred_prob = float(pred_prob[0])

    memory_probs = return_data["attentions"][0]

    story_txt = story_txt.replace(" . ", ".\n")
    question_txt += "?"

    return flask.jsonify({
        "question_idx": question_idx,
        "story": story_txt,
        "question": question_txt,
        "correct_answer": correct_answer,
        #"pred_answer" : pred_answer
    })

@app.route('/get/answer', methods=['GET'])
def get_answer():
    question_idx  = flask.request.args.get('question_idx')
    user_question = flask.request.args.get('user_question', '')

    return flask.jsonify({
        "pred_answer" : pred_answer,
        "pred_prob" : pred_prob,
        "memory_probs": memory_probs.T.tolist()
    })

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

This is my wsgi file:

# The below has been auto-generated for your Flask project

import sys

# add your project directory to the sys.path
project_home = u'/home/Stylianos/Improved-Dynamic-Memory-Networks-DMN-plus'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from webapp import app as application  # noqa

So the exception message is this:

2018-04-16 08:46:45,913: "memory_probs": memory_probs.T.tolist()
2018-04-16 08:46:45,913: AttributeError: 'NoneType' object has no attribute 'T'

This means that the value of your global variable memory_probs is None. This is because in the instance of the website code that your browser is visiting, the code that sets it has not been called.

The important thing to note here is that your website is made up of a number of different processes, each running the same code. Any particular request that comes in to the site can go to any of the different processes. So, when you access the view /get/answer, you can't assume that the same process previously handled a request to /get/story. That request might have been handled by a different process. And if that is the case, the process handling the /get/answer won't have a useful value in the memory_probs variable -- it will just be the default None (or perhaps even a value from some other website user's access to /get/story).

This means that in general, you can't usefully use global variables in website code. (There are exceptions, but none relevant to this example.)

In order to maintain state between request, you need to use sessions; there are a lot of good tutorials on how to do that in Flask, so I won't try to duplicate them here -- the best option is probably for you to google for it and find one that looks like it will be good for you.