[Solved]: TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor is not an element of this graph
Solution: Flask uses multiple threads. The problem you are running into is because the tensorflow model is not loaded and used in the same thread. One workaround is to force tensorflow to use the global default graph.
This worked for me
from keras import backend as K
and after predicting my data I inserted this part of code then I had again loaded the model.
K.clear_session()
Example:
import json
from flask import Flask, request
from flask_restful import Api
from load import model_load
from sentiment import get_sentiment
from keras import backend as K
app = Flask(__name__)
api = Api(app)
@app.route('/text/sent', methods=['POST'])
def sentiment_post():
text = request.json.get('text')
sent = get_sentiment(text)
K.clear_session()
res = {}
res['Negative'] = '%f'%sent[0]
res['Positive'] = '%f'%sent[1]
return json.dumps(res)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)