Update app.py
Browse files
app.py
CHANGED
|
@@ -55,39 +55,51 @@ def predict_need_maintenance ():
|
|
| 55 |
# Define an endpoint to predict sales for Super Kart
|
| 56 |
@pred_mainteanance_api.post ('/v1/EngPredMaintenanceForBatch')
|
| 57 |
def predict_need_maintenance_for_batch ():
|
| 58 |
-
|
| 59 |
-
engine_sensor_inputs = request.get_json ()
|
| 60 |
-
|
| 61 |
# validate request (json)
|
| 62 |
# if input is valid - return prediction
|
| 63 |
# in case of error - return appropriate error
|
| 64 |
try:
|
| 65 |
# Get the uploaded CSV file from the request
|
| 66 |
-
file = request.files
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if file.filename == "":
|
| 69 |
return jsonify({
|
| 70 |
"status": "error",
|
|
|
|
| 71 |
"message": "No file selected"
|
| 72 |
}), 400
|
| 73 |
|
| 74 |
# Read the file into a DataFrame
|
| 75 |
input_df = pd.read_csv (file)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Process the data to clean up and make it ready for prediction
|
| 78 |
# mostly we will use the file with same format as given in problem statement for batch prediction
|
| 79 |
|
| 80 |
# remove/drop engine condition column if present
|
| 81 |
input_df.drop(columns=['Engine Condition'], inplace=True, errors='ignore')
|
| 82 |
|
| 83 |
-
# update column names to replace
|
| 84 |
input_df.columns = input_df.columns.str.replace(' ', '_')
|
| 85 |
|
| 86 |
-
#
|
| 87 |
int_columns = input_df.select_dtypes(include=['int64']).columns
|
| 88 |
-
|
| 89 |
-
# convert integer columns to float
|
| 90 |
-
int_columns[int_columns] = int_columns[int_columns].astype('float64')
|
| 91 |
|
| 92 |
# Validate entire batch
|
| 93 |
validated_df = validate_and_prepare_input(input_df, model)
|
|
|
|
| 55 |
# Define an endpoint to predict sales for Super Kart
|
| 56 |
@pred_mainteanance_api.post ('/v1/EngPredMaintenanceForBatch')
|
| 57 |
def predict_need_maintenance_for_batch ():
|
| 58 |
+
|
|
|
|
|
|
|
| 59 |
# validate request (json)
|
| 60 |
# if input is valid - return prediction
|
| 61 |
# in case of error - return appropriate error
|
| 62 |
try:
|
| 63 |
# Get the uploaded CSV file from the request
|
| 64 |
+
file = request.files.get('file')
|
| 65 |
|
| 66 |
+
if file is None:
|
| 67 |
+
return jsonify({
|
| 68 |
+
"status": "error",
|
| 69 |
+
"error_type": "input_error",
|
| 70 |
+
"message": "File not provided"
|
| 71 |
+
}), 400
|
| 72 |
+
|
| 73 |
if file.filename == "":
|
| 74 |
return jsonify({
|
| 75 |
"status": "error",
|
| 76 |
+
"error_type": "input_error",
|
| 77 |
"message": "No file selected"
|
| 78 |
}), 400
|
| 79 |
|
| 80 |
# Read the file into a DataFrame
|
| 81 |
input_df = pd.read_csv (file)
|
| 82 |
|
| 83 |
+
if input_df.empty:
|
| 84 |
+
return jsonify({
|
| 85 |
+
"status": "error",
|
| 86 |
+
"error_type": "input_error",
|
| 87 |
+
"message": "Uploaded file is empty"
|
| 88 |
+
}), 400
|
| 89 |
+
|
| 90 |
+
|
| 91 |
# Process the data to clean up and make it ready for prediction
|
| 92 |
# mostly we will use the file with same format as given in problem statement for batch prediction
|
| 93 |
|
| 94 |
# remove/drop engine condition column if present
|
| 95 |
input_df.drop(columns=['Engine Condition'], inplace=True, errors='ignore')
|
| 96 |
|
| 97 |
+
# update column names to replace spaces with underscore
|
| 98 |
input_df.columns = input_df.columns.str.replace(' ', '_')
|
| 99 |
|
| 100 |
+
# Convert int → float
|
| 101 |
int_columns = input_df.select_dtypes(include=['int64']).columns
|
| 102 |
+
input_df[int_columns] = input_df[int_columns].astype('float64')
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Validate entire batch
|
| 105 |
validated_df = validate_and_prepare_input(input_df, model)
|