我正在嘗試使用 Flask 部署基于 NLP 的垃圾郵件檢測模型。下面是我的 app.py 代碼
import numpy as np
import pandas as pd
import nltk
import re
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
from nltk.corpus import stopwords
stop_words=stopwords.words('english')
#詞形還原
from nltk.stem import WordNetLemmatizer
lemmatizer=WordNetLemmatizer()
from flask import Flask,request,jsonify,render_template,escape
import pickle
import joblib
model = joblib.load('final_pickle_model.pkl')
model ='final_pickle_model.pkl'
app=Flask(__name__,template_folder='template')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/prediction')
def prediction():
return render_template('prediction.html')
@app.route('/prediction',methods=[ 'POST'])
def predict():
'''
For rendering results on HTML GUI
'''
int_features=[str(x) for x in request.form.values()]
a=int_features
msg=str(a)
filter_sentence=''
sentence=re.sub(r'[^\w\s]','',msg) #cleaning
words=nltk.word_tokenize(sentence)#tokenize
words=[w for w in words if not w in stop_words]
for word in words:
filter_sentence=filter_sentence ' ' str(lemmatizer.lemmatize(word)).lower()
data=(filter_sentence)
print(data)
my_prediction=model.predict(data)
my_prediction=int(my_prediction)
print(my_prediction)
if my_prediction==1:
print("This tweet is real")
return render_template('prediction.html',prediction_text="This tweet is real")
else:
print("This tweet is spam")
return render_template('prediction.html', prediction_text="This tweet is spam")
if __name__=="__main__":
app.run(debug=True)
如果我只運行我的 ML 模型,它會完美無誤地運行。但是當我使用燒瓶(上面的代碼)部署它并輸入文本并按下預測按鈕時,我收到以下錯誤:- AttributeError:'str'物件沒有屬性'predict'。
如何解決此錯誤
uj5u.com熱心網友回復:
model = joblib.load('final_pickle_model.pkl')
model ='final_pickle_model.pkl'
model您的變數似乎被重新定義為str. 這就是發生錯誤的原因,也許你可以得到這個model另一個名字?
uj5u.com熱心網友回復:
我想你的問題在這里:
model = joblib.load('final_pickle_model.pkl') <-- looks correct
model ='final_pickle_model.pkl' <-- looks out of place (probably a mistake?)
您已定義model為一個簡單的字串,顯然字串沒有predict()方法。
uj5u.com熱心網友回復:
看我不知道如何解決它,但我認為錯誤是'str'(字串模塊)沒有預定義的函式'predict',所以函式'predict'不能與字串變數一起使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/459290.html
上一篇:如何重繪JWT
