AnuSubash commited on
Commit
34487a7
·
verified ·
1 Parent(s): 23d8e4c

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. Dockerfile +16 -0
  2. app.py +98 -0
  3. batch_superkart_predict.csv +20 -0
  4. requirements.txt +11 -0
  5. superkart_model.joblib +3 -0
Dockerfile ADDED
@@ -0,0 +1,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:product_sales_predictor_api"]
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import numpy as np
3
+ import joblib # For loading the serialized model
4
+ import pandas as pd # For data manipulation
5
+ from flask import Flask, request, jsonify # For creating the Flask API
6
+ from datetime import datetime # For handling date and time operations
7
+ # Initialize the Flask application
8
+ product_sales_predictor_api = Flask("SuperKart Product Sales Predictor")
9
+
10
+
11
+ # Load the trained machine learning model
12
+ model = joblib.load("superkart_model.joblib")
13
+
14
+ # Define a route for the home page (GET request)
15
+ @product_sales_predictor_api.get('/')
16
+ def home():
17
+ """
18
+ This function handles GET requests to the root URL ('/') of the API.
19
+ It returns a simple welcome message.
20
+ """
21
+ return "Welcome to the SuperKart Product Sales Prediction API!"
22
+
23
+ # Define an endpoint for single property prediction (POST request)
24
+ @product_sales_predictor_api.post('/v1/sales')
25
+ def predict_product_sales():
26
+ """
27
+ This function handles POST requests to the '/v1/sales' endpoint.
28
+ It expects a JSON payload containing property details and returns
29
+ the predicted rental price as a JSON response.
30
+ """
31
+ # Get the JSON data from the request body
32
+ property_data = request.get_json()
33
+ current_year = datetime.now().year
34
+
35
+ # Extract relevant features from the JSON data
36
+ sample = {
37
+ 'Product_Id': property_data['Product_Id'],
38
+ 'Product_Weight': property_data['Product_Weight'],
39
+ 'Product_Sugar_Content': property_data['Product_Sugar_Content'],
40
+ 'Product_Allocated_Area': property_data['Product_Allocated_Area'],
41
+ 'Product_Type': property_data['Product_Type'],
42
+ 'Product_MRP': property_data['Product_MRP'],
43
+ 'Store_Id': property_data['Store_Id'],
44
+ 'Store_Establishment_Year': property_data['Store_Establishment_Year'],
45
+ 'Store_Size': property_data['Store_Size'],
46
+ 'Store_Location_City_Type': property_data['Store_Location_City_Type'],
47
+ 'Store_Type': property_data['Store_Type'],
48
+ 'Store_Age': current_year - property_data['Store_Establishment_Year']
49
+ }
50
+
51
+ # Convert the extracted data into a Pandas DataFrame
52
+ input_data = pd.DataFrame([sample])
53
+
54
+ # Make prediction (get log_sales)
55
+ predicted_sales = model.predict(input_data)[0]
56
+
57
+ # Calculate actual price
58
+ #predicted_sales = np.exp(predicted_log_sales)
59
+
60
+ # Convert predicted_price to Python float
61
+ predicted_sales = round(float(predicted_sales), 2)
62
+ # The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
63
+ # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
64
+
65
+ # Return the actual price
66
+ return jsonify({'Predicted Sales (in dollars)': predicted_sales})
67
+
68
+
69
+ # Define an endpoint for batch prediction (POST request)
70
+ @product_sales_predictor_api.post('/v1/salesbatch')
71
+ def predict_product_sales_batch():
72
+ """
73
+ This function handles POST requests to the '/v1/salesbatch' endpoint.
74
+ It expects a CSV file containing property details for multiple properties
75
+ and returns the predicted rental prices as a dictionary in the JSON response.
76
+ """
77
+ # Get the uploaded CSV file from the request
78
+ file = request.files['file']
79
+
80
+ # Read the CSV file into a Pandas DataFrame
81
+ input_data = pd.read_csv(file)
82
+
83
+ # Make predictions for all properties in the DataFrame (get log_prices)
84
+ predicted_list_sales = model.predict(input_data).tolist()
85
+
86
+ # Calculate actual prices
87
+ predicted_sales = [round(float(list_sales), 2) for list_sales in predicted_list_sales]
88
+
89
+ # Create a dictionary of predictions with property IDs as keys
90
+ product_ids = input_data['Product_Id'].tolist() # Assuming 'id' is the property ID column
91
+ output_dict = dict(zip(product_ids, predicted_sales)) # Use actual prices
92
+
93
+ # Return the predictions dictionary as a JSON response
94
+ return output_dict
95
+
96
+ # Run the Flask application in debug mode if this script is executed directly
97
+ if __name__ == '__main__':
98
+ product_sales_predictor_api.run(debug=True)
batch_superkart_predict.csv ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Product_Id,Product_Weight,Product_Sugar_Content,Product_Allocated_Area,Product_Type,Product_MRP,Store_Id,Store_Establishment_Year,Store_Size,Store_Location_City_Type,Store_Type,Store_Age
2
+ FD6114,12.66,Low Sugar,0.027,Frozen Foods,117.08,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
3
+ FD7839,16.54,Low Sugar,0.144,Dairy,171.43,OUT003,1999,Medium,Tier 1,Departmental Store,26
4
+ FD5075,14.28,Regular,0.031,Canned,162.08,OUT001,1987,High,Tier 2,Supermarket Type1,38
5
+ FD8233,12.1,Low Sugar,0.112,Baking Goods,186.31,OUT001,1987,High,Tier 2,Supermarket Type1,38
6
+ NC1180,9.57,No Sugar,0.01,Health and Hygiene,123.67,OUT002,1998,Small,Tier 3,Food Mart,27
7
+ FD5680,12.03,Low Sugar,0.053,Snack Foods,113.64,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
8
+ FD5484,16.35,Low Sugar,0.112,Meat,185.71,OUT003,1999,Medium,Tier 1,Departmental Store,26
9
+ NC5885,12.94,No Sugar,0.286,Household,194.75,OUT003,1999,Medium,Tier 1,Departmental Store,26
10
+ FD1961,9.45,Low Sugar,0.047,Snack Foods,95.95,OUT002,1998,Small,Tier 3,Food Mart,27
11
+ NC6657,8.94,No Sugar,0.045,Health and Hygiene,143.01,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
12
+ DR2699,10.64,Low Sugar,0.02,Hard Drinks,165.99,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
13
+ FD6027,13.92,Low Sugar,0.099,Fruits and Vegetables,116.89,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
14
+ FD8553,10.97,Low Sugar,0.156,Fruits and Vegetables,175.71,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
15
+ FD5276,12.25,Low Sugar,0.04,Canned,160.42,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
16
+ FD1231,11.31,Regular,0.049,Baking Goods,168.4,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
17
+ FD1387,13.26,Low Sugar,0.024,Breads,156.17,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
18
+ NC1071,10.74,No Sugar,0.173,Household,138.19,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
19
+ FD2625,12.45,Low Sugar,0.045,Frozen Foods,143.25,OUT004,2009,Medium,Tier 2,Supermarket Type2,16
20
+ FD4982,13.17,Low Sugar,0.075,Fruits and Vegetables,185.03,OUT003,1999,Medium,Tier 1,Departmental Store,26
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
superkart_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f9dfd819393497b85d7b01c6f39d9d61960fc48719e7bcbf9c3fa13b5b0ca791
3
+ size 260929