Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +80 -0
- requirements.txt +11 -0
- superkart_sales_prediction.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:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superkart_sales_forecast_api"]
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
superkart_sales_forecast_api = Flask("SuperKart Sales Forecast API")
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
model = joblib.load("superkart_sales_prediction.joblib", mmap_mode=None)
|
| 12 |
+
|
| 13 |
+
# ---------------- Home Route ----------------
|
| 14 |
+
@superkart_sales_forecast_api.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
"""
|
| 17 |
+
Handles GET requests to the root URL ('/').
|
| 18 |
+
Returns a welcome message.
|
| 19 |
+
"""
|
| 20 |
+
return "Welcome to the SuperKart Sales Forecast API!"
|
| 21 |
+
|
| 22 |
+
# ---------------- Online Forecast Route ----------------
|
| 23 |
+
@superkart_sales_forecast_api.post('/v1/sales')
|
| 24 |
+
def predict_sales_forecast():
|
| 25 |
+
"""
|
| 26 |
+
Handles POST requests to '/v1/sales'.
|
| 27 |
+
Expects a JSON payload of product-store details.
|
| 28 |
+
Returns the predicted sales total.
|
| 29 |
+
"""
|
| 30 |
+
try:
|
| 31 |
+
forecast_data = request.get_json()
|
| 32 |
+
|
| 33 |
+
sample = {
|
| 34 |
+
'Product_Weight': float(forecast_data['Product_Weight']),
|
| 35 |
+
'Product_MRP': float(forecast_data['Product_MRP']),
|
| 36 |
+
'Product_Sugar_Content': forecast_data['Product_Sugar_Content'],
|
| 37 |
+
'Product_Allocated_Area': float(forecast_data['Product_Allocated_Area']),
|
| 38 |
+
'Product_Type': forecast_data['Product_Type'],
|
| 39 |
+
'Store_Id': forecast_data['Store_Id'],
|
| 40 |
+
'Store_Establishment_Year': int(forecast_data['Store_Establishment_Year']),
|
| 41 |
+
'Store_Size': forecast_data['Store_Size'],
|
| 42 |
+
'Store_Location_City_Type': forecast_data['Store_Location_City_Type'],
|
| 43 |
+
'Store_Type': forecast_data['Store_Type']
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
input_df = pd.DataFrame([sample])
|
| 47 |
+
prediction = model.predict(input_df)
|
| 48 |
+
predicted_sales = round(float(prediction[0]), 2)
|
| 49 |
+
|
| 50 |
+
return jsonify({'predicted_product_store_sales_total': predicted_sales})
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return jsonify({'error': str(e)}), 500
|
| 53 |
+
|
| 54 |
+
# ---------------- Batch Forecast Route ----------------
|
| 55 |
+
@superkart_sales_forecast_api.post('/v1/salesbatch')
|
| 56 |
+
def predict_sales_forecast_batch():
|
| 57 |
+
"""
|
| 58 |
+
Handles POST requests to '/v1/salesbatch'.
|
| 59 |
+
Expects a CSV file with product-store rows.
|
| 60 |
+
Returns predicted sales totals per row.
|
| 61 |
+
"""
|
| 62 |
+
try:
|
| 63 |
+
file = request.files.get('file')
|
| 64 |
+
if file is None:
|
| 65 |
+
return jsonify({"error": "No file uploaded"}), 400
|
| 66 |
+
|
| 67 |
+
input_df = pd.read_csv(file)
|
| 68 |
+
|
| 69 |
+
# Predict using the trained model
|
| 70 |
+
predictions = model.predict(input_df)
|
| 71 |
+
input_df["predicted_product_store_sales_total"] = [round(float(x), 2) for x in predictions]
|
| 72 |
+
|
| 73 |
+
return jsonify(input_df.to_dict(orient="records"))
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return jsonify({'error': str(e)}), 500
|
| 76 |
+
|
| 77 |
+
# ---------------- Run the Flask App ----------------
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
superkart_sales_forecast_api.run(host='0.0.0.0', port=7860, debug=True)
|
| 80 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.28.1
|
| 10 |
+
uvicorn[standard]
|
| 11 |
+
streamlit==1.43.2
|
superkart_sales_prediction.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:45dc1553385a864a27012d09dca9f87661c514c2f4b6c322a85e3a89527da526
|
| 3 |
+
size 208249
|