Python Flask webapp:';代币';对象没有属性';测试';|渲染模板错误
在假新闻中编码.py:Python Flask webapp:';代币';对象没有属性';测试';|渲染模板错误,python,python-3.x,numpy,flask,deployment,Python,Python 3.x,Numpy,Flask,Deployment,在假新闻中编码.py: from flask import Flask, render_template, request from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import PassiveAggressiveClassifier import pickle import pandas as pd from sklearn.model_selection impo
from flask import Flask, render_template, request
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import PassiveAggressiveClassifier
import pickle
import pandas as pd
from sklearn.model_selection import train_test_split
app = Flask(__name__)
tfvect = TfidfVectorizer(stop_words='english', max_df=0.7)
loaded_model = pickle.load(open('D:\Fake_News_Detection\model.pkl', 'rb'))
dataframe = pd.read_csv('D:\Fake_News_Detection\data.csv')
x = dataframe['text']
y = dataframe['label']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
def fake_news_det(news):
tfid_x_train = tfvect.fit_transform(x_train)
tfid_x_test = tfvect.transform(x_test)
input_data = [news]
vectorized_input_data = tfvect.transform(input_data)
prediction = loaded_model.predict(vectorized_input_data)
return prediction
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
message = request.form['message']
pred = fake_news_det(message)
print(pred)
return render_template('index.html', prediction=pred)
else:
return render_template('index.html', prediction="Something went wrong")
if __name__ == '__main__':
app.run(debug=True)
运行代码并在本地端口上查看后,会显示一个错误:-
AttributeError AttributeError:“令牌”对象没有属性“测试”
它有一个错误列表,按照类型jinja错误的顺序排列,但我认为
File "D:\Fake_News_Detection\Fake_News_Det.py", line 31, in home
return render_template('index.html')
这与模板文件夹中的index.html文件有关
我的文件路径:
请检查这些图像文件
编辑:
index.html代码:
假新闻尝试更改python版本。有时降级和升级会有所帮助
它最好在2.7.8+上运行。请粘贴index.html的代码/内容。@simpleApp现在检查,我已经更新了一些在我的设置上运行的行,例如从loaded\u model=pickle.load(打开('D:\Fake\u News\u Detection\model.pkl','rb')
到x\u train,x\u test,y\u train,y\u test=train\u test\u split(x,y,y,test size=0.2,random\u state=0)
。当http://127.0.0.1:5000
,它打开了网页。看起来一切正常,因此无法重新创建。我能给你提供github代码吗@simpleApp@simpleApp回复brotherWorked bro安装了2.7.0,并且正在使用2.9.x。很高兴知道这有帮助