from flask import Flask, render_template, flash, request, url_for, redirect, session
import numpy as np
import pandas as pd
import re
import os
import tensorflow as tf
from numpy import array
from keras.datasets import imdb
from keras.preprocessing import sequence
from tensorflow.keras.models import load_model
IMAGE_FOLDER = os.path.join('static', 'img_pool')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER
def init():
global model,graph
# load the pre-trained Keras model
model = load_model('sentiment_analysis_model_new.h5')
graph = tf.get_default_graph()
#########################Code for Sentiment Analysis
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template("home.html")
@app.route('/sentiment_analysis_prediction', methods = ['POST', "GET"])
def sent_anly_prediction():
if request.method=='POST':
text = request.form['text']
sentiment = ''
max_review_length = 500
word_to_id = imdb.get_word_index()
strip_special_chars = re.compile("[^A-Za-z0-9 ]+")
text = text.lower().replace("<br />", " ")
text=re.sub(strip_special_chars, "", text.lower())
words = text.split() #split string into a list
x_test = [[word_to_id[word] if (word in word_to_id and word_to_id[word]<=20000) else 0 for word in words]]
x_test = sequence.pad_sequences(x_test, maxlen=500) # Should be same which you used for training data
vector = np.array([x_test.flatten()])
with graph.as_default():
probability = model.predict(array([vector][0]))[0][0]
class1 = model.predict_classes(array([vector][0]))[0][0]
if class1 == 0:
sentiment = 'Negative'
img_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'Sad_Emoji.png')
else:
sentiment = 'Positive'
img_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'Smiling_Emoji.png')
return render_template('home.html', text=text, sentiment=sentiment, probability=probability, image=img_filename)
#########################Code for Sentiment Analysis
if __name__ == "__main__":
init()
app.run(debug=True)
试图在anaconda提示符中运行上述代码获取-
File "app.py", line 23, in init
graph = tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
我做了什么来解决这个问题?我仍然得到相同的错误:
我将导入从keras.something.something
更改为tensorflow.keras.something
,问题似乎消失了。把它放在这里是为了让别人受益
我尝试安装tf=1.14.1
目前正在使用tf== 2.4
如何在此版本中解决此问题
使用兼容性模块
相关问题 更多 >
编程相关推荐