[英]500 internal server error when using flask
I'm deploying a model for first time using flask.我第一次使用 flask 部署 model。 I was following the tutorial from: https://www.geeksforgeeks.org/deploy-machine-learning-model-using-flask/ Also I can't make sense of how the script.py will be able to call index.html Here is my script.py file:
我正在关注以下教程: 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)
Here is the index.html file:这是 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>
The result.html file is:结果.html文件为:
<!doctype html>
<html>
<body>
<h1> {{ prediction }}</h1>
</body>
</html>
You're trying to use uninmported functions.您正在尝试使用未导入的函数。
You need to add at the start of your python script:您需要在 python 脚本的开头添加:
from flask import request, render_template从 flask 导入请求,render_template
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.