Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +23 -0
- app.py +65 -0
- requirements.txt +10 -0
- sales_forecasting_model_v1_0.joblib +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight Python base image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy requirements file first (for efficient Docker caching)
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the rest of the application files
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose port (Flask default is 5000)
|
| 17 |
+
EXPOSE 5000
|
| 18 |
+
|
| 19 |
+
# Set environment variables
|
| 20 |
+
ENV PYTHONUNBUFFERED=1
|
| 21 |
+
|
| 22 |
+
# Run the Flask app
|
| 23 |
+
CMD ["python", "backend_files/app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app
|
| 6 |
+
app = Flask("Sales Forecasting")
|
| 7 |
+
|
| 8 |
+
# Load the trained Sales Forecasting prediction model
|
| 9 |
+
model = joblib.load("sales_forecasting_model_v1_0.joblib")
|
| 10 |
+
|
| 11 |
+
# Define categorical mapping dictionary
|
| 12 |
+
replaceStruct = {
|
| 13 |
+
"Product_Sugar_Content": {"Low Sugar": 1, "Regular": 2, "No Sugar": 3, "reg": 4},
|
| 14 |
+
"Product_Type": {
|
| 15 |
+
"Fruits and Vegetables": 1, "Snack Foods": 2, "Frozen Foods": 3, "Dairy": 4,
|
| 16 |
+
"Household": 5, "Baking Goods": 6, "Canned": 7, "Health and Hygiene": 8,
|
| 17 |
+
"Meat": 9, "Soft Drinks": 10, "Bread": 11, "Breads": 12, "Hard Drinks": 13,
|
| 18 |
+
"Others": 14, "Starchy Foods": 15, "Breakfast": 16, "Seafood": 17
|
| 19 |
+
},
|
| 20 |
+
"Store_Id": {"OUT001": 1, "OUT002": 2, "OUT003": 3, "OUT004": 4},
|
| 21 |
+
"Store_Size": {"Medium": 1, "High": 2, "Low": 3, "Small": 4},
|
| 22 |
+
"Store_Location_City_Type": {"Tier 1": 1, "Tier 2": 2, "Tier 3": 3},
|
| 23 |
+
"Store_Type": {"Departmental Store": 1, "Supermarket Type1": 2, "Supermarket Type2": 3, "Food Mart": 4},
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Home route
|
| 28 |
+
@app.get('/')
|
| 29 |
+
def home():
|
| 30 |
+
return "Welcome to the Sales Forecasting API!"
|
| 31 |
+
|
| 32 |
+
# Prediction endpoint
|
| 33 |
+
@app.post('/v1/sales_forecast')
|
| 34 |
+
def predict_sales():
|
| 35 |
+
# Get JSON data from the request
|
| 36 |
+
user_data = request.get_json()
|
| 37 |
+
|
| 38 |
+
# Extract relevant customer features from the input data
|
| 39 |
+
sample = {
|
| 40 |
+
"Product_Sugar_Content": user_data["Product_Sugar_Content"], # e.g. "Low Sugar"
|
| 41 |
+
"Product_Type": user_data["Product_Type"], # e.g. "Snack Foods"
|
| 42 |
+
"Store_Id": user_data["Store_Id"], # e.g. "OUT002"
|
| 43 |
+
"Store_Size": user_data["Store_Size"], # e.g. "Medium"
|
| 44 |
+
"Store_Location_City_Type": user_data["Store_Location_City_Type"], # e.g. "Tier 2"
|
| 45 |
+
"Store_Type": user_data["Store_Type"], # e.g. "Supermarket Type1"
|
| 46 |
+
"Product_Weight": user_data["Product_Weight"], # numeric field
|
| 47 |
+
"Product_Price": user_data["Product_Price"], # numeric field
|
| 48 |
+
"Store_Area": user_data["Store_Area"], # numeric field
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Convert to DataFrame and apply mapping
|
| 52 |
+
input_data = pd.DataFrame([sample]).replace(replaceStruct)
|
| 53 |
+
|
| 54 |
+
# Make a Sales Forecasting prediction
|
| 55 |
+
prediction = model.predict(input_data)[0]
|
| 56 |
+
|
| 57 |
+
# Prepare readable response
|
| 58 |
+
prediction_label = f"Prediction of Weekly Sales is {prediction:.2f}"
|
| 59 |
+
|
| 60 |
+
# Return JSON
|
| 61 |
+
return jsonify({'Prediction': prediction_label})
|
| 62 |
+
|
| 63 |
+
# Run the Flask app in debug mode
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy==2.0.2
|
| 2 |
+
pandas==2.2.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
matplotlib==3.10.0
|
| 5 |
+
seaborn==0.13.2
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
xgboost==2.1.4
|
| 8 |
+
requests==2.32.3
|
| 9 |
+
huggingface_hub==0.30.1
|
| 10 |
+
flask==3.0.3
|
sales_forecasting_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a9dbc1f0f4b0a4ca0a91845f5d0ffe7bfbff4317687c9ea2cf8ff10e61910387
|
| 3 |
+
size 51054546
|