Upload 2 files
Browse files- app.py +83 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Create the Flask application instance
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Load the pre-trained model and preprocessor
|
| 10 |
+
try:
|
| 11 |
+
model = joblib.load("xgboost_model.joblib")
|
| 12 |
+
onehot_encoder = joblib.load("onehot_encoder.joblib")
|
| 13 |
+
print("Model and encoder loaded successfully.")
|
| 14 |
+
except FileNotFoundError:
|
| 15 |
+
print("Error: Model or encoder file not found.")
|
| 16 |
+
model = None
|
| 17 |
+
onehot_encoder = None
|
| 18 |
+
|
| 19 |
+
# Define the categorical columns used in the original training
|
| 20 |
+
categorical_cols = [
|
| 21 |
+
'Product_Sugar_Content', 'Product_Type', 'Store_Size',
|
| 22 |
+
'Store_Location_City_Type', 'Store_Type'
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
# Define the numerical columns used in the original training
|
| 26 |
+
numerical_cols = [
|
| 27 |
+
'Product_Weight', 'Product_Allocated_Area', 'Product_MRP',
|
| 28 |
+
'Store_Establishment_Year'
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
@app.route('/predict', methods=['POST'])
|
| 32 |
+
def predict():
|
| 33 |
+
"""
|
| 34 |
+
Endpoint to make predictions on new data.
|
| 35 |
+
Input should be a JSON object with the following keys:
|
| 36 |
+
- Product_Weight (float)
|
| 37 |
+
- Product_Sugar_Content (string)
|
| 38 |
+
- Product_Allocated_Area (float)
|
| 39 |
+
- Product_Type (string)
|
| 40 |
+
- Product_MRP (float)
|
| 41 |
+
- Store_Establishment_Year (int)
|
| 42 |
+
- Store_Size (string)
|
| 43 |
+
- Store_Location_City_Type (string)
|
| 44 |
+
- Store_Type (string)
|
| 45 |
+
"""
|
| 46 |
+
if model is None or onehot_encoder is None:
|
| 47 |
+
return jsonify({"error": "Model not loaded. Check server logs."}), 500
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Get JSON data from the request
|
| 51 |
+
data = request.get_json(silent=True)
|
| 52 |
+
if not data:
|
| 53 |
+
return jsonify({"error": "No data provided or invalid JSON format."}), 400
|
| 54 |
+
|
| 55 |
+
# Create a DataFrame from the input data
|
| 56 |
+
input_df = pd.DataFrame([data])
|
| 57 |
+
|
| 58 |
+
# Preprocess the data using the loaded OneHotEncoder
|
| 59 |
+
encoded_features = onehot_encoder.transform(input_df[categorical_cols]).toarray()
|
| 60 |
+
|
| 61 |
+
# Create a DataFrame for the encoded categorical features
|
| 62 |
+
encoded_df = pd.DataFrame(encoded_features, columns=onehot_encoder.get_feature_names_out(categorical_cols))
|
| 63 |
+
|
| 64 |
+
# Combine numerical and encoded categorical features
|
| 65 |
+
final_df = pd.concat([input_df[numerical_cols], encoded_df], axis=1)
|
| 66 |
+
|
| 67 |
+
# Make a prediction
|
| 68 |
+
prediction = model.predict(final_df)
|
| 69 |
+
|
| 70 |
+
# Format the response
|
| 71 |
+
response = {
|
| 72 |
+
"prediction": float(prediction[0])
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
return jsonify(response), 200
|
| 76 |
+
|
| 77 |
+
except KeyError as e:
|
| 78 |
+
return jsonify({"error": f"Missing feature in request: {e}"}), 400
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return jsonify({"error": str(e)}), 500
|
| 81 |
+
|
| 82 |
+
if __name__ == '__main__':
|
| 83 |
+
app.run(host='0.0.0.0', port=5000, debug=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask
|
| 2 |
+
gunicorn
|