Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +8 -9
- app.py +54 -72
- backend/Dockerfile +17 -0
- backend/SuperKart_Model_V1_0.joblib +3 -0
- backend/app.py +74 -0
- backend/requirements.txt +20 -0
- requirements.txt +0 -13
Dockerfile
CHANGED
|
@@ -1,17 +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
|
| 7 |
COPY . .
|
| 8 |
|
| 9 |
-
# Install dependencies
|
| 10 |
-
RUN
|
| 11 |
|
| 12 |
-
# Define the command to
|
| 13 |
-
|
| 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_api"]
|
| 17 |
|
|
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.9 installed
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Set the working directory inside the container to /app
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
+
# Install Python dependencies listed in requirements.txt
|
| 11 |
+
RUN pip3 install -r requirements.txt
|
| 12 |
|
| 13 |
+
# Define the command to run the Streamlit app on port 8501 and make it accessible externally
|
| 14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# NOTE: Disable XSRF protection for easier external access in order to make batch predictions
|
app.py
CHANGED
|
@@ -1,74 +1,56 @@
|
|
| 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 |
-
from flask_cors import CORS
|
| 7 |
-
|
| 8 |
-
# Initialize the Flask application
|
| 9 |
-
superkart_api = Flask("SuperKart Revenue Predictor")
|
| 10 |
-
CORS(superkart_api)
|
| 11 |
-
|
| 12 |
-
#model path needs to be updated to root once this is pushed
|
| 13 |
-
model_path = "deployment_files/SuperKart_Model_V1_0.joblib"
|
| 14 |
-
|
| 15 |
-
# Load the trained machine learning model
|
| 16 |
-
model = joblib.load("model_path")
|
| 17 |
-
|
| 18 |
-
# Health check route
|
| 19 |
-
@superkart_api.get('/')
|
| 20 |
-
def home():
|
| 21 |
-
return "Welcome to SuperKart Sales Prediction"
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Parse JSON payload
|
| 29 |
-
data = request.get_json()
|
| 30 |
-
print("Raw incoming data:", data)
|
| 31 |
-
|
| 32 |
-
# Validate expected fields
|
| 33 |
-
required_fields = [
|
| 34 |
-
'Product_Weight',
|
| 35 |
-
'Product_Sugar_Content',
|
| 36 |
-
'Product_Allocated_Area',
|
| 37 |
-
'Product_MRP',
|
| 38 |
-
'Store_Size',
|
| 39 |
-
'Store_Location_City_Type',
|
| 40 |
-
'Store_Type',
|
| 41 |
-
'Store_Age_Years',
|
| 42 |
-
'Product_Type_Category'
|
| 43 |
-
]
|
| 44 |
-
missing_fields = [f for f in required_fields if f not in data]
|
| 45 |
-
if missing_fields:
|
| 46 |
-
return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
|
| 47 |
-
|
| 48 |
-
# Convert and transform input
|
| 49 |
-
sample = {
|
| 50 |
-
'Product_Weight': float(data['Product_Weight']),
|
| 51 |
-
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 52 |
-
'Product_Allocated_Area_Log': np.log1p(float(data['Product_Allocated_Area'])), # log-transform
|
| 53 |
-
'Product_MRP': float(data['Product_MRP']),
|
| 54 |
-
'Store_Size': data['Store_Size'],
|
| 55 |
-
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 56 |
-
'Store_Type': data['Store_Type'],
|
| 57 |
-
'Store_Age_Years': int(data['Store_Age_Years']),
|
| 58 |
-
'Product_Type_Category': data['Product_Type_Category']
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
input_df = pd.DataFrame([sample])
|
| 62 |
-
print("Transformed input for model:\n", input_df)
|
| 63 |
-
|
| 64 |
-
# Make prediction
|
| 65 |
-
prediction = model.predict(input_df).tolist()[0]
|
| 66 |
-
return jsonify({'Predicted_Sales': prediction})
|
| 67 |
-
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print("Error during prediction:", str(e))
|
| 70 |
-
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load the trained model
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
return joblib.load("SuperKart_Model_V1_0.joblib")
|
| 11 |
+
|
| 12 |
+
model = load_model()
|
| 13 |
+
|
| 14 |
+
# Streamlit UI for Price Prediction
|
| 15 |
+
st.title("SuperKart Revenue Prediction App")
|
| 16 |
+
st.write("This tool predicts the sales revenue listing based on the given details.")
|
| 17 |
+
|
| 18 |
+
st.subheader("Enter the listing details:")
|
| 19 |
+
|
| 20 |
+
# Collect user input
|
| 21 |
+
Product_Type = st.selectbox("Product_Type", ["Fruits and Vegetables ", "Snack Foods", "Frozen Foods","Dairy","Household","Baking Goods","Canned","Health and Hygiene","Meat","Soft Drinks","Breads","Hard Drinks","Others","Starchy Foods ","Breakfast","Seafood"])
|
| 22 |
+
Product_Weight = st.number_input("Product_Weight", min_value=0.0, value=12.66)
|
| 23 |
+
Product_MRP = st.number_input("Product_MRP",min_value=0.0, value=100.0)
|
| 24 |
+
Product_Allocated_Area = st.number_input("Product_Allocated_Area", min_value=0.0, value=100.0)
|
| 25 |
+
Product_Sugar_Content = st.selectbox("Product_Sugar_Content", ["Low Sugar", "No Sugar", "Regular", "reg"])
|
| 26 |
+
Store_Type = st.selectbox("Store_Type", ["Supermarket Type2 ", "Supermarket Type1","Departmental Store","Food Mart"])
|
| 27 |
+
Store_Location_City_Type = st.selectbox("Store_Location_City_Type", ["Tier 2", "Tier 1","Tier 3"])
|
| 28 |
+
Store_Id = st.selectbox("Store_Id",["OUT004","OUT003","OUT002","OUT001"])
|
| 29 |
+
Store_Establishment_Year = st.number_input(
|
| 30 |
+
"Store_Establishment_Year",
|
| 31 |
+
min_value=1900,
|
| 32 |
+
max_value=2025,
|
| 33 |
+
step=1,
|
| 34 |
+
value=2000,
|
| 35 |
+
format='%d'
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Convert user input into a DataFrame
|
| 39 |
+
input_data = pd.DataFrame([{
|
| 40 |
+
'Product_Type': Product_Type,
|
| 41 |
+
'Product_Weight': Product_Weight,
|
| 42 |
+
'Product_MRP': Product_MRP,
|
| 43 |
+
'Product_Allocated_Area': Product_Allocated_Area,
|
| 44 |
+
'Product_Sugar_Content': Product_Sugar_Content,
|
| 45 |
+
'Store_Type': Store_Type,
|
| 46 |
+
'Store_Location_City_Type': Store_Location_City_Type,
|
| 47 |
+
'Store_Id': Store_Id,
|
| 48 |
+
'Store_Establishment_Year': Store_Establishment_Year,
|
| 49 |
+
}])
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Predict button
|
| 53 |
+
if st.button("Predict"):
|
| 54 |
+
prediction = model.predict(input_data)
|
| 55 |
+
#st.write(f"The predicted revnue is ${np.exp(prediction)[0]:.2f}.")
|
| 56 |
+
st.write(f"The predicted revenue is ${prediction[0]:.2f}.")
|
backend/Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_api"]
|
| 17 |
+
|
backend/SuperKart_Model_V1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:06234b86b6bdeea8fa7023e0d50f4e8e378d395609985f79b17a456f7311bc27
|
| 3 |
+
size 211281
|
backend/app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from flask_cors import CORS
|
| 7 |
+
|
| 8 |
+
# Initialize the Flask application
|
| 9 |
+
superkart_api = Flask("SuperKart Revenue Predictor")
|
| 10 |
+
CORS(superkart_api)
|
| 11 |
+
|
| 12 |
+
#model path needs to be updated to root once this is pushed
|
| 13 |
+
model_path = "deployment_files/Backend/SuperKart_Model_V1_0.joblib"
|
| 14 |
+
|
| 15 |
+
# Load the trained machine learning model
|
| 16 |
+
model = joblib.load("model_path")
|
| 17 |
+
|
| 18 |
+
# Health check route
|
| 19 |
+
@superkart_api.get('/')
|
| 20 |
+
def home():
|
| 21 |
+
return "Welcome to SuperKart Sales Prediction"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Prediction route
|
| 25 |
+
@superkart_api.post('/v1/predict')
|
| 26 |
+
def predict_sales():
|
| 27 |
+
try:
|
| 28 |
+
# Parse JSON payload
|
| 29 |
+
data = request.get_json()
|
| 30 |
+
print("Raw incoming data:", data)
|
| 31 |
+
|
| 32 |
+
# Validate expected fields
|
| 33 |
+
required_fields = [
|
| 34 |
+
'Product_Weight',
|
| 35 |
+
'Product_Sugar_Content',
|
| 36 |
+
'Product_Allocated_Area',
|
| 37 |
+
'Product_MRP',
|
| 38 |
+
'Store_Size',
|
| 39 |
+
'Store_Location_City_Type',
|
| 40 |
+
'Store_Type',
|
| 41 |
+
'Store_Age_Years',
|
| 42 |
+
'Product_Type_Category'
|
| 43 |
+
]
|
| 44 |
+
missing_fields = [f for f in required_fields if f not in data]
|
| 45 |
+
if missing_fields:
|
| 46 |
+
return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
|
| 47 |
+
|
| 48 |
+
# Convert and transform input
|
| 49 |
+
sample = {
|
| 50 |
+
'Product_Weight': float(data['Product_Weight']),
|
| 51 |
+
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 52 |
+
'Product_Allocated_Area_Log': np.log1p(float(data['Product_Allocated_Area'])), # log-transform
|
| 53 |
+
'Product_MRP': float(data['Product_MRP']),
|
| 54 |
+
'Store_Size': data['Store_Size'],
|
| 55 |
+
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 56 |
+
'Store_Type': data['Store_Type'],
|
| 57 |
+
'Store_Age_Years': int(data['Store_Age_Years']),
|
| 58 |
+
'Product_Type_Category': data['Product_Type_Category']
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
input_df = pd.DataFrame([sample])
|
| 62 |
+
print("Transformed input for model:\n", input_df)
|
| 63 |
+
|
| 64 |
+
# Make prediction
|
| 65 |
+
prediction = model.predict(input_df).tolist()[0]
|
| 66 |
+
return jsonify({'Predicted_Sales': prediction})
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print("Error during prediction:", str(e))
|
| 70 |
+
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
|
| 71 |
+
|
| 72 |
+
# Run the app (for local testing only)
|
| 73 |
+
if __name__ == '__main__':
|
| 74 |
+
superkart_api.run(debug=True)
|
backend/requirements.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 12 |
+
|
| 13 |
+
# Flask web server
|
| 14 |
+
flask==2.2.2
|
| 15 |
+
flask-cors==3.0.10
|
| 16 |
+
gunicorn==20.1.0
|
| 17 |
+
Werkzeug==2.2.2
|
| 18 |
+
|
| 19 |
+
# For API testing
|
| 20 |
+
requests==2.32.3
|
requirements.txt
CHANGED
|
@@ -3,18 +3,5 @@ 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
|
| 12 |
|
| 13 |
-
# Flask web server
|
| 14 |
-
flask==2.2.2
|
| 15 |
-
flask-cors==3.0.10
|
| 16 |
-
gunicorn==20.1.0
|
| 17 |
-
Werkzeug==2.2.2
|
| 18 |
-
|
| 19 |
-
# For API testing
|
| 20 |
-
requests==2.32.3
|
|
|
|
| 3 |
scikit-learn==1.6.1
|
| 4 |
xgboost==2.1.4
|
| 5 |
joblib==1.4.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
streamlit==1.43.2
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|