Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +58 -0
- requirements.txt +9 -0
- superkart_sales_model.pkl +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11.3-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:sales_predictor_api"]
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app with a name
|
| 6 |
+
sales_predictor_api = Flask("SuperKart Sales Predictor")
|
| 7 |
+
|
| 8 |
+
# Re-define the same functions used inside the pipeline
|
| 9 |
+
def add_store_age(df):
|
| 10 |
+
df = df.copy()
|
| 11 |
+
df['Store_Age'] = 2025 - df['Store_Established_Year']
|
| 12 |
+
df = df.drop(columns=['Store_Established_Year'])
|
| 13 |
+
return df
|
| 14 |
+
|
| 15 |
+
def map_ordered_features(X):
|
| 16 |
+
sugar_order_map = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 17 |
+
size_order_map = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 18 |
+
city_order_map = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 19 |
+
|
| 20 |
+
X = X.copy()
|
| 21 |
+
X['Product_Sugar_Content'] = X['Product_Sugar_Content'].map(sugar_order_map).astype(int)
|
| 22 |
+
X['Store_Size'] = X['Store_Size'].map(size_order_map).astype(int)
|
| 23 |
+
X['Store_Location_City_Type'] = X['Store_Location_City_Type'].map(city_order_map).astype(int)
|
| 24 |
+
return X
|
| 25 |
+
|
| 26 |
+
# Load the trained SuperKart Sales prediction model
|
| 27 |
+
model = joblib.load("superkart_sales_model.pkl")
|
| 28 |
+
|
| 29 |
+
# Define a route for the home page
|
| 30 |
+
@sales_predictor_api.get('/')
|
| 31 |
+
def home():
|
| 32 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
| 33 |
+
|
| 34 |
+
# Define an endpoint to predict churn for a single customer
|
| 35 |
+
@sales_predictor_api.post('/v1/productsales')
|
| 36 |
+
def predict_sales():
|
| 37 |
+
try:
|
| 38 |
+
# Get JSON data
|
| 39 |
+
sales_data = request.get_json()
|
| 40 |
+
|
| 41 |
+
# Ensure input is a list of records
|
| 42 |
+
if isinstance(sales_data, dict):
|
| 43 |
+
sales_data = [sales_data]
|
| 44 |
+
|
| 45 |
+
# Convert to DataFrame
|
| 46 |
+
input_data = pd.DataFrame(sales_data)
|
| 47 |
+
|
| 48 |
+
# Predict
|
| 49 |
+
prediction = model.predict(input_data).tolist()[0]
|
| 50 |
+
|
| 51 |
+
return jsonify({"prediction": float(prediction)})
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return jsonify({"error": str(e)})
|
| 55 |
+
|
| 56 |
+
# Run the Flask app in debug mode
|
| 57 |
+
if __name__ == '__main__':
|
| 58 |
+
sales_predictor_api.run(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.32.3
|
superkart_sales_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4b9d9a00480131b5d599b0cc6d958ab39e6466a30c1a4d5e846e4157c3fa312c
|
| 3 |
+
size 12299802
|