Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +98 -0
- gradient_tuned.joblib +3 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set the working directory inside the container
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all files from the current directory to the container's working directory
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Expose API port
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
# Start Flask API with Gunicorn (4 workers, bind to 7860)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "superkart_backend.app:sales_prediction_api"]
|
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# Initialize Flask App
|
| 8 |
+
# -------------------------------
|
| 9 |
+
sales_prediction_api = Flask('Superkart Sales Prediction')
|
| 10 |
+
|
| 11 |
+
# -------------------------------
|
| 12 |
+
# Load the trained Gradient Boosting model
|
| 13 |
+
# (Make sure the path matches where you saved the .joblib file)
|
| 14 |
+
# -------------------------------
|
| 15 |
+
model = joblib.load("gradient_tuned.joblib")
|
| 16 |
+
|
| 17 |
+
# -------------------------------
|
| 18 |
+
# Root endpoint (Health check)
|
| 19 |
+
# -------------------------------
|
| 20 |
+
@sales_prediction_api.get("/")
|
| 21 |
+
def home():
|
| 22 |
+
"""Simple health check endpoint"""
|
| 23 |
+
return 'Welcome to the SuperKart Sales Prediction API'
|
| 24 |
+
|
| 25 |
+
# -------------------------------
|
| 26 |
+
# Single Prediction Endpoint
|
| 27 |
+
# -------------------------------
|
| 28 |
+
@sales_prediction_api.post("/v1/salesdata")
|
| 29 |
+
def predict_sales():
|
| 30 |
+
"""
|
| 31 |
+
This endpoint accepts a single JSON object with product & store details
|
| 32 |
+
and returns the predicted sales total.
|
| 33 |
+
"""
|
| 34 |
+
# Parse incoming JSON
|
| 35 |
+
sales_data = request.get_json()
|
| 36 |
+
|
| 37 |
+
# Create a sample dictionary with only the features used by the model
|
| 38 |
+
sample = {
|
| 39 |
+
'Product_Weight': sales_data['Product_Weight'],
|
| 40 |
+
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
|
| 41 |
+
'Product_MRP': sales_data['Product_MRP'],
|
| 42 |
+
'Product_Sugar_Content': sales_data['Product_Sugar_Content'], # fixed key
|
| 43 |
+
'Product_Type': sales_data['Product_Type'],
|
| 44 |
+
'Store_Id': sales_data['Store_Id'],
|
| 45 |
+
'Store_Size': sales_data['Store_Size'],
|
| 46 |
+
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Convert the dictionary into a pandas DataFrame (model expects a DataFrame)
|
| 50 |
+
input_data = pd.DataFrame([sample])
|
| 51 |
+
|
| 52 |
+
# Predict sales (model is trained on log of sales, so output is log-scale)
|
| 53 |
+
predicted_log_sales = model.predict(input_data)[0]
|
| 54 |
+
|
| 55 |
+
# Convert back from log scale to original sales units
|
| 56 |
+
predicted_sales = np.exp(predicted_log_sales)
|
| 57 |
+
|
| 58 |
+
# Round the sales value to 2 decimal places for readability
|
| 59 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 60 |
+
|
| 61 |
+
# Return prediction as JSON
|
| 62 |
+
return jsonify({'Product_Store_Sales_Total': predicted_sales})
|
| 63 |
+
|
| 64 |
+
# -------------------------------
|
| 65 |
+
# Batch Prediction Endpoint
|
| 66 |
+
# -------------------------------
|
| 67 |
+
@sales_prediction_api.post("/v1/salesdatabatch")
|
| 68 |
+
def predict_sales_batch():
|
| 69 |
+
"""
|
| 70 |
+
This endpoint accepts a CSV file with multiple product records
|
| 71 |
+
and returns predicted sales totals for all rows.
|
| 72 |
+
"""
|
| 73 |
+
# Get uploaded CSV file from request
|
| 74 |
+
file = request.files['file']
|
| 75 |
+
|
| 76 |
+
# Load the CSV into a pandas DataFrame
|
| 77 |
+
input_data = pd.read_csv(file)
|
| 78 |
+
|
| 79 |
+
# Predict sales for each row (drop unused columns before prediction)
|
| 80 |
+
predicted_log_sales = model.predict(
|
| 81 |
+
input_data.drop(['Product_Id', 'Store_Establishment_Year', 'Store_Type'], axis=1)
|
| 82 |
+
).tolist()
|
| 83 |
+
|
| 84 |
+
# Convert predictions back to original sales units
|
| 85 |
+
predicted_sales = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_sales]
|
| 86 |
+
|
| 87 |
+
# Attach predictions to Product IDs for clarity
|
| 88 |
+
product_ids = input_data['Product_Id'].tolist()
|
| 89 |
+
output_dict = dict(zip(product_ids, predicted_sales))
|
| 90 |
+
|
| 91 |
+
# Return predictions as JSON
|
| 92 |
+
return jsonify(output_dict)
|
| 93 |
+
|
| 94 |
+
# -------------------------------
|
| 95 |
+
# Run Flask App
|
| 96 |
+
# -------------------------------
|
| 97 |
+
if __name__ == '__main__':
|
| 98 |
+
sales_prediction_api.run(debug=True)
|
gradient_tuned.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:42b4d70fdf4fd9609e15aec0d5d8426f4a6b1d04b8d958f98de8c2227cfd097a
|
| 3 |
+
size 1650258
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.32.3
|
| 10 |
+
uvicorn[standard]==0.32.0
|