这是我第一次在Web服务器上工作,我正在使用flask编写宁静的get请求,以将参数传递给我构建的预测模型。
app = Flask(__name__)
api = Api(app)
class AST(Resource):
def get(self):
args = request.args
x1 = args['key1']
x2 = args['key2']
X = [[x1, x2]]
x = load_scaler.transform(X)
y_predict = load_model.predict(x)
y_predict = load_model.predict(np.array(x)).tolist
time = [{'predection':y_predict}]
return Response (json.dumps(time), mimetype='application/json')
api.add_resource(AST, '/start_time')
if __name__ == '__main__':
app.run
运行代码时,它给了我错误:
回溯显示
File "/Users/123/Documents/123/webserver/predictionast.py", line 60, in get
return Response (json.dumps(time), mimetype='application/json')
File "/Users/123/opt/anaconda3/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/Users/123/opt/anaconda3/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Users/123/opt/anaconda3/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/Users/123/opt/anaconda3/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type builtin_function_or_method is not JSON serializable
y_predict是数组