Python 500使用flask时出现内部服务器错误
我第一次使用flask部署了一个模型。我从以下位置开始学习教程: 另外,我无法理解script.py将如何调用index.html 这是我的script.py文件:Python 500使用flask时出现内部服务器错误,python,html,machine-learning,flask,keras,Python,Html,Machine Learning,Flask,Keras,我第一次使用flask部署了一个模型。我从以下位置开始学习教程: 另外,我无法理解script.py将如何调用index.html 这是我的script.py文件: from flask import Flask app = Flask(__name__) @app.route('/') def index(): return render_template("index.html") def ValuePredictor(to_predict_list):
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
def ValuePredictor(to_predict_list):
to_predict = np.array(to_predict_list).reshape(1, 8)
loaded_model = pickle.load(open("model.pkl", "rb"))
result = loaded_model.predict(to_predict)
return result[0]
@app.route('/', methods = ['POST'])
def result():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list = list(to_predict_list.values())
to_predict_list = list(map(int, to_predict_list))
result = ValuePredictor(to_predict_list)
if int(result)== 1:
prediction ='Chance of diabetes'
else:
prediction ='You are safe'
return render_template("result.html", prediction = prediction)
以下是index.html文件:
<html>
<body>
<h3>Diabetes Prediction</h3>
<div>
<form action="/result" method="POST">
<label for="age">Age</label>
<input type="text" id="age" name="age">
<br>
<label for="preg">Number of times pregnant </label>
<input type="text" id="preg" name="preg">
<br>
<label for="p_glu">Plasma glucose concentration a 2 hours in an oral glucose tolerance test </label>
<input type="text" id="p_glu" name="p_glu">
<br>
<label for="bp">Diastolic blood pressure (mm Hg) </label>
<input type="text" id="bp" name="bp">
<br>
<label for="thickness">Triceps skin fold thickness (mm)</label>
<input type="text" id="thickness" name="thickness">
<br>
<label for="insulin">2-Hour serum insulin (mu U/ml)</label>
<input type="text" id="insulin" name="insulin">
<br>
<label for="bmi">Body mass index (weight in kg/(height in m)^2)</label>
<input type="text" id="bmi" name="bmi">
<br>
<label for="diab">Diabetes pedigree function</label>
<input type="text" id="diab" name="diab">
<br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
糖尿病预测
年龄
怀孕次数
口服葡萄糖耐量试验中2小时的血糖浓度
舒张压(毫米汞柱)
三头肌皮褶厚度(mm)
2小时血清胰岛素(μU/ml)
体重指数(体重单位kg/(身高单位m)^2)
糖尿病家系功能
result.html文件为:
<!doctype html>
<html>
<body>
<h1> {{ prediction }}</h1>
</body>
</html>
{{预测}}
您试图使用未导入的函数
您需要在python脚本的开头添加:
从flask import request,render_template如果您遇到500个错误,请查看web服务器错误日志。此外,我注意到您引用了许多变量和函数,如
render_template()
,request
和np
,但是你还没有把它们导入任何地方。@JohnGordon这是我第一次尝试,如果你能找出错误并建议编辑,那就太好了!它解决了第一个问题,现在我可以输入信息了,但是现在同样的错误出现在result.html中。