Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +75 -0
- requirements.txt +11 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-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:product_sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app
|
| 6 |
+
product_sales_predictor_api = Flask("Product Sales Predictor")
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
model = joblib.load("../model/product_sales_prediction_model_v1_0.joblib")
|
| 10 |
+
|
| 11 |
+
# Home endpoint
|
| 12 |
+
@product_sales_predictor_api.get('/')
|
| 13 |
+
def home():
|
| 14 |
+
return "Welcome to the SuperKart Product Sales Prediction API!"
|
| 15 |
+
|
| 16 |
+
# Single product prediction
|
| 17 |
+
@product_sales_predictor_api.post('/v1/product')
|
| 18 |
+
def predict_single_product():
|
| 19 |
+
try:
|
| 20 |
+
# Get JSON data from request
|
| 21 |
+
product_info = request.get_json()
|
| 22 |
+
|
| 23 |
+
# Extract features
|
| 24 |
+
prod_input = {
|
| 25 |
+
'Product_Weight': product_info['Product_Weight'],
|
| 26 |
+
'Product_Sugar_Content': product_info['Product_Sugar_Content'],
|
| 27 |
+
'Product_Allocated_Area': product_info['Product_Allocated_Area'],
|
| 28 |
+
'Product_Type': product_info['Product_Type'],
|
| 29 |
+
'Product_MRP': product_info['Product_MRP'],
|
| 30 |
+
'Store_Id': product_info['Store_Id'],
|
| 31 |
+
'Store_Establishment_Year': product_info['Store_Establishment_Year'],
|
| 32 |
+
'Store_Size': product_info['Store_Size'],
|
| 33 |
+
'Store_Location_City_Type': product_info['Store_Location_City_Type'],
|
| 34 |
+
'Store_Type': product_info['Store_Type']
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Convert to DataFrame and predict
|
| 38 |
+
input_data = pd.DataFrame([prod_input])
|
| 39 |
+
prediction = model.predict(input_data)[0]
|
| 40 |
+
|
| 41 |
+
return jsonify({'predicted_sales': round(prediction, 2)})
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return jsonify({'error': str(e)}), 500
|
| 45 |
+
|
| 46 |
+
# Batch prediction
|
| 47 |
+
@product_sales_predictor_api.post('/v1/products')
|
| 48 |
+
def predict_multiple_products():
|
| 49 |
+
try:
|
| 50 |
+
# Get uploaded CSV file
|
| 51 |
+
input_file = request.files['file']
|
| 52 |
+
input_data = pd.read_csv(input_file)
|
| 53 |
+
|
| 54 |
+
# Remove Product_Id if present
|
| 55 |
+
prediction_data = input_data.drop(['Product_Id'], axis=1, errors='ignore')
|
| 56 |
+
|
| 57 |
+
# Make predictions
|
| 58 |
+
predictions = model.predict(prediction_data).tolist()
|
| 59 |
+
predictions = [round(pred, 2) for pred in predictions]
|
| 60 |
+
|
| 61 |
+
# Create output dictionary
|
| 62 |
+
if 'Product_Id' in input_data.columns:
|
| 63 |
+
prod_id_list = input_data['Product_Id'].tolist()
|
| 64 |
+
result = dict(zip(prod_id_list, predictions))
|
| 65 |
+
else:
|
| 66 |
+
result = {f"product_{i+1}": pred for i, pred in enumerate(predictions)}
|
| 67 |
+
|
| 68 |
+
return jsonify(result)
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return jsonify({'error': str(e)}), 500
|
| 72 |
+
|
| 73 |
+
# Run the Flask app in debug mode
|
| 74 |
+
if __name__ == '__main__':
|
| 75 |
+
app.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy: 2.3.2
|
| 2 |
+
pandas: 2.3.1
|
| 3 |
+
scikit-learn: 1.7.1
|
| 4 |
+
xgboost: 3.0.4
|
| 5 |
+
joblib: 1.5.1
|
| 6 |
+
Werkzeug: 3.1.3
|
| 7 |
+
flask: 3.1.2
|
| 8 |
+
gunicorn: 23.0.0
|
| 9 |
+
requests: 2.32.4
|
| 10 |
+
uvicorn[standard]
|
| 11 |
+
streamlit: 1.48.1
|