Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -8
- app.py +65 -65
- requirements.txt +9 -1
Dockerfile
CHANGED
|
@@ -1,15 +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
|
| 7 |
COPY . .
|
| 8 |
|
| 9 |
-
# Install
|
| 10 |
-
RUN
|
| 11 |
|
| 12 |
-
# Define the command to
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
|
|
|
|
|
| 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
CHANGED
|
@@ -1,81 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import joblib
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
def load_model():
|
| 10 |
-
return joblib.load("super_kart_model_v1_0.joblib")
|
| 11 |
-
|
| 12 |
-
model = load_model()
|
| 13 |
-
|
| 14 |
-
# Streamlit UI for Super Kart Sales Prediction
|
| 15 |
-
st.title("Super Kart Product Sales Prediction App")
|
| 16 |
-
st.write("This tool predicts the total sales for a product based on store and product details.")
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 3", "Tier 2", "Tier 1"])
|
| 29 |
-
store_type = st.selectbox("Store Type", ["Grocery Store", "Supermarket Type1", "Supermarket Type2", "Supermarket Type3"]) # Add actual types from your data
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
sample = {
|
| 35 |
-
'Product_Weight':
|
| 36 |
-
'Product_Sugar_Content':
|
| 37 |
-
'Product_Allocated_Area':
|
| 38 |
-
'Product_Type':
|
| 39 |
-
'Product_MRP':
|
| 40 |
-
'Store_Establishment_Year':
|
| 41 |
-
'Store_Size':
|
| 42 |
-
'Store_Location_City_Type':
|
| 43 |
-
'Store_Type':
|
| 44 |
}
|
| 45 |
-
|
| 46 |
-
# Convert to DataFrame
|
| 47 |
features_df = pd.DataFrame([sample])
|
| 48 |
-
|
| 49 |
-
# Apply one-hot encoding for nominal columns (matching
|
| 50 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 51 |
-
|
| 52 |
-
# Apply ordinal encoding (based on
|
| 53 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 54 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 55 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 56 |
-
|
| 57 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 58 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 59 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Option 2: Call the backend Flask API (recommended if backend is hosted separately)
|
| 66 |
-
# Replace with your actual backend URL (e.g., from Hugging Face Space)
|
| 67 |
-
backend_url = "https://Hugo014/TotalSalesPredictionBackend.hf.space/v1/sales" # Update with real URL
|
| 68 |
-
try:
|
| 69 |
-
response = requests.post(backend_url, json=sample)
|
| 70 |
-
if response.status_code == 200:
|
| 71 |
-
result = response.json()
|
| 72 |
-
predicted_sales = result['Predicted Sales Total (in dollars)']
|
| 73 |
-
else:
|
| 74 |
-
st.error(f"Backend error: {response.status_code} - {response.text}")
|
| 75 |
-
predicted_sales = None
|
| 76 |
-
except Exception as e:
|
| 77 |
-
st.error(f"Error calling backend: {str(e)}")
|
| 78 |
-
predicted_sales = None
|
| 79 |
-
|
| 80 |
-
if predicted_sales is not None:
|
| 81 |
-
st.write(f"The predicted sales total for the product is ${predicted_sales:.2f}.")
|
|
|
|
| 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 path to match deployment structure)
|
| 11 |
+
model_path = "super_kart_model_v1_0.joblib"
|
| 12 |
+
try:
|
| 13 |
+
model = joblib.load(model_path)
|
| 14 |
+
print(f"Model loaded successfully from {model_path}")
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
raise FileNotFoundError(f"Model file not found at {model_path}. Ensure it's included in the deployment.")
|
| 17 |
|
| 18 |
+
# Define a route for the home page (GET request)
|
| 19 |
+
@super_kart_api.get('/')
|
| 20 |
+
def home():
|
| 21 |
+
"""
|
| 22 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 23 |
+
It returns a simple welcome message.
|
| 24 |
+
"""
|
| 25 |
+
return "Welcome to the Super Kart Price Prediction API!"
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Define an endpoint for single product sales prediction (POST request)
|
| 28 |
+
@super_kart_api.post('/v1/sales')
|
| 29 |
+
def predict_sales():
|
| 30 |
+
"""
|
| 31 |
+
This function handles POST requests to the '/v1/sales' endpoint.
|
| 32 |
+
It expects a JSON payload containing product and store details and returns
|
| 33 |
+
the predicted sales total as a JSON response.
|
| 34 |
+
"""
|
| 35 |
+
# Get the JSON data from the request body
|
| 36 |
+
input_data = request.get_json()
|
| 37 |
+
|
| 38 |
+
# Extract relevant features from the JSON data
|
| 39 |
+
# Note: Exclude Product_Id and Store_Id if they are not used in prediction
|
| 40 |
sample = {
|
| 41 |
+
'Product_Weight': input_data['Product_Weight'],
|
| 42 |
+
'Product_Sugar_Content': input_data['Product_Sugar_Content'],
|
| 43 |
+
'Product_Allocated_Area': input_data['Product_Allocated_Area'],
|
| 44 |
+
'Product_Type': input_data['Product_Type'],
|
| 45 |
+
'Product_MRP': input_data['Product_MRP'],
|
| 46 |
+
'Store_Establishment_Year': input_data['Store_Establishment_Year'],
|
| 47 |
+
'Store_Size': input_data['Store_Size'],
|
| 48 |
+
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 49 |
+
'Store_Type': input_data['Store_Type']
|
| 50 |
}
|
| 51 |
+
# Convert the extracted data into a Pandas DataFrame
|
|
|
|
| 52 |
features_df = pd.DataFrame([sample])
|
| 53 |
+
|
| 54 |
+
# Apply one-hot encoding for nominal columns (matching training)
|
| 55 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 56 |
+
|
| 57 |
+
# Apply ordinal encoding (based on provided orders)
|
| 58 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 59 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 60 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 61 |
+
|
| 62 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 63 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 64 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 65 |
+
|
| 66 |
+
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 67 |
+
predicted_sales = model.predict(features_df)[0]
|
| 68 |
+
|
| 69 |
+
# If your model predicts log(sales), uncomment and use this instead:
|
| 70 |
+
# predicted_log_sales = model.predict(features_df)[0]
|
| 71 |
+
# predicted_sales = np.exp(predicted_log_sales)
|
| 72 |
+
|
| 73 |
+
# Convert to Python float and round to 2 decimals
|
| 74 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 75 |
+
|
| 76 |
+
# Return the predicted sales total
|
| 77 |
+
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 78 |
|
| 79 |
+
# Run the app (for testing locally; remove or adjust for production)
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
super_kart_api.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,11 @@
|
|
| 1 |
pandas==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests==2.28.1
|
| 3 |
-
|
|
|
|
|
|
| 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]
|
| 11 |
+
streamlit==1.43.2
|