Upload folder using huggingface_hub
Browse files- Dockerfile +23 -0
- app.py +85 -0
- model.joblib +3 -0
- preprocessor.joblib +3 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# 1. Base Image: Use a lightweight Python 3.9 image
|
| 3 |
+
FROM python:3.9-slim-buster
|
| 4 |
+
|
| 5 |
+
# 2. Set the working directory inside the container
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
# 3. Copy and install requirements first (for better caching)
|
| 9 |
+
COPY requirements.txt .
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# 4. Copy the rest of the application files
|
| 13 |
+
# This copies app.py, model.joblib, and preprocessor.joblib
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# 5. Expose the port the app runs on
|
| 17 |
+
EXPOSE 5000
|
| 18 |
+
|
| 19 |
+
# 6. Define the command to run the application
|
| 20 |
+
# We use gunicorn to serve the Flask app (app:superkart_api)
|
| 21 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "app:superkart_api"]
|
| 22 |
+
|
| 23 |
+
print("✅ Dockerfile created in /content/drive/MyDrive/SuperKart/backend_files/")
|
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Import necessary libraries
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib # For loading the serialized artifacts
|
| 5 |
+
import pandas as pd # For data manipulation
|
| 6 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Define the directory where the app and models are
|
| 10 |
+
# This is good practice for finding the files
|
| 11 |
+
APP_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
+
|
| 13 |
+
# Initialize Flask app
|
| 14 |
+
superkart_api = Flask("superkart_predictor")
|
| 15 |
+
|
| 16 |
+
# --- Load the preprocessor AND the model ---
|
| 17 |
+
try:
|
| 18 |
+
preprocessor_path = os.path.join(APP_DIR, "preprocessor.joblib")
|
| 19 |
+
model_path = os.path.join(APP_DIR, "model.joblib")
|
| 20 |
+
|
| 21 |
+
preprocessor = joblib.load(preprocessor_path)
|
| 22 |
+
model = joblib.load(model_path)
|
| 23 |
+
print("✅ Preprocessor and model loaded successfully.")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"❌ Error loading artifacts: {e}")
|
| 26 |
+
preprocessor = None
|
| 27 |
+
model = None
|
| 28 |
+
|
| 29 |
+
# Define a route for the home page
|
| 30 |
+
@superkart_api.get('/')
|
| 31 |
+
def home():
|
| 32 |
+
# A simple welcome message
|
| 33 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 34 |
+
|
| 35 |
+
# Define an endpoint to predict sales
|
| 36 |
+
@superkart_api.post('/v1/predict')
|
| 37 |
+
def predict_sales():
|
| 38 |
+
|
| 39 |
+
if preprocessor is None or model is None:
|
| 40 |
+
return jsonify({"error": "Model or preprocessor not loaded"}), 500
|
| 41 |
+
|
| 42 |
+
# Get JSON data from the request
|
| 43 |
+
data = request.get_json()
|
| 44 |
+
|
| 45 |
+
# --- Extract features from the input data ---
|
| 46 |
+
# The keys (e.g., 'Product_Weight') MUST match the JSON sent
|
| 47 |
+
# The columns MUST match what the preprocessor was trained on.
|
| 48 |
+
try:
|
| 49 |
+
sample = {
|
| 50 |
+
'Product_Weight': data['Product_Weight'],
|
| 51 |
+
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 52 |
+
'Product_Allocated_Area': data['Product_Allocated_Area'],
|
| 53 |
+
'Product_Type': data['Product_Type'],
|
| 54 |
+
'Product_MRP': data['Product_MRP'],
|
| 55 |
+
'Store_Size': data['Store_Size'],
|
| 56 |
+
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 57 |
+
'Store_Type': data['Store_Type'],
|
| 58 |
+
'Store_Age': data['Store_Age']
|
| 59 |
+
}
|
| 60 |
+
except KeyError as e:
|
| 61 |
+
return jsonify({"error": f"Missing key in JSON payload: {e}"}), 400
|
| 62 |
+
|
| 63 |
+
# Convert the extracted data into a 1-row DataFrame
|
| 64 |
+
input_data = pd.DataFrame([sample])
|
| 65 |
+
|
| 66 |
+
# --- Make a prediction ---
|
| 67 |
+
try:
|
| 68 |
+
# 1. Transform the raw input data using the preprocessor
|
| 69 |
+
processed_data = preprocessor.transform(input_data)
|
| 70 |
+
|
| 71 |
+
# 2. Make a prediction using the trained model
|
| 72 |
+
prediction = model.predict(processed_data).tolist()[0]
|
| 73 |
+
|
| 74 |
+
# Return the prediction as a JSON response
|
| 75 |
+
# We also round the prediction to 2 decimal places
|
| 76 |
+
return jsonify({'predicted_sales': round(prediction, 2)})
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return jsonify({"error": f"Error during prediction: {str(e)}"}), 500
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Run the Flask app
|
| 83 |
+
if __name__ == '__main__':
|
| 84 |
+
# Use 0.0.0.0 to make it accessible outside the container/notebook
|
| 85 |
+
superkart_api.run(host='0.0.0.0', port=5000, debug=True)
|
model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:54aeda2ba7d11b49c070bf6e15f0a4dfdd1c4dace2a4a1a1c6805edab5a2bcf4
|
| 3 |
+
size 472809
|
preprocessor.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6cc6ae7565037569fd6c94e73a4f64b865e0a29d078fb06f17603aed40112b2d
|
| 3 |
+
size 3583
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
joblib==1.4.2
|
| 5 |
+
flask==2.2.2
|
| 6 |
+
gunicorn==20.1.0
|
| 7 |
+
|
| 8 |
+
print("✅ requirements.txt file created in /content/drive/MyDrive/SuperKart/backend_files/")
|