Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +19 -0
- app.py +76 -0
- requirements.txt +11 -0
- sales_prediction_model_v1_0.joblib +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory inside the container
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy requirements first (to leverage Docker cache for faster builds)
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
|
| 9 |
+
# Install dependencies without cache
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Copy the rest of the application files
|
| 13 |
+
COPY . .
|
| 14 |
+
|
| 15 |
+
# Expose the application port
|
| 16 |
+
EXPOSE 7860
|
| 17 |
+
|
| 18 |
+
# Start the application with Gunicorn (4 workers)
|
| 19 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize flask app
|
| 6 |
+
sales_predictor_api = Flask("Sales Predictor")
|
| 7 |
+
|
| 8 |
+
# Load the trained sales prediction model
|
| 9 |
+
model = joblib.load("sales_prediction_model_v1_0.joblib")
|
| 10 |
+
|
| 11 |
+
# Home route
|
| 12 |
+
@sales_predictor_api.get('/')
|
| 13 |
+
def home():
|
| 14 |
+
return "Welcome to the Sales Prediction API"
|
| 15 |
+
|
| 16 |
+
# Predict for a single product
|
| 17 |
+
@sales_predictor_api.post('/v1/product')
|
| 18 |
+
def predict_sales():
|
| 19 |
+
try:
|
| 20 |
+
# Get JSON data from the request
|
| 21 |
+
customer_data = request.get_json()
|
| 22 |
+
|
| 23 |
+
# Extract relevant features
|
| 24 |
+
sample = {
|
| 25 |
+
'Product_Weight': customer_data['Product_Weight'],
|
| 26 |
+
'Product_Sugar_Content': customer_data['Product_Sugar_Content'],
|
| 27 |
+
'Product_Allocated_Area': customer_data['Product_Allocated_Area'],
|
| 28 |
+
'Product_Type': customer_data['Product_Type'],
|
| 29 |
+
'Product_MRP': customer_data['Product_MRP'],
|
| 30 |
+
'Store_Id': customer_data['Store_Id'],
|
| 31 |
+
'Store_Establishment_Year': customer_data['Store_Establishment_Year'],
|
| 32 |
+
'Store_Size': customer_data['Store_Size'],
|
| 33 |
+
'Store_Location_City_Type': customer_data['Store_Location_City_Type'],
|
| 34 |
+
'Store_Type': customer_data['Store_Type'],
|
| 35 |
+
'Product_Id': customer_data['Product_Id']
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Convert to DataFrame
|
| 39 |
+
input_data = pd.DataFrame([sample])
|
| 40 |
+
|
| 41 |
+
# Optional: extract ID prefix if needed
|
| 42 |
+
input_data["Id"] = input_data["Product_Id"].str[:2]
|
| 43 |
+
input_data.drop("Product_Id", axis=1, inplace=True)
|
| 44 |
+
|
| 45 |
+
# Predict
|
| 46 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 47 |
+
|
| 48 |
+
return jsonify({"Prediction": prediction})
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return jsonify({"error": str(e)}), 400
|
| 52 |
+
|
| 53 |
+
# Predict for a batch of products
|
| 54 |
+
@sales_predictor_api.post('/v1/productbatch')
|
| 55 |
+
def predict_sales_batch():
|
| 56 |
+
try:
|
| 57 |
+
# Get uploaded CSV
|
| 58 |
+
file = request.files['file']
|
| 59 |
+
input_data = pd.read_csv(file)
|
| 60 |
+
|
| 61 |
+
input_data["Id"] = input_data["Product_Id"].str[:2]
|
| 62 |
+
cust_id_list = input_data["Product_Id"].tolist()
|
| 63 |
+
input_data.drop("Product_Id", axis=1, inplace=True)
|
| 64 |
+
|
| 65 |
+
# Predict
|
| 66 |
+
predictions = model.predict(input_data).tolist()
|
| 67 |
+
output_dict = dict(zip(cust_id_list, predictions))
|
| 68 |
+
|
| 69 |
+
return jsonify(output_dict)
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return jsonify({"error": str(e)}), 400
|
| 73 |
+
|
| 74 |
+
# Run Flask app
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
sales_predictor_api.run(debug=True)
|
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
|
sales_prediction_model_v1_0.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:84e263c9b1b8b97eb8fc23c58312e29906a3c10b25d7306d7f1d797adca82865
|
| 3 |
+
size 13931434
|