Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +1 -1
- app.py +58 -74
Dockerfile
CHANGED
|
@@ -13,4 +13,4 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
| 13 |
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
# - `app:superkart_api`: Runs the Flask app (Flask app instance is named `superkart_api` inside app.py)
|
| 16 |
-
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:
|
|
|
|
| 13 |
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
# - `app:superkart_api`: Runs the Flask app (Flask app instance is named `superkart_api` inside app.py)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superkart_api"]
|
app.py
CHANGED
|
@@ -1,96 +1,80 @@
|
|
| 1 |
|
| 2 |
-
import os
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import joblib
|
| 6 |
from flask import Flask, request, jsonify
|
| 7 |
-
from flask_cors import CORS
|
| 8 |
|
| 9 |
# ------------------------------------------------------------
|
| 10 |
-
#
|
| 11 |
# ------------------------------------------------------------
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Factory function to create and configure the Flask app.
|
| 15 |
-
"""
|
| 16 |
-
app = Flask("superkart_api")
|
| 17 |
-
CORS(app)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Health check route
|
| 27 |
-
# --------------------------------------------------------
|
| 28 |
-
@app.route("/", methods=["GET"])
|
| 29 |
-
def health_check():
|
| 30 |
-
"""
|
| 31 |
-
Returns a simple message to verify the API is running.
|
| 32 |
-
"""
|
| 33 |
-
return jsonify({"status": "ok", "message": "SuperKart Sales Prediction API is active"})
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
data = request.get_json()
|
| 44 |
|
| 45 |
-
if not data:
|
| 46 |
-
return jsonify({"error": "No input received"}), 400
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
]
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
if missing:
|
| 62 |
-
return jsonify({"error": f"Missing
|
| 63 |
-
|
| 64 |
-
try:
|
| 65 |
-
# Preprocess input data into model format
|
| 66 |
-
processed = pd.DataFrame([{
|
| 67 |
-
"Product_Weight": float(data["Product_Weight"]),
|
| 68 |
-
"Product_Sugar_Content": data["Product_Sugar_Content"],
|
| 69 |
-
"Product_Allocated_Area_Log": np.log1p(float(data["Product_Allocated_Area"])),
|
| 70 |
-
"Product_MRP": float(data["Product_MRP"]),
|
| 71 |
-
"Store_Size": data["Store_Size"],
|
| 72 |
-
"Store_Location_City_Type": data["Store_Location_City_Type"],
|
| 73 |
-
"Store_Type": data["Store_Type"],
|
| 74 |
-
"Store_Age_Years": int(data["Store_Age_Years"]),
|
| 75 |
-
"Product_Type_Category": data["Product_Type_Category"]
|
| 76 |
-
}])
|
| 77 |
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
"status": "success"
|
| 83 |
-
})
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
app.run(debug=True)
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
from flask import Flask, request, jsonify
|
|
|
|
| 6 |
|
| 7 |
# ------------------------------------------------------------
|
| 8 |
+
# Application Initialization
|
| 9 |
# ------------------------------------------------------------
|
| 10 |
+
# Create Flask app instance
|
| 11 |
+
superkart_api = Flask("superkart_sales_prediction_api")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Load the serialized (trained) model
|
| 14 |
+
# Ensure the model file path is correct relative to this script
|
| 15 |
+
MODEL_PATH = "superkart_product_sales_forecasting_model_v1_0.joblib"
|
| 16 |
+
model = joblib.load(MODEL_PATH)
|
| 17 |
|
| 18 |
+
# ------------------------------------------------------------
|
| 19 |
+
# Routes
|
| 20 |
+
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@superkart_api.route("/", methods=["GET"])
|
| 23 |
+
def home():
|
| 24 |
+
"""
|
| 25 |
+
Health check or root endpoint.
|
| 26 |
+
Returns a simple confirmation message.
|
| 27 |
+
"""
|
| 28 |
+
return "SuperKart Sales Prediction API is running successfully."
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
@superkart_api.route("/v1/predict", methods=["POST"])
|
| 32 |
+
def predict_sales():
|
| 33 |
+
"""
|
| 34 |
+
Predicts sales based on product and store attributes.
|
| 35 |
+
Expects a JSON payload with the required feature fields.
|
| 36 |
+
"""
|
| 37 |
+
try:
|
| 38 |
+
# Parse incoming JSON data
|
| 39 |
+
data = request.get_json(force=True)
|
| 40 |
+
|
| 41 |
+
# Define required input fields
|
| 42 |
+
required_fields = [
|
| 43 |
+
'Product_Weight',
|
| 44 |
+
'Product_Sugar_Content',
|
| 45 |
+
'Product_Allocated_Area',
|
| 46 |
+
'Product_MRP',
|
| 47 |
+
'Store_Size',
|
| 48 |
+
'Store_Location_City_Type',
|
| 49 |
+
'Store_Type',
|
| 50 |
+
'Product_Id_char',
|
| 51 |
+
'Store_Age_Years',
|
| 52 |
+
'Product_Type_Category'
|
| 53 |
]
|
| 54 |
|
| 55 |
+
# Check for missing fields
|
| 56 |
+
missing = [f for f in required_fields if f not in data]
|
| 57 |
if missing:
|
| 58 |
+
return jsonify({"error": f"Missing input fields: {missing}"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Prepare input as DataFrame for model
|
| 61 |
+
input_df = pd.DataFrame([{f: data[f] for f in required_fields}])
|
| 62 |
|
| 63 |
+
# Generate prediction
|
| 64 |
+
prediction = float(model.predict(input_df)[0])
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Return prediction result
|
| 67 |
+
return jsonify({
|
| 68 |
+
"Predicted_Sales": round(prediction, 2),
|
| 69 |
+
"status": "success"
|
| 70 |
+
})
|
| 71 |
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return jsonify({"error": f"Prediction failed: {str(e)}"}), 500
|
| 74 |
|
| 75 |
|
| 76 |
+
# ------------------------------------------------------------
|
| 77 |
+
# Entry Point
|
| 78 |
+
# ------------------------------------------------------------
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
superkart_api.run(host="0.0.0.0", port=5000, debug=True)
|
|
|