Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +70 -0
- requirements.txt +16 -0
- superkart_sales_forecast_model_v1_0.joblib +3 -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 --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 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
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app
|
| 9 |
+
superkart_api = Flask("superkart_sales_api")
|
| 10 |
+
CORS(superkart_api)
|
| 11 |
+
|
| 12 |
+
# Load the trained model pipeline (preprocessing + model)
|
| 13 |
+
model = joblib.load("superkart_sales_forecast_model_v1_0.joblib")
|
| 14 |
+
|
| 15 |
+
# Health check route
|
| 16 |
+
@superkart_api.get('/')
|
| 17 |
+
def home():
|
| 18 |
+
return "✅ Welcome to the SuperKart Sales Prediction API"
|
| 19 |
+
|
| 20 |
+
# Prediction route
|
| 21 |
+
@superkart_api.post('/v1/predict')
|
| 22 |
+
def predict_sales():
|
| 23 |
+
try:
|
| 24 |
+
# Parse JSON payload
|
| 25 |
+
data = request.get_json()
|
| 26 |
+
print("Raw incoming data:", data)
|
| 27 |
+
|
| 28 |
+
# Validate expected fields
|
| 29 |
+
required_fields = [
|
| 30 |
+
'Product_Weight',
|
| 31 |
+
'Product_Sugar_Content',
|
| 32 |
+
'Product_Allocated_Area',
|
| 33 |
+
'Product_MRP',
|
| 34 |
+
'Store_Size',
|
| 35 |
+
'Store_Location_City_Type',
|
| 36 |
+
'Store_Type',
|
| 37 |
+
'Store_Age_Years',
|
| 38 |
+
'Product_Type_Category'
|
| 39 |
+
]
|
| 40 |
+
missing_fields = [f for f in required_fields if f not in data]
|
| 41 |
+
if missing_fields:
|
| 42 |
+
return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
|
| 43 |
+
|
| 44 |
+
# Convert and transform input
|
| 45 |
+
sample = {
|
| 46 |
+
'Product_Weight': float(data['Product_Weight']),
|
| 47 |
+
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 48 |
+
'Product_Allocated_Area_Log': np.log1p(float(data['Product_Allocated_Area'])), # transform here
|
| 49 |
+
'Product_MRP': float(data['Product_MRP']),
|
| 50 |
+
'Store_Size': data['Store_Size'],
|
| 51 |
+
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 52 |
+
'Store_Type': data['Store_Type'],
|
| 53 |
+
'Store_Age_Years': int(data['Store_Age_Years']),
|
| 54 |
+
'Product_Type_Category': data['Product_Type_Category']
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
input_df = pd.DataFrame([sample])
|
| 58 |
+
print("Transformed input for model:\n", input_df)
|
| 59 |
+
|
| 60 |
+
# Make prediction
|
| 61 |
+
prediction = model.predict(input_df).tolist()[0]
|
| 62 |
+
return jsonify({'Predicted_Sales': prediction})
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print("❌ Error during prediction:", str(e))
|
| 66 |
+
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
|
| 67 |
+
|
| 68 |
+
# Run the app (for local testing only)
|
| 69 |
+
if __name__ == '__main__':
|
| 70 |
+
superkart_api.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core libraries
|
| 2 |
+
pandas==2.2.2
|
| 3 |
+
numpy==2.0.2
|
| 4 |
+
scikit-learn==1.6.1
|
| 5 |
+
seaborn==0.13.2
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
xgboost==2.1.4
|
| 8 |
+
|
| 9 |
+
# Flask web server
|
| 10 |
+
flask==2.2.2
|
| 11 |
+
flask-cors==3.0.10
|
| 12 |
+
gunicorn==20.1.0
|
| 13 |
+
Werkzeug==2.2.2
|
| 14 |
+
|
| 15 |
+
# For API testing
|
| 16 |
+
requests==2.32.3
|
superkart_sales_forecast_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a2c321b5c4f98cc85c8d11df8310e8bee3f18ffba2a4f2026a55d079885d6764
|
| 3 |
+
size 63805219
|