Upload folder using huggingface_hub
Browse files- Dockerfile +12 -9
- app.py +90 -48
- requirements.txt +8 -0
Dockerfile
CHANGED
|
@@ -1,16 +1,19 @@
|
|
| 1 |
-
# Use a minimal base image with Python 3.9 installed
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
| 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 run the Streamlit app on port 8501 and make it accessible externally
|
| 14 |
-
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
+
# Install wget
|
| 4 |
+
RUN apt-get update && apt-get install -y wget && rm -rf /var/lib/apt/lists/*
|
| 5 |
+
|
| 6 |
+
# Set the working directory inside the container
|
| 7 |
WORKDIR /app
|
| 8 |
|
| 9 |
+
# Copy all files from the current directory to the container's working directory
|
| 10 |
COPY . .
|
| 11 |
|
| 12 |
+
# Install dependencies from the requirements file without using cache to reduce image size
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
|
|
|
|
|
|
|
|
| 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`: Runs the Flask app instance named 'sales_predictor_api' from the 'app.py' file
|
| 19 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_predictor_api"]
|
app.py
CHANGED
|
@@ -1,48 +1,90 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
sales_predictor_api = Flask("SuperKart Sales Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained machine learning model
|
| 11 |
+
model = joblib.load("sales_predictor.joblib")
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page (GET request)
|
| 14 |
+
@sales_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 SuperKart Sales Prediction API!"
|
| 21 |
+
|
| 22 |
+
# Define an endpoint for single product prediction (POST request)
|
| 23 |
+
@sales_predictor_api.post('/v1/predict')
|
| 24 |
+
def predict_sales():
|
| 25 |
+
"""
|
| 26 |
+
This function handles POST requests to the '/v1/predict' endpoint.
|
| 27 |
+
It expects a JSON payload containing product and store details and returns
|
| 28 |
+
the predicted sales as a JSON response.
|
| 29 |
+
"""
|
| 30 |
+
# Get the JSON data from the request body
|
| 31 |
+
product_store_data = request.get_json()
|
| 32 |
+
|
| 33 |
+
# Extract relevant features from the JSON data
|
| 34 |
+
sample = {
|
| 35 |
+
'Product_Weight': product_store_data['Product_Weight'],
|
| 36 |
+
'Product_Sugar_Content': product_store_data['Product_Sugar_Content'],
|
| 37 |
+
'Product_Allocated_Area': product_store_data['Product_Allocated_Area'],
|
| 38 |
+
'Product_Type': product_store_data['Product_Type'],
|
| 39 |
+
'Product_MRP': product_store_data['Product_MRP'],
|
| 40 |
+
'Store_Establishment_Year_Before_2005': product_store_data['Store_Establishment_Year_Before_2005'],
|
| 41 |
+
'Store_Size': product_store_data['Store_Size'],
|
| 42 |
+
'Store_Location_City_Type': product_store_data['Store_Location_City_Type'],
|
| 43 |
+
'Store_Type': product_store_data['Store_Type']
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Convert the extracted data into a Pandas DataFrame
|
| 47 |
+
input_data = pd.DataFrame([sample])
|
| 48 |
+
|
| 49 |
+
# Make prediction
|
| 50 |
+
predicted_sales = model.predict(input_data)[0]
|
| 51 |
+
|
| 52 |
+
# Convert predicted_sales to Python float
|
| 53 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Return the predicted sales
|
| 57 |
+
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Define an endpoint for batch prediction (POST request) - This endpoint is not functional with the current model
|
| 61 |
+
# as the model is trained on individual product-store combinations, not batch data with IDs.
|
| 62 |
+
# @sales_predictor_api.post('/v1/predictbatch')
|
| 63 |
+
# def predict_sales_batch():
|
| 64 |
+
# """
|
| 65 |
+
# This function handles POST requests to the '/v1/predictbatch' endpoint.
|
| 66 |
+
# It expects a CSV file containing product and store details for multiple entries
|
| 67 |
+
# and returns the predicted sales as a dictionary in the JSON response.
|
| 68 |
+
# """
|
| 69 |
+
# # Get the uploaded CSV file from the request
|
| 70 |
+
# file = request.files['file']
|
| 71 |
+
|
| 72 |
+
# # Read the CSV file into a Pandas DataFrame
|
| 73 |
+
# input_data = pd.read_csv(file)
|
| 74 |
+
|
| 75 |
+
# # Make predictions for all entries in the DataFrame
|
| 76 |
+
# predicted_sales = model.predict(input_data).tolist()
|
| 77 |
+
|
| 78 |
+
# # For batch prediction with IDs, you would need an ID column in the input data
|
| 79 |
+
# # and a way to associate predictions with IDs.
|
| 80 |
+
# # Since the current model predicts for individual entries based on features,
|
| 81 |
+
# # this batch endpoint is not directly applicable without further modifications
|
| 82 |
+
# # to handle input data with IDs and return predictions associated with those IDs.
|
| 83 |
+
|
| 84 |
+
# # Returning a placeholder response
|
| 85 |
+
# return jsonify({"message": "Batch prediction endpoint is not yet implemented for this model."})
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Run the Flask application in debug mode if this script is executed directly
|
| 89 |
+
if __name__ == '__main__':
|
| 90 |
+
sales_predictor_api.run(debug=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,11 @@
|
|
| 1 |
pandas==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests==2.28.1
|
|
|
|
| 3 |
streamlit==1.43.2
|
|
|
|
| 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
|