Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +77 -0
- requirements.txt +10 -0
- super_kart_model_v1_0.joblib +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11.13
|
| 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:super_kart_api"]
|
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib # For loading the serialized model
|
| 4 |
+
import pandas as pd # For data manipulation
|
| 5 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 6 |
+
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
super_kart_api = Flask("Super Kart Price Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model (updated to match Super Kart model file)
|
| 11 |
+
model = joblib.load("backend_files/super_kart_model_v1_0.joblib")
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page (GET request)
|
| 14 |
+
@super_kart_api.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
"""
|
| 17 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 18 |
+
It returns a simple welcome message.
|
| 19 |
+
"""
|
| 20 |
+
return "Welcome to the Super Kart Price Prediction API!"
|
| 21 |
+
|
| 22 |
+
# Define an endpoint for single product sales prediction (POST request)
|
| 23 |
+
@super_kart_api.post('/v1/sales')
|
| 24 |
+
def predict_sales():
|
| 25 |
+
"""
|
| 26 |
+
This function handles POST requests to the '/v1/sales' endpoint.
|
| 27 |
+
It expects a JSON payload containing product and store details and returns
|
| 28 |
+
the predicted sales total as a JSON response.
|
| 29 |
+
"""
|
| 30 |
+
# Get the JSON data from the request body
|
| 31 |
+
input_data = request.get_json()
|
| 32 |
+
|
| 33 |
+
# Extract relevant features from the JSON data
|
| 34 |
+
# Note: Exclude Product_Id and Store_Id if they are not used in prediction
|
| 35 |
+
sample = {
|
| 36 |
+
'Product_Weight': input_data['Product_Weight'],
|
| 37 |
+
'Product_Sugar_Content': input_data['Product_Sugar_Content'],
|
| 38 |
+
'Product_Allocated_Area': input_data['Product_Allocated_Area'],
|
| 39 |
+
'Product_Type': input_data['Product_Type'],
|
| 40 |
+
'Product_MRP': input_data['Product_MRP'],
|
| 41 |
+
'Store_Establishment_Year': input_data['Store_Establishment_Year'],
|
| 42 |
+
'Store_Size': input_data['Store_Size'],
|
| 43 |
+
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 44 |
+
'Store_Type': input_data['Store_Type']
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 48 |
+
features_df = pd.DataFrame([sample])
|
| 49 |
+
|
| 50 |
+
# Apply one-hot encoding for nominal columns (matching training)
|
| 51 |
+
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 52 |
+
|
| 53 |
+
# Apply ordinal encoding (based on provided orders)
|
| 54 |
+
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 55 |
+
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 56 |
+
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 57 |
+
|
| 58 |
+
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 59 |
+
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 60 |
+
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 61 |
+
|
| 62 |
+
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 63 |
+
predicted_sales = model.predict(features_df)[0]
|
| 64 |
+
|
| 65 |
+
# If your model predicts log(sales), uncomment and use this instead:
|
| 66 |
+
# predicted_log_sales = model.predict(features_df)[0]
|
| 67 |
+
# predicted_sales = np.exp(predicted_log_sales)
|
| 68 |
+
|
| 69 |
+
# Convert to Python float and round to 2 decimals
|
| 70 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 71 |
+
|
| 72 |
+
# Return the predicted sales total
|
| 73 |
+
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 74 |
+
|
| 75 |
+
# Run the app (for testing locally; remove or adjust for production)
|
| 76 |
+
if __name__ == '__main__':
|
| 77 |
+
super_kart_api.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==3.0.4
|
| 5 |
+
joblib==1.5.1
|
| 6 |
+
Werkzeug==3.1.3
|
| 7 |
+
flask==3.1.1
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
+
requests==2.28.1
|
| 10 |
+
uvicorn[standard]
|
super_kart_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6c63695fa45429ae2f8178467fb3d36e524c2bf14adf7d3daa8df6c67188ac6c
|
| 3 |
+
size 410265
|