我想使用Flask部署模型RandomForestClassifier。 我在git hub-https://github.com/a-djebali/flask-machine-learning-resful
中获得了参考但是当我向服务发送请求时,它将无法工作,而是给出了错误消息,
下面的Request.py代码
导入请求 导入json 将numpy导入为np
url = "http://127.0.0.1:9000/predict_api"
data = json.dumps({'sl':5.84,'sw':3.0,'pl':3.75,'pw':1.1})
r = requests.post(url,data)
print(r.json())
用于发布app.py 下面的代码
import numpy as np
from flask import Flask, abort, jsonify, request
import pickle as pickle
random_forest_model = pickle.load(open("rfc.pkl","rb"))
app = Flask(__name__)
@app.route('/predict_api', methods=['POST'])
def predict():
# Error checking
data = request.get_json(force=True)
# Convert JSON to numpy array
predict_request = [data['sl'],data['sw'],data['pl'],data['pw']]
predict_request = np.array(predict_request)
# Predict using the random forest model
y = random_forest_model.predict(predict_request)
# Return prediction
output = [y[0]]
return jsonify(results=output)
if __name__ == '__main__':
app.run(port = 9000, debug = True)
错误消息
raw_decode中的文件“ C:\ Anaconda \ lib \ json \ decoder.py”,第357行 从None提高JSONDecodeError(“期望值”,s,err.value)
JSONDecodeError:期望值
是否有人尝试使用Flask Web服务,请帮助我