Upload 4 files
Browse files- Dockerfile +28 -0
- app.py +92 -0
- final_superkart_sales_forecaster.joblib +3 -0
- requirements.txt +11 -0
Dockerfile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
# Install system dependencies in a single layer
|
| 4 |
+
RUN apt-get update \
|
| 5 |
+
&& apt-get install -y build-essential libpq-dev \
|
| 6 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 7 |
+
|
| 8 |
+
# Create a non-root user
|
| 9 |
+
RUN useradd -m -u 1000 user
|
| 10 |
+
|
| 11 |
+
# Set the working directory
|
| 12 |
+
WORKDIR /app
|
| 13 |
+
|
| 14 |
+
# Copy requirements first to leverage caching
|
| 15 |
+
COPY ./requirements.txt requirements.txt
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# Copy the rest of the application files (including model)
|
| 19 |
+
COPY --chown=user:user . /app
|
| 20 |
+
|
| 21 |
+
# Switch to non-root user
|
| 22 |
+
USER user
|
| 23 |
+
|
| 24 |
+
# Expose Hugging Face Spaces port
|
| 25 |
+
EXPOSE 7860
|
| 26 |
+
|
| 27 |
+
# Start the Flask app
|
| 28 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Backendapp.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1PjbKz6_4cQUuHck90e_e2dJXA7oWpc9B
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# -*- coding: utf-8 -*-
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import pandas as pd
|
| 14 |
+
import numpy as np
|
| 15 |
+
import joblib
|
| 16 |
+
from flask import Flask, request, jsonify
|
| 17 |
+
|
| 18 |
+
# --- Define Constants and Global Model ---
|
| 19 |
+
|
| 20 |
+
# Model File Name (Must match the file created in Rubric 5)
|
| 21 |
+
MODEL_FILE = 'final_superkart_sales_forecaster.joblib'
|
| 22 |
+
|
| 23 |
+
# The raw features expected by the pipeline. This order is CRITICAL.
|
| 24 |
+
FEATURE_COLUMNS = [
|
| 25 |
+
'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area',
|
| 26 |
+
'Product_Type', 'Product_MRP', 'Store_Size', 'Store_Location_City_Type',
|
| 27 |
+
'Store_Type', 'Store_Establishment_Year', 'Product_Id'
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Load the model once when the server starts
|
| 31 |
+
model = None
|
| 32 |
+
try:
|
| 33 |
+
# Load the best model pipeline (which includes the preprocessor and the Tuned XGBoost)
|
| 34 |
+
model = joblib.load(MODEL_FILE)
|
| 35 |
+
print(f"Model loaded successfully from {MODEL_FILE}")
|
| 36 |
+
except FileNotFoundError:
|
| 37 |
+
print(f"ERROR: Model file {MODEL_FILE} not found. Ensure it is in the deployment folder.")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
# This error typically happens due to mismatched joblib/scikit-learn/xgboost versions.
|
| 40 |
+
print(f"CRITICAL ERROR: Failed to load model. Check library versions. Error: {e}")
|
| 41 |
+
|
| 42 |
+
# --- Flask Application Setup ---
|
| 43 |
+
|
| 44 |
+
app = Flask(__name__)
|
| 45 |
+
|
| 46 |
+
@app.route('/predict', methods=['POST'])
|
| 47 |
+
def predict():
|
| 48 |
+
"""
|
| 49 |
+
Receives JSON data for prediction, processes it, and returns the sales forecast.
|
| 50 |
+
Expected input: A JSON array of objects, where each object contains all FEATURE_COLUMNS.
|
| 51 |
+
"""
|
| 52 |
+
if model is None:
|
| 53 |
+
return jsonify({"error": "Model is not loaded. Cannot perform prediction."}), 503
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Get JSON data from the request
|
| 57 |
+
data = request.get_json(force=True)
|
| 58 |
+
|
| 59 |
+
# 1. Convert JSON list of dictionaries to DataFrame
|
| 60 |
+
df = pd.DataFrame(data)
|
| 61 |
+
|
| 62 |
+
# 2. Reorder columns to match the trained pipeline's expected input order
|
| 63 |
+
# This is CRITICAL because the ColumnTransformer relies on input order.
|
| 64 |
+
df = df[FEATURE_COLUMNS]
|
| 65 |
+
|
| 66 |
+
# 3. Predict on the log scale (pipeline handles all preprocessing)
|
| 67 |
+
log_predictions = model.predict(df)
|
| 68 |
+
|
| 69 |
+
# 4. Inverse transform predictions to the original sales scale
|
| 70 |
+
# np.expm1 is the inverse of np.log1p
|
| 71 |
+
predictions = np.expm1(log_predictions).tolist()
|
| 72 |
+
|
| 73 |
+
return jsonify({"predictions": predictions})
|
| 74 |
+
|
| 75 |
+
except KeyError as e:
|
| 76 |
+
# Handle cases where required features are missing
|
| 77 |
+
return jsonify({"error": f"Missing required feature: {e}. All features in FEATURE_COLUMNS must be present."}), 400
|
| 78 |
+
except Exception as e:
|
| 79 |
+
# Catch unexpected errors during prediction
|
| 80 |
+
return jsonify({"error": f"An unexpected error occurred during prediction: {e}"}), 500
|
| 81 |
+
|
| 82 |
+
@app.route('/health', methods=['GET'])
|
| 83 |
+
def health_check():
|
| 84 |
+
"""A simple health check endpoint to verify the API and model are running."""
|
| 85 |
+
# This endpoint is what Hugging Face checks for to confirm the app is live.
|
| 86 |
+
status = "OK" if model is not None else "ERROR: Model not loaded"
|
| 87 |
+
return jsonify({"status": status, "model_ready": model is not None})
|
| 88 |
+
|
| 89 |
+
# Standard run command for Docker/Hugging Face compatibility
|
| 90 |
+
if __name__ == '__main__':
|
| 91 |
+
# Use 0.0.0.0 for Docker compatibility. We MUST use port 7860 for Hugging Face Spaces.
|
| 92 |
+
app.run(debug=False, host='0.0.0.0', port=7860)
|
final_superkart_sales_forecaster.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:77d5eb12ff255ae1d4c22f8efef44724894e450fd3065a43704c879d5ad4d0df
|
| 3 |
+
size 184720
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Flask and core utilities
|
| 2 |
+
flask==3.0.3
|
| 3 |
+
joblib==1.4.0
|
| 4 |
+
|
| 5 |
+
# Numerical and Data Handling
|
| 6 |
+
numpy==1.26.4
|
| 7 |
+
pandas==2.2.2
|
| 8 |
+
|
| 9 |
+
# Machine Learning Libraries (exact versions used in training)
|
| 10 |
+
scikit-learn==1.6.1
|
| 11 |
+
xgboost==2.1.4
|