我第一次使用flask部署模型。我遵循的教程来自: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>
result.html文件为:
<!doctype html>
<html>
<body>
<h1> {{ prediction }}</h1>
</body>
</html>
答案 0 :(得分:1)
您正在尝试使用未导入的功能。
您需要在python脚本的开头添加:
从烧瓶导入请求,render_template