Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +10 -9
- app.py +130 -108
- requirements.txt +8 -1
Dockerfile
CHANGED
|
@@ -1,16 +1,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
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
# Copy all files from the current directory
|
| 8 |
COPY . .
|
| 9 |
|
| 10 |
-
# Install
|
| 11 |
-
RUN
|
| 12 |
|
| 13 |
-
# Define the command to
|
| 14 |
-
#
|
| 15 |
-
|
| 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 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 -r be_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:app"]
|
| 17 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "be_app:super_kart_predictor_api"]
|
app.py
CHANGED
|
@@ -1,110 +1,132 @@
|
|
| 1 |
-
# import
|
|
|
|
| 2 |
# import pandas as pd
|
| 3 |
-
# import
|
| 4 |
-
|
| 5 |
-
# #
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# #
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
-
#
|
| 37 |
-
#
|
| 38 |
-
#
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
#
|
| 46 |
-
#
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
import pandas as pd
|
| 55 |
-
import
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import numpy as np
|
| 2 |
+
# import joblib
|
| 3 |
# import pandas as pd
|
| 4 |
+
# from flask import Flask, request, jsonify
|
| 5 |
+
|
| 6 |
+
# # Initialize the Flask app with a custom name
|
| 7 |
+
# super_kart_predictor_api = Flask("Super Kart Sales Predictor")
|
| 8 |
+
|
| 9 |
+
# # Load the trained model from the specified path
|
| 10 |
+
# # Make sure model_path variable is defined or replace with the actual path string
|
| 11 |
+
# model = joblib.load(model_path)
|
| 12 |
+
|
| 13 |
+
# # Define a route for the home page (GET request)
|
| 14 |
+
# @super_kart_predictor_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 Sales Predictor API!"
|
| 21 |
+
|
| 22 |
+
# # Define a route for predictions (POST request)
|
| 23 |
+
# @super_kart_predictor_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 commodity sales details and returns
|
| 28 |
+
# the predicted sales as a JSON response
|
| 29 |
+
# """
|
| 30 |
+
# # Get JSON data from the POST request
|
| 31 |
+
# superkart_data = request.get_json
|
| 32 |
+
# print(superkart_data)
|
| 33 |
+
|
| 34 |
+
# # Extract relevant features from the JSON payload into a dictionary
|
| 35 |
+
# sample = {
|
| 36 |
+
# 'Product_Weight': superkart_data['Product_Weight'],
|
| 37 |
+
# 'Product_Allocated_Area': superkart_data['Product_Allocated_Area'],
|
| 38 |
+
# 'Product_MRP': superkart_data['Product_MRP'],
|
| 39 |
+
# 'Store_Tenure': superkart_data['Store_Tenure'],
|
| 40 |
+
# 'Product_Category': superkart_data['Product_Category'],
|
| 41 |
+
# 'Product_Sugar_Content': superkart_data['Product_Sugar_Content'],
|
| 42 |
+
# 'Product_Type': superkart_data['Product_Type'],
|
| 43 |
+
# 'Store_Id': superkart_data['Store_Id'],
|
| 44 |
+
# 'Store_Size': superkart_data['Store_Size'],
|
| 45 |
+
# 'Store_Location_City_Type': superkart_data['Store_Location_City_Type'],
|
| 46 |
+
# 'Store_Type': superkart_data['Store_Type'],
|
| 47 |
+
# 'Perishability': superkart_data['Perishability']
|
| 48 |
+
# }
|
| 49 |
+
|
| 50 |
+
# # Create a DataFrame from the input dictionary for model compatibility
|
| 51 |
+
# input_data = pd.DataFrame([sample])
|
| 52 |
+
|
| 53 |
+
# # Predict sales price using the loaded model
|
| 54 |
+
# predicted_sales_price = model.predict(input_data)[0]
|
| 55 |
+
|
| 56 |
+
# # Convert predicted sales price back from log scale using exponential
|
| 57 |
+
# predicted_sales = np.exp(predicted_sales_price)
|
| 58 |
+
|
| 59 |
+
# # Convert the prediction to a float type with rounding to 2 decimal places
|
| 60 |
+
# predicted_sales = float(predicted_sales, 2)
|
| 61 |
+
|
| 62 |
+
# # Return the prediction as a JSON response
|
| 63 |
+
# return jsonify({"predicted_sales_price": predicted_sales})
|
| 64 |
+
import numpy as np
|
| 65 |
+
import joblib
|
| 66 |
import pandas as pd
|
| 67 |
+
from flask import Flask, request, jsonify
|
| 68 |
+
|
| 69 |
+
# Initialize the Flask app with a custom name
|
| 70 |
+
super_kart_predictor_api = Flask("Super Kart Sales Predictor")
|
| 71 |
+
|
| 72 |
+
# Load the trained model from the specified path
|
| 73 |
+
# Make sure model_path variable is defined or replace with the actual path string
|
| 74 |
+
model_path = "super_kart_prediction_gbr_tuned_model_v1_0.joblib"
|
| 75 |
+
model = joblib.load(model_path)
|
| 76 |
+
|
| 77 |
+
# Define a route for the home page (GET request)
|
| 78 |
+
@super_kart_predictor_api.get('/')
|
| 79 |
+
def home():
|
| 80 |
+
"""
|
| 81 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 82 |
+
It returns a simple welcome message.
|
| 83 |
+
"""
|
| 84 |
+
return "Welcome to the Super Kart Sales Predictor API!"
|
| 85 |
+
|
| 86 |
+
# Define a route for predictions (POST request)
|
| 87 |
+
@super_kart_predictor_api.post("/v1/sales")
|
| 88 |
+
def predict_sales():
|
| 89 |
+
"""
|
| 90 |
+
This function handles POST requests to the /v1/sales endpoint.
|
| 91 |
+
It expects a JSON payload containing commodity sales details and returns
|
| 92 |
+
the predicted sales as a JSON response
|
| 93 |
+
"""
|
| 94 |
+
# Get JSON data from the POST request
|
| 95 |
+
superkart_data = request.get_json()
|
| 96 |
+
print(f"\nIncoming request data: \n{superkart_data}\n")
|
| 97 |
+
|
| 98 |
+
# Extract relevant features from the JSON payload into a dictionary
|
| 99 |
+
sample = {
|
| 100 |
+
'Product_Weight': superkart_data['Product_Weight'],
|
| 101 |
+
'Product_Allocated_Area': superkart_data['Product_Allocated_Area'],
|
| 102 |
+
'Product_MRP': superkart_data['Product_MRP'],
|
| 103 |
+
'Product_Sugar_Content': superkart_data['Product_Sugar_Content'],
|
| 104 |
+
'Product_Type': superkart_data['Product_Type'],
|
| 105 |
+
'Product_Category': superkart_data['Product_Category'],
|
| 106 |
+
'Store_Id': superkart_data['Store_Id'],
|
| 107 |
+
'Store_Establishment_Year': superkart_data['Store_Establishment_Year'],
|
| 108 |
+
'Store_Size': superkart_data['Store_Size'],
|
| 109 |
+
'Store_Location_City_Type': superkart_data['Store_Location_City_Type'],
|
| 110 |
+
'Store_Type': superkart_data['Store_Type'],
|
| 111 |
+
'Store_Tenure': superkart_data['Store_Tenure'],
|
| 112 |
+
'Perishability': superkart_data['Perishability'],
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# Create a DataFrame from the input dictionary for model compatibility
|
| 116 |
+
input_data = pd.DataFrame([sample])
|
| 117 |
+
|
| 118 |
+
# Predict sales price using the loaded model
|
| 119 |
+
predicted_sales_price = model.predict(input_data)[0]
|
| 120 |
+
|
| 121 |
+
# Convert the prediction to a float type with rounding to 3 decimal places
|
| 122 |
+
predicted_sales = round(predicted_sales_price, 3)
|
| 123 |
+
|
| 124 |
+
print(f"\nPredicted Sales Price: {predicted_sales}\n")
|
| 125 |
+
|
| 126 |
+
# Return the prediction as a JSON response
|
| 127 |
+
return jsonify({"predicted_sales_price": predicted_sales})
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# if __name__ == '__main__':
|
| 132 |
+
# super_kart_predictor_api.run()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 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==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]
|