Upload folder using huggingface_hub
Browse files- Dockerfile +10 -10
- app.py +89 -67
- requirements.txt +9 -1
Dockerfile
CHANGED
|
@@ -1,16 +1,16 @@
|
|
| 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 --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_sales_prediction_api"]
|
app.py
CHANGED
|
@@ -1,69 +1,91 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Import necessary libraries
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib # For loading the serialized model
|
| 5 |
+
import pandas as pd # For data manipulation
|
| 6 |
+
from flask import Flask, request, jsonify # For creating the Flask API
|
| 7 |
+
|
| 8 |
+
# Initialize the Flask application
|
| 9 |
+
superkart_sales_prediction_api = Flask("SuperKart Sales Predictor")
|
| 10 |
+
|
| 11 |
+
# Load the trained machine learning model
|
| 12 |
+
model = joblib.load("superkart_sales_prediction_model_v1_0.joblib")
|
| 13 |
+
|
| 14 |
+
# Define the required feature columns
|
| 15 |
+
REQUIRED_FEATURES = [
|
| 16 |
+
'Product_Weight',
|
| 17 |
+
'Product_Sugar_Content',
|
| 18 |
+
'Product_Allocated_Area',
|
| 19 |
+
'Product_MRP',
|
| 20 |
+
'Store_Size',
|
| 21 |
+
'Store_Location_City_Type',
|
| 22 |
+
'Store_Type',
|
| 23 |
+
'Product_Id_Group',
|
| 24 |
+
'Store_Age',
|
| 25 |
+
'Product_Type_Category',
|
| 26 |
+
'Store_Id'
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# Define a route for the home page (GET request)
|
| 30 |
+
@superkart_sales_prediction_api.get('/')
|
| 31 |
+
def home():
|
| 32 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 33 |
+
|
| 34 |
+
# Define an endpoint for single product prediction (POST request)
|
| 35 |
+
@superkart_sales_prediction_api.post('/v1/sales')
|
| 36 |
+
def predict_sales():
|
| 37 |
try:
|
| 38 |
+
data = request.get_json()
|
| 39 |
+
|
| 40 |
+
# Validate input fields
|
| 41 |
+
missing_fields = [field for field in REQUIRED_FEATURES if field not in data]
|
| 42 |
+
if missing_fields:
|
| 43 |
+
return jsonify({
|
| 44 |
+
"error": "Missing required fields",
|
| 45 |
+
"missing_fields": missing_fields
|
| 46 |
+
}), 400
|
| 47 |
+
|
| 48 |
+
# Prepare input data
|
| 49 |
+
input_data = pd.DataFrame([{key: data[key] for key in REQUIRED_FEATURES}])
|
| 50 |
+
|
| 51 |
+
# Make prediction
|
| 52 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 53 |
+
|
| 54 |
+
return jsonify({'Sales': prediction})
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
+
return jsonify({"error": "Internal server error", "message": str(e)}), 500
|
| 58 |
+
|
| 59 |
+
# Define an endpoint for batch prediction (POST request)
|
| 60 |
+
@superkart_sales_prediction_api.post('/v1/salesbatch')
|
| 61 |
+
def predict_sales_batch():
|
| 62 |
+
try:
|
| 63 |
+
file = request.files.get('file')
|
| 64 |
+
if file is None:
|
| 65 |
+
return jsonify({"error": "CSV file not provided"}), 400
|
| 66 |
+
|
| 67 |
+
input_data = pd.read_csv(file)
|
| 68 |
+
|
| 69 |
+
# Check for missing columns
|
| 70 |
+
missing_cols = [col for col in REQUIRED_FEATURES if col not in input_data.columns]
|
| 71 |
+
if missing_cols:
|
| 72 |
+
return jsonify({
|
| 73 |
+
"error": "Missing columns in CSV file",
|
| 74 |
+
"missing_columns": missing_cols
|
| 75 |
+
}), 400
|
| 76 |
+
|
| 77 |
+
# Predict
|
| 78 |
+
predictions = model.predict(input_data[REQUIRED_FEATURES]).tolist()
|
| 79 |
+
predicted_sales = [round(float(pred), 2) for pred in predictions]
|
| 80 |
+
|
| 81 |
+
ids = input_data['id'].tolist() if 'id' in input_data.columns else list(range(1, len(predicted_sales) + 1))
|
| 82 |
+
output_dict = dict(zip(ids, predicted_sales))
|
| 83 |
+
|
| 84 |
+
return jsonify(output_dict)
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return jsonify({"error": "Internal server error", "message": str(e)}), 500
|
| 88 |
+
|
| 89 |
+
# Run the API
|
| 90 |
+
if __name__ == '__main__':
|
| 91 |
+
superkart_sales_prediction_api.run(debug=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
|
|
| 1 |
numpy==2.0.2
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|