I use flask as API server. I test it with python it's ok!
class ModelLoader(MethodView):
def __init__(self):
pass
def post(self):
content = request.get_json()
request
X_input = content['X_input']
X_in = str(X_input)
pred_val,posib = predictor.predict(X_input=X_in)
pred_val = pred_val.tolist()
posib = str(round(posib,4)*100)
#posib = posib.tolist()
return jsonify({'pred_val': pred_val,'confident':posib})
def initialize_models(json_path, weights_path, normalized_x, normalized_y):
global predictor
predictor = Predictor(json_path, weights_path, normalized_x, normalized_y)
predictor.compile_model(loss='categorical_crossentropy', optimizer='adam')
def run(host='0.0.0.0', port=19865):
"""Run a WSGI server using gevent."""
app.add_url_rule('/predict',
view_func=ModelLoader.as_view('predict'))
print('running server http://{0}'.format(host + ':' + str(port)))
WSGIServer((host, port), app).serve_forever()
test code
import requests
def get_predictions(X_input):
"""Get predictions from a rest backend for your input."""
print("Requesting prediction for XOR with {0}".format(X_input))
r = requests.post("http://yansheng.wang:19865/predict", json={'input': X_input})
print(r.status_code, r.reason)
resp = r.json()
prediction = resp['pred_val']
confident = resp['confident']
print("XOR of input: {0} is {1},on confident of {2} ".format(X_input, prediction,confident))
if __name__ == '__main__':
X_inputs = ["885863343916543724"]
for x_input in X_inputs:
get_predictions(x_input)
when I run it I can't get post data from postman or web page!
the server console output difference is like below,what's wrong?:
45.76.182.34 - - [2017-07-29 12:11:55] "POST /predict HTTP/1.1" 200 162 0.044396
45.76.182.34 - - [2017-07-29 12:12:03] "POST /predict?input=3423432432424233 HTTP/1.1" 400 303 0.001046
when you are testing the code with your test code you are passing the X_input
value in json body.
# part of request
json={'input': X_input}
# I would suggest make the key consistent
# inside post menthod
content = request.get_json()
request
X_input = content['X_input']
X_in = str(X_input)
But when you are sending it from web/postman you are sending it in url params.
"POST /predict?input=3423432432424233 HTTP/1.1" 400
Hence it's failing. Send this data from postman in json body. And it should work.
example:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments