Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +19 -0
- app.py +58 -0
- best_random_forest_model_v1.0.joblib +3 -0
- requirements.txt +11 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Expose the port the Flask app will run on
|
| 13 |
+
#EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 16 |
+
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 17 |
+
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 18 |
+
# - `app:sales_predictor_api`: Specifies the application entry point (app.py with Flask instance named sales_predictor_api)
|
| 19 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Initialize the Flask application
|
| 9 |
+
sales_predictor_api = Flask("Superkart Sales Predictor")
|
| 10 |
+
|
| 11 |
+
# Load the trained machine learning model
|
| 12 |
+
model_filename = joblib.load("best_random_forest_model_v1.0.joblib")
|
| 13 |
+
|
| 14 |
+
# Define a route for the home page (GET request)
|
| 15 |
+
@sales_predictor_api.get('/')
|
| 16 |
+
def home():
|
| 17 |
+
"""
|
| 18 |
+
This function handles GET requests to the root URL ('/') of the API.
|
| 19 |
+
It returns a simple welcome message.
|
| 20 |
+
"""
|
| 21 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 22 |
+
|
| 23 |
+
# Define an endpoint for single property prediction (POST request)
|
| 24 |
+
@sales_predictor_api.post('/v1/sales')
|
| 25 |
+
def predict_rental_price():
|
| 26 |
+
"""
|
| 27 |
+
This function handles POST requests to the '/v1/sales' endpoint.
|
| 28 |
+
It expects a JSON payload containing property details and returns
|
| 29 |
+
the predicted rental price as a JSON response.
|
| 30 |
+
"""
|
| 31 |
+
# Get the JSON data from the request body
|
| 32 |
+
sales_data = request.get_json()
|
| 33 |
+
|
| 34 |
+
# Extract relevant features from the JSON data
|
| 35 |
+
sample = {
|
| 36 |
+
'Product_Weight': sales_data['Product_Weight']
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 40 |
+
input_data = pd.DataFrame([sample])
|
| 41 |
+
|
| 42 |
+
# Make prediction (get log_price)
|
| 43 |
+
predicted_log_sales = model_filename.predict(input_data)[0]
|
| 44 |
+
|
| 45 |
+
# Calculate actual price
|
| 46 |
+
predicted_sales = np.exp(predicted_log_sales)
|
| 47 |
+
|
| 48 |
+
# Convert predicted_price to Python float
|
| 49 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 50 |
+
# The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
|
| 51 |
+
# When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
|
| 52 |
+
|
| 53 |
+
# Return the actual price
|
| 54 |
+
return jsonify({'predicted_sales': predicted_sales})
|
| 55 |
+
|
| 56 |
+
# Run the Flask application in debug mode if this script is executed directly
|
| 57 |
+
if __name__ == '__main__':
|
| 58 |
+
sales_predictor_api.run()
|
best_random_forest_model_v1.0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6ee8c6fca4440cad1bb44cc11a7e057a1f8c571ccd18927e1c0e3e06a27d79e
|
| 3 |
+
size 24974954
|
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
|