【发布时间】:2019-08-16 19:54:07
【问题描述】:
伙计们。
所以我的问题如下。 我创建了一个线性模型并在 Python 3.7 中将其保存为 .pkl。
然后,我用如下所示的代码创建了一个app.py文件(html模板文件也被创建了)。
import pickle
from flask import Flask, request, render_template, jsonify
#creating instance of the class
app=Flask(__name__)
#loading the model
model = pickle.load(open("model.pkl", "rb"))
#loading the index template and the main page
@app.route('/')
def index():
return render_template('index.html')
#inputs from the user
@app.route('/result', methods=['POST'])
def result():
features = request.get_json(force=True)['Input1', 'Input2', 'Input3',
'Input4', 'Input5', 'Input6',
'Input7', 'Input8', 'Input9']
#creating a response object
#storing the model's prediction in the object
response = {}
response['predictions'] = model.predict([features]).tolist()
#returning the response object as json
return flask.jsonify(response)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000, debug=True)
问题是,当我运行 app.py 文件时,我收到此错误:“无法解码 JSON 对象:预期值:第 1 行第 1 列(字符 0)”。我尝试多次修改我的代码,甚至试图找到另一种解决方案来编写它,但到目前为止没有运气,它总是会引发错误。
我的代码中是否有任何可能引发此错误的错误?我应该注意(如果它可能很重要的话)我所有的变量都是浮点类型,除了一个整数。
这里包含的html脚本如下:
<html>
<body>
<h3>Prediction_form</h3>
<div>
<form action="{{ url_for('result') }}" method="POST">
<label for="Input1">Input1</label>
<input type="text" id="Input1" name="Input1">
<br>
<label for="Input2">Input2</label>
<input type="text" id="Input2" name="Input2">
<br>
<label for="Input3">Input3</label>
<input type="text" id="Input3" name="Input3">
<br>
<label for="Input4">Input4</label>
<input type="text" id="Input4" name="Input4">
<br>
<label for="Input5">Input5</label>
<input type="text" id="Input5" name="Input5">
<br>
<label for="Input6">Input6</label>
<input type="text" id="Input6" name="Input6">
<br>
<label for="Input7">Input7</label>
<input type="text" id="Input7" name="Input7">
<br>
<label for="Input8">Input8</label>
<input type="text" id="Input8" name="Input8">
<br>
<label for="Input9">Input9</label>
<input type="text" id="Input9" name="Input9">
<br>
<input type="submit" value="Submit">
<br>
<br>
{{ prediction_text }}
</form>
</div>
</body>
</html>
我是 Python 新手,所以我可能遗漏了一些重要的东西。
感谢任何帮助。
非常感谢。
【问题讨论】:
-
请包含将 POST 发送到
/result的 javascript 或表单。 -
嗨,戴夫,感谢您的评论。我编辑了我的帖子并包含了 html 代码。