DataWiz-6939 commited on
Commit
4505cd4
·
verified ·
1 Parent(s): b0c6f9d

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(Flask instance `Superkart_api` inside `app.py`)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:Superkart_api"]
17
+
18
+
19
+
Superkart_Sales_prediction_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c633b226e1ab2f7998b721fcab20514d2de2c88de42861f13589285db5832727
3
+ size 689928
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import numpy as np
3
+ import pandas as pd
4
+ import joblib
5
+ from flask import Flask, request, jsonify
6
+ from datetime import datetime
7
+
8
+ # Initialize Flask app
9
+ Superkart_api = Flask("SuperKart Sales Prediction")
10
+
11
+ # Load the trained SuperKart sales model from the correct path
12
+ model = joblib.load("backend_files/Superkart_Sales_prediction_model_v1_0.joblib")
13
+
14
+ # Feature Engineering
15
+ current_year = datetime.now().year
16
+ perishable = {'Meat','Dairy','Fruits and Vegetables','Frozen Foods','Seafood','Breads','Breakfast'}
17
+
18
+ def apply_feature_engineering(dataset):
19
+
20
+ if 'Product_Id' in dataset.columns:
21
+ dataset['Product_Category'] = dataset['Product_Id'].str[:2].map({
22
+ 'DR':'Drinks','NC':'Non-Consumable','FD':'Food & Veg'
23
+ }).fillna('Other')
24
+ if 'Store_Establishment_Year' in dataset.columns:
25
+ dataset['Store_Age'] = current_year - dataset['Store_Establishment_Year']
26
+ if 'Product_Type' in dataset.columns:
27
+ dataset['Food_Type'] = dataset['Product_Type'].apply(
28
+ lambda t: 'Perishable' if t in perishable else 'Non-Perishable'
29
+ )
30
+ return dataset
31
+
32
+ # Home route
33
+ @Superkart_api.get('/')
34
+ def home():
35
+ return "Welcome to the SuperKart Sales Prediction!"
36
+
37
+ # Single prediction endpoint
38
+ @Superkart_api.post('/v1/sales')
39
+ def predict_sales():
40
+ data = request.get_json()
41
+ sample = {
42
+ 'Product_Id': data.get('Product_Id'),
43
+ 'Product_Weight': data.get('Product_Weight'),
44
+ 'Product_Sugar_Content': data.get('Product_Sugar_Content'),
45
+ 'Product_Allocated_Area': data.get('Product_Allocated_Area'),
46
+ 'Product_Type': data.get('Product_Type'),
47
+ 'Product_MRP': data.get('Product_MRP'),
48
+ 'Store_Id': data.get('Store_Id'),
49
+ 'Store_Establishment_Year': data.get('Store_Establishment_Year'),
50
+ 'Store_Size': data.get('Store_Size'),
51
+ 'Store_Location_City_Type': data.get('Store_Location_City_Type'),
52
+ 'Store_Type': data.get('Store_Type'),
53
+ }
54
+ input_df = pd.DataFrame([sample])
55
+ input_df = apply_feature_engineering(input_df)
56
+ predicted_sales = model.predict(input_df)[0]
57
+ return jsonify({'Predicted Sales': round(float(predicted_sales), 2)})
58
+
59
+ # Batch prediction endpoint
60
+ @Superkart_api.post('/v1/salesbatch')
61
+ def predict_sales_batch():
62
+ file = request.files['file']
63
+ input_df = pd.read_csv(file)
64
+ input_df = apply_feature_engineering(input_df)
65
+ predicted_sales = model.predict(input_df).tolist()
66
+
67
+ if 'Product_Id' in input_df.columns:
68
+ product_ids = input_df['Product_Id'].tolist()
69
+ output_dict = dict(zip(product_ids, [round(float(x), 2) for x in predicted_sales]))
70
+ else:
71
+ output_dict = [round(float(x), 2) for x in predicted_sales]
72
+
73
+ return jsonify(output_dict)
74
+
75
+ # Run the app
76
+ if __name__ == '__main__':
77
+ Superkart_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