Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +69 -0
- requirements.txt +12 -0
- superkart_storesales_prediction_model_v1_0.joblib +3 -0
- superkartapp.py +71 -0
Dockerfile
ADDED
|
@@ -0,0 +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 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 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:store_sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
|
| 3 |
+
# Initialize Flask app
|
| 4 |
+
store_sales_predictor_api = Flask("Super Kart Store Sales Predictor Application")
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
try:
|
| 8 |
+
superkart_model = joblib.load("superkart_storesales_prediction_model_v1_0.joblib") #superkart_deployment_files/
|
| 9 |
+
print("Model loaded successfully.")
|
| 10 |
+
except FileNotFoundError:
|
| 11 |
+
print("Error: 'superkart_storesales_prediction_model_v1_0.joblib' not found. Please train and save the model first.")
|
| 12 |
+
superkart_model = None
|
| 13 |
+
|
| 14 |
+
# Define home page for app
|
| 15 |
+
@store_sales_predictor_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
return "Welcome to Super Kart Store Sales Predictor Application",200
|
| 18 |
+
|
| 19 |
+
# Health check endpoint
|
| 20 |
+
@store_sales_predictor_api.get('/healthcheck')
|
| 21 |
+
def health_check():
|
| 22 |
+
"""
|
| 23 |
+
Returns a 200 status code and a JSON response to indicate the service is healthy.
|
| 24 |
+
"""
|
| 25 |
+
return jsonify({"status": "healthy"}), 200
|
| 26 |
+
|
| 27 |
+
# Define prediction form page for app
|
| 28 |
+
@store_sales_predictor_api.post('/v1/predict')
|
| 29 |
+
def predict_sales_price():
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
Handles prediction requests.
|
| 33 |
+
Expects a JSON payload with 'features'.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
# Get data from the POST request
|
| 37 |
+
payload = request.get_json()
|
| 38 |
+
|
| 39 |
+
# Extract Relevant Features from Payload
|
| 40 |
+
app_features = {
|
| 41 |
+
"Product_Weight": payload["Product_Weight"],
|
| 42 |
+
"Product_Sugar_Content": payload["Product_Sugar_Content"],
|
| 43 |
+
"Product_Allocated_Area": payload["Product_Allocated_Area"],
|
| 44 |
+
"Product_Type": payload["Product_Type"],
|
| 45 |
+
"Product_MRP": payload["Product_MRP"],
|
| 46 |
+
"Store_Establishment_Year": payload["Store_Establishment_Year"],
|
| 47 |
+
"Store_Location_City_Type": payload["Store_Location_City_Type"],
|
| 48 |
+
"Store_Type": payload["Store_Type"],
|
| 49 |
+
"Store_Size": payload["Store_Size"]}
|
| 50 |
+
# store app_features in dataframe
|
| 51 |
+
input_data = pd.DataFrame([app_features])
|
| 52 |
+
|
| 53 |
+
# Make prediction and get store sales
|
| 54 |
+
predicted_sales = superkart_model.predict(input_data)[0]
|
| 55 |
+
|
| 56 |
+
# calculate actual value
|
| 57 |
+
predicted_sales_value = np.exp(predicted_sales)
|
| 58 |
+
|
| 59 |
+
# convert value to python float
|
| 60 |
+
predicted_sales_value = round(float(predicted_sales_value),2)
|
| 61 |
+
|
| 62 |
+
return jsonify({"predicted store sales total-": predicted_sales_value}), 200
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return jsonify({"error": str(e)}), 500
|
| 66 |
+
|
| 67 |
+
# Run the Flask app in debug mode
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
datetime
|
| 4 |
+
seaborn
|
| 5 |
+
scikit-learn==1.6.1
|
| 6 |
+
xgboost==2.1.4
|
| 7 |
+
joblib==1.4.2
|
| 8 |
+
Werkzeug==2.2.2
|
| 9 |
+
flask==2.2.2
|
| 10 |
+
gunicorn==20.1.0
|
| 11 |
+
requests==2.28.1
|
| 12 |
+
uvicorn[standard]
|
superkart_storesales_prediction_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d2b143d1a035b38f23cb8fd447f1ca593c390c7f9992a88c46dd824db88fb452
|
| 3 |
+
size 1433507
|
superkartapp.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
|
| 3 |
+
# Initialize Flask app
|
| 4 |
+
store_sales_predictor_api = Flask("Super Kart Store Sales Predictor Application")
|
| 5 |
+
|
| 6 |
+
# Load the trained model
|
| 7 |
+
try:
|
| 8 |
+
superkart_model = joblib.load("superkart_storesales_prediction_model_v1_0.joblib") #superkart_deployment_files/
|
| 9 |
+
print("Model loaded successfully.")
|
| 10 |
+
except FileNotFoundError:
|
| 11 |
+
print("Error: 'superkart_storesales_prediction_model_v1_0.joblib' not found. Please train and save the model first.")
|
| 12 |
+
superkart_model = None
|
| 13 |
+
|
| 14 |
+
# Define home page for app
|
| 15 |
+
@store_sales_predictor_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
return "Welcome to Super Kart Store Sales Predictor Application",200
|
| 18 |
+
|
| 19 |
+
# Health check endpoint
|
| 20 |
+
@store_sales_predictor_api.get('/healthcheck')
|
| 21 |
+
def health_check():
|
| 22 |
+
"""
|
| 23 |
+
Returns a 200 status code and a JSON response to indicate the service is healthy.
|
| 24 |
+
"""
|
| 25 |
+
return jsonify({"status": "healthy"}), 200
|
| 26 |
+
|
| 27 |
+
# Define prediction form page for app
|
| 28 |
+
@store_sales_predictor_api.post('/v1/predict')
|
| 29 |
+
def predict_sales_price():
|
| 30 |
+
|
| 31 |
+
"""
|
| 32 |
+
Handles prediction requests.
|
| 33 |
+
Expects a JSON payload with 'features'.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
# Get data from the POST request
|
| 37 |
+
payload = request.get_json()
|
| 38 |
+
|
| 39 |
+
# Extract Relevant Features from Payload
|
| 40 |
+
app_features = {
|
| 41 |
+
"Product_Weight": payload["Product_Weight"],
|
| 42 |
+
"Product_Sugar_Content": payload["Product_Sugar_Content"],
|
| 43 |
+
"Product_Allocated_Area": payload["Product_Allocated_Area"],
|
| 44 |
+
"Product_Type": payload["Product_Type"],
|
| 45 |
+
"Product_MRP": payload["Product_MRP"],
|
| 46 |
+
"Store_Establishment_Year": payload["Store_Establishment_Year"].
|
| 47 |
+
"Store_Location_City_Type": payload["Store_Location_City_Type"],
|
| 48 |
+
"Store_Type": payload["Store_Type"],
|
| 49 |
+
"Store_Size": payload["Store_Size"]
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# store app_features in dataframe
|
| 53 |
+
input_data= pd.DataFrame([app_features])
|
| 54 |
+
|
| 55 |
+
# Make prediction and get store sales
|
| 56 |
+
predicted_sales = superkart_model.predict(input_data)[0]
|
| 57 |
+
|
| 58 |
+
# calculate actual value
|
| 59 |
+
predicted_sales_value = np.exp(predicted_sales)
|
| 60 |
+
|
| 61 |
+
# convert value to python float
|
| 62 |
+
predicted_sales_value = round(float(predicted_sales_value),2)
|
| 63 |
+
|
| 64 |
+
return jsonify({"predicted store sales total-": predicted_sales_value}), 200
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return jsonify({"error": str(e)}), 500
|
| 68 |
+
|
| 69 |
+
# Run the Flask app in debug mode
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
app.run(debug=True)
|