0

When I try to check this code in postman using POST method http://0.0.0.0:8088/prediction it gives me Error Occurred! <class 'KeyError'> I have tried using try and except but it not working any solutions?? Posting the code which gives the error.

@app.route('/prediction', methods=['POST'])
@cross_origin()

def single_prediction_route_client():
 try:
        config = Config()
        # get run id
        run_id = config.get_run_id()
        data_path = config.prediction_data_path
        print('Test')

        if request.method == 'POST':
            satisfaction_level = request.form['satisfaction_level']
            last_evaluation = request.form["last_evaluation"]
            number_project = request.form["number_project"]
            average_montly_hours = request.form["average_montly_hours"]
            time_spend_company = request.form["time_spend_company"]
            work_accident = request.form["work_accident"]
            promotion_last_5years = request.form["promotion_last_5years"]
            salary = request.form["salary"]

            data = pd.DataFrame(data=[
                [0, satisfaction_level, last_evaluation, number_project, average_montly_hours, time_spend_company,
                 work_accident, promotion_last_5years, salary]],
                columns=['empid', 'satisfaction_level', 'last_evaluation', 'number_project',
                         'average_montly_hours', 'time_spend_company', 'Work_accident',
                         'promotion_last_5years', 'salary'])
            # using dictionary to convert specific columns
            convert_dict = {'empid': int,
                            'satisfaction_level': float,
                            'last_evaluation': float,
                            'number_project': int,
                            'average_montly_hours': int,
                            'time_spend_company': int,
                            'Work_accident': int,
                            'promotion_last_5years': int,
                            'salary': object}

            data = data.astype(convert_dict)

            # object initialization
            predictModel = PredictModel(run_id, data_path)
            # prediction the model
            output = predictModel.single_predict_from_model(data)
            print('output : ' + str(output))
            return Response("Predicted Output is : " + str(output))
    except ValueError:
        return Response("Error Occurred! %s" % ValueError)
    except KeyError:
        return Response("Error Occurred! %s" % KeyError)
    except Exception as e:
        return Response("Error Occurred! %s" % e)

0 Answers0