Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend_files/Dockerfile +16 -0
- backend_files/app.py +61 -0
- backend_files/requirements.txt +11 -0
- superkart_prediction_model_v1_0.joblib +3 -0
backend_files/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 --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_revenue_predictor_api"]
|
backend_files/app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib # For loading the trained ML model
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
sales_forecast_api = Flask("SuperKart Sales Forecast API")
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
model = joblib.load("/content/drive/MyDrive/AIML Practice/Python Basics/deployment_files/superkart_prediction_model_v1_0.joblib") # Ensure this file is present in the same directory
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page (GET request)
|
| 14 |
+
@sales_forecast_api.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
"""
|
| 17 |
+
Handles GET requests to the root URL.
|
| 18 |
+
Returns a welcome message.
|
| 19 |
+
"""
|
| 20 |
+
return "Welcome to the SuperKart Sales Forecast API!"
|
| 21 |
+
|
| 22 |
+
# Define a route for single prediction (POST request)
|
| 23 |
+
@sales_forecast_api.post('/v1/forecast')
|
| 24 |
+
def predict_sales():
|
| 25 |
+
"""
|
| 26 |
+
Handles POST requests to the '/v1/forecast' endpoint.
|
| 27 |
+
Accepts product and store details in JSON format and returns the predicted sales revenue.
|
| 28 |
+
"""
|
| 29 |
+
# Get JSON data from request body
|
| 30 |
+
data = request.get_json()
|
| 31 |
+
|
| 32 |
+
# Extract features for prediction
|
| 33 |
+
sample = {
|
| 34 |
+
'Product_Id': data['Product_Id'],
|
| 35 |
+
'Product_Weight': data['Product_Weight'],
|
| 36 |
+
'Product_Sugar_Content': data['Product_Sugar_Content'],
|
| 37 |
+
'Product_Allocated_Area': data['Product_Allocated_Area'],
|
| 38 |
+
'Product_Type': data['Product_Type'],
|
| 39 |
+
'Product_MRP': data['Product_MRP'],
|
| 40 |
+
'Store_Id': data['Store_Id'],
|
| 41 |
+
'Store_Establishment_Year': data['Store_Establishment_Year'],
|
| 42 |
+
'Store_Size': data['Store_Size'],
|
| 43 |
+
'Store_Location_City_Type': data['Store_Location_City_Type'],
|
| 44 |
+
'Store_Type': data['Store_Type']
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Convert to DataFrame
|
| 48 |
+
input_df = pd.DataFrame([sample])
|
| 49 |
+
|
| 50 |
+
# Make prediction
|
| 51 |
+
predicted_sales = model.predict(input_df)[0]
|
| 52 |
+
|
| 53 |
+
# Round off and convert to float
|
| 54 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 55 |
+
|
| 56 |
+
# Return response
|
| 57 |
+
return jsonify({'Predicted_Sales_Revenue': predicted_sales})
|
| 58 |
+
|
| 59 |
+
# Run the Flask app
|
| 60 |
+
if __name__ == '__main__':
|
| 61 |
+
sales_forecast_api.run(debug=True)
|
backend_files/requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
superkart_prediction_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1e9e75f29c42c32542be1eca1d4a1aa60740872cc59fe7f99923a514b9d95e6e
|
| 3 |
+
size 207342
|