我试图从flask UI中传递一条推文,并能够预测该推文的类型,如捐款、灾难等。
这是一个来自Jupyter笔记本的工作代码。
loaded_model = joblib.load('NB_spam_model.pkl')
result = loaded_model.score(X_test, y_test)
predict = loaded_model.predict([new_tweet])
print(result)
print(predict)
结果是
0.8409090909090909
['donations_and_help']
谁能帮我看看这段代码,纠正我哪里做错了?
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html" )
@app.route("/predict", methods=["GET","POST"])
def api():
if request.method == "POST":
words = joblib.load('words.pkl')
model = joblib.load('NB_spam_model.pkl')
pstem = PorterStemmer()
tweet = request.form["tweet"]
text = tweet
text = re.sub("[^a-zA-Z]", ' ', text)
text = text.lower()
text = text.split()
text = [pstem.stem(word) for word in text if not word in set(stopwords.words('english'))]
text = ' '.join(text)
print("This is the text %s:" + text)
query = []
for word in words:
if word in text:
query.append(1)
else:
query.append(0)
print(query)
#prediction = list(model.predict(np.matrix(query)))[0]
pred = model.predict(query)[0]
print(pred)
if pred == 1:
msg = "."
return render_template("index.html", msg=msg, tweet=tweet)
else:
error = "Approximately 70%, your tweet Fake"
return render_template("index.html", error=error, tweet=tweet)
else:
return redirect(url_for("index"))
if __name__ == '__main__':
app.run(debug=False)
堆栈跟踪。
Project\lib\site-packages\sklearn\feature_extraction\text.py",
line 69, in _preprocess
doc = doc.lower()
AttributeError: 'int' object has no attribute 'lower'
127.0.0.1 - - [03/Jun/2020 20:17:47] "?[35m?[1mPOST /predict HTTP/1.1?[0m" 500 -
正如你所看到的错误是AttributeError: 'int' object has no attribute 'lower' which means integer cannot be lower-cases. 在你的代码中的某个地方,它试图将整数对象小写,这是不可能的。print(text)
转换成低级别的前