[英]500 internal server error when using flask
我第一次使用 flask 部署 model。 我正在關注以下教程: https://www.geeksforgeeks.org/deploy-machine-learning-model-using-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):
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>
結果.html文件為:
<!doctype html>
<html>
<body>
<h1> {{ prediction }}</h1>
</body>
</html>
您正在嘗試使用未導入的函數。
您需要在 python 腳本的開頭添加:
從 flask 導入請求,render_template
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.