Search code examples
python-2.7flaskmachine-learningmodelflask-restful

error when parsing value into Model (flask)


Objective is to accept UI multiple parameters and give it to the model (127.0.0.1:5002)using flask API and then the scoring from the model post back to UI (127.0.0.1:5001)

I am getting error (which is posted below at the end) when a model accept the value from the UI. So i am posting values to 127.0.0.1:5002 where model takes that as 1 json object but i am getting error.

So i post 1 json object from this code (let me know if there is an issue in the code- i am a newbie)

 <script>
 $(function() {$('#analysis').bind('click', function() {
   $.post('http://127.0.0.1:5002/',{
     'CK': $('CK').val(),
     'OCE': $('OCE').val(),
     'range_04': $('range_04').val(),
   },
   function(data) {
                 var parsed = JSON.parse(data);
                 $("#xyz").text(parsed['abc']);
             });
            return false;
     });
   });
</script>

Now this code generates the json (and that json object feeds the model)

app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('args.xyz')


class getPredProb(Resource):
    def post(self):
        args = parser.parse_args()
        clf = joblib.load('AO.pkl')
        frameToScore = pandas.read_json('args.xyz')
        prediction = clf.predict(frameToScore)
        probability = clf.predict_proba(frameToScore)
        return json.dumps({'Prediction': prediction},{'Probability':probability}), 201, {'Access-Control-Allow-Origin': 'http://127.0.0.1:5001'}



api.add_resource(getPredProb, '/')

if __name__ == '__main__':
    #http_server = WSGIServer(('', 5002), app)
    #http_server.serve_forever()
    app.run(debug=True,port=5002)

Image of ERROR


Solution

  • You're only passing a string, 'args.xyz', to read_json, you should use args['xyz'] (assuming this is the json data, as I don't see anything with an xyz key being passed to the backend).