question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting error when loading on flask | Session issue

See original GitHub issue

I am getting session management issue when try to use it in flask application, can you please help me

ValueError: Tensor(“TensorSliceDataset:0”, shape=(), dtype=variant) must be from the same graph as Tensor(“Iterator:0”, shape=(), dtype=resource).

Sample code structure


from io import StringIO
import flask
import pandas as pd
import tensorrec as tr
import tensorflow as tf


class ScoringService(object):
    model = None                
	model_path = 'model path'
    @classmethod
    def get_model(cls):
        if cls.model == None:
            cls.model = tr.TensorRec.load_model(model_path)
           
        return cls.model

    @classmethod
    def get_reco(cls, model,use_ft, ite_ft):
            tf.reset_default_graph()
            predictions = model.predict(use_ft, ite_ft)
        return predictions

    @classmethod
    def predict(cls, input):
        clf = cls.get_model()
        n_reco_test = cls.get_reco(clf, input,use_ft, ite_ft)
        return n_reco_test

# The flask app for serving predictions
app = flask.Flask(__name__)

@app.route('/invocations', methods=['POST'])
def transformation():
    data = None

    # Convert from CSV to pandas
    if flask.request.content_type == 'text/csv':
        data = flask.request.data.decode('utf-8')
        s = StringIO(data)
        data = pd.read_csv(s, header=None)
    else:
        return flask.Response(response='This predictor only supports CSV data', status=415, mimetype='text/plain')
    # Do the prediction
    predictions = ScoringService.predict(data)
    return flask.Response(response=predictions, status=200,mimetype='text/csv')

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gallmercicommented, Dec 12, 2018

@jaiswalvineet I got the same problem. The tf graph is not thread safe and needs to be globally defined and always reused when doing the prediction. Here is how I solved it (actually solution is coming from https://github.com/keras-team/keras/issues/2397#issuecomment-306687500):

I inserted a global model variable, loaded the model and stored the graph after model loading:

import tensorflow as tf
from tensorrec import TensorRec
model = TensorRec.load_model(directory_path=model_path)
graph = tf.get_default_graph()

and then in every call, I used the same graph as loaded in graph with a new session:

with graph.as_default():
    session = tf.Session()
    with session.as_default():
        # Now do the prediction
        predictions = ScoringService.predict(data)
0reactions
jaiswalvineetcommented, Dec 6, 2018

I removed the tf.reset_default_graph() bus still same error, seems the flask app does persist the graph, it’s strange that its working if I directly run it but does not work if I call it from flask app, if its running one time for you from flask then refresh it on browser, you will get the error, if not then please share your flask app …thanks in advance

Read more comments on GitHub >

github_iconTop Results From Across the Web

Internal Server Error when using Flask session - Stack Overflow
I want to save an ID between requests, using Flask session cookie, but I'm getting an Internal Server Error as result, when I...
Read more >
How To Fix an Internal Server Error in Flask - YouTube
In this beginner level video I explain what steps you need to take when you get an Internal Server Error in your Flask...
Read more >
How To Handle Errors in a Flask Application - DigitalOcean
In this tutorial, you'll build a small web application that demonstrates how to handle common errors one encounters when developing a web ...
Read more >
Flask session issues that occur after deployment - Reddit
Flask session issues that occur after deployment ... al. i found the following (up until now) working solution to start my gunicorn server:....
Read more >
Quickstart — Flask Documentation (1.1.x)
The FLASK_APP environment variable is the name of the module to import at flask run. In case that module is incorrectly named you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found