hi i experience a problem. So i deploy a model in flask
and then make a post request
as I result I take back the following error. I can figure out how to fix it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from flask import Flask, request, jsonify #--jsonify will return the data from keras.models import load_model import numpy as np import pandas as pd #creates the web service running on port 127.0.0.1:12345 and answer post requests on address of the machine/api app = Flask(__name__) model = load_model( 'modelfordeploy.h5' ) @app .route( "/" ) def hello(): return "machine learning model APIs!" @app .route( '/api' , methods = [ 'GET' , 'POST' ]) def predict(): # get the json send from the post as data data = request.get_json(force = True ) print (data) # i want 6 variables to make the prediction #i expected jason to have the following values predict_request = [data[ "accx" ], data[ "accy" ], data[ "accz" ], data[ "gyrx" ], data[ "gyry" ], data[ "gyrz" ]] print (predict_request) predict_request = np.array(predict_request) print (predict_request) type (predict_request) predict_request = np.asarray( predict_request).reshape( - 1 , 1 , 6 ) pred = model.predict(predict_request) print (pred) output = [pred[ 0 ]] print (output) #take a list of dictionaries and convert them to json return jsonify(results = output) if __name__ = = '__main__' : app.run(port = 8000 , debug = True ) #threaded=False) |
1 2 3 4 5 6 7 8 9 |
import requests, json import pandas as pd import numpy as np data = json.dumps ({ "accx" : 0.660583 , "accy" : 0.454468 , "accz" : - 0.585022 , "gyrx" : 32.366615 , "gyry" : 27.206556 , "gyrz" : - 23.471800 }) r = requests.get(url, data) print (r.json()) |
Error: File "request.py", line 22, in <module>
print (r.json())
File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\site-packages\requests\models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\aggelos\anaconda3\envs\tensorflow_env\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
(tensorflow_env) C:\Users\aggelos\Desktop\new_try>