siaese commited on
Commit
c4b89b9
·
verified ·
1 Parent(s): 9c10ab9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -9
  2. app.py +130 -108
  3. requirements.txt +8 -1
Dockerfile CHANGED
@@ -1,16 +1,17 @@
1
- # Use a minimal base image with Python 3.9 installed
2
  FROM python:3.9-slim
3
 
4
- # Set the working directory inside the container to /app
5
  WORKDIR /app
6
 
7
- # Copy all files from the current directory on the host to the container's /app directory
8
  COPY . .
9
 
10
- # Install Python dependencies listed in requirements.txt
11
- RUN pip3 install -r fe_requirements.txt
12
 
13
- # Define the command to run the Streamlit app on port 8501 and make it accessible externally
14
- # CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
15
- CMD ["streamlit", "run", "fe_app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
16
- # NOTE: Disable XSRF protection for easier external access in order to make batch predictions
 
 
 
 
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 -r be_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:app"]
17
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "be_app:super_kart_predictor_api"]
app.py CHANGED
@@ -1,110 +1,132 @@
1
- # import streamlit as st
 
2
  # import pandas as pd
3
- # import requests
4
-
5
- # # Streamlit UI for Customer Churn Prediction
6
- # st.title("SuperKart Sales Prediction App")
7
- # st.subheader("Online Sales Prediction")
8
-
9
- # # Collect user input based on dataset columns
10
- # Product_Id = st.number_input("Product ID", min_value=1, max_value=1000000, value=1)
11
- # Product_Weight = st.number_input("Product Weight (kg)", min_value=0.1, max_value=100.0, value=1.0, format="%.2f")
12
- # Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low", "Medium", "High"]) # Adjust options as per your dataset
13
- # Product_Allocated_Area = st.number_input("Product Allocated Area (sq. units)", min_value=0.0, max_value=10000.0, value=100.0, format="%.2f")
14
- # Product_Type = st.selectbox("Product Type", ["Type A", "Type B", "Type C"]) # Replace with actual product types
15
- # Product_MRP = st.number_input("Product MRP", min_value=0.0, max_value=10000.0, value=100.0, format="%.2f")
16
-
17
- # Store_Id = st.number_input("Store ID", min_value=1, max_value=1000000, value=1)
18
- # Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1900, max_value=2025, value=2000)
19
-
20
- # Store_Size = st.selectbox("Store Size", ["Small", "Medium", "Large"]) # Adjust options according to your data
21
- # Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
22
- # Store_Type = st.selectbox("Store Type", ["Type 1", "Type 2", "Type 3"]) # Replace with actual store types
23
-
24
- # # Product_Store_Sales_Total = st.number_input("Total Product Store Sales", min_value=0.0, value=0.0, format="%.2f")
25
-
26
- # # Convert user input to dataframe
27
- # input_data = {
28
- # 'Product_Id': Product_Id,
29
- # 'Product_Weight': Product_Weight,
30
- # 'Product_Sugar_Content': Product_Sugar_Content,
31
- # 'Product_Allocated_Area': Product_Allocated_Area,
32
- # 'Product_Type': Product_Type,
33
- # 'Product_MRP': Product_MRP,
34
- # 'Store_Id': Store_Id,
35
- # 'Store_Establishment_Year': Store_Establishment_Year,
36
- # 'Store_Size': Store_Size,
37
- # 'Store_Location_City_Type': Store_Location_City_Type,
38
- # 'Store_Type': Store_Type
39
- # }
40
-
41
- # # Change as per requirement
42
- # if st.button("Predict"):
43
- # # response = requests.post("https://siaese/superkart-api/v1/sales", json=input_data.to_dict(orient='records')[0]) # enter user name and space name before running the cell
44
- # response = requests.post("https://siaese/superkart-api/v1/sales", json=input_data) # enter user name and space name before running the cell
45
- # if response.status_code == 200:
46
- # prediction = response.json()['Predicted Sales for SuperKart']
47
- # st.success(f'Predicted Sales: {prediction}')
48
- # else:
49
- # st.error("Error making sales prediction")
50
-
51
- # # Sales Prediction
52
- # st.subheader("Sales Prediction")
53
- import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
54
  import pandas as pd
55
- import requests
56
-
57
- # Streamlit UI for Customer Churn Prediction
58
- st.title("SuperKart Sales Prediction App")
59
- st.subheader("Online Sales Prediction")
60
-
61
- # Collect user input based on dataset columns
62
- Product_Id = st.number_input("Product ID", min_value=1, max_value=1000000, value=1)
63
- Product_Weight = st.number_input("Product Weight (kg)", min_value=0.1, max_value=100.0, value=1.0, format="%.2f")
64
- Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low", "Medium", "High"]) # Adjust options as per your dataset
65
- Product_Allocated_Area = st.number_input("Product Allocated Area (sq. units)", min_value=0.0, max_value=10000.0, value=100.0, format="%.2f")
66
- Product_Type = st.selectbox("Product Type", ["Type A", "Type B", "Type C"]) # Replace with actual product types
67
- Product_MRP = st.number_input("Product MRP", min_value=0.0, max_value=10000.0, value=100.0, format="%.2f")
68
- Product_Category = st.selectbox("Product_Category", ["FD" "NC" "DR"])
69
-
70
- Store_Id = st.number_input("Store ID", min_value=1, max_value=1000000, value=1)
71
- Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1900, max_value=2025, value=2000)
72
- Store_Tenure = st.number_input("Store_Tenure", min_value=16, max_value=50, value=32)
73
- Store_Size = st.selectbox("Store Size", ["Small", "Medium", "Large"]) # Adjust options according to your data
74
- Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
75
- Store_Type = st.selectbox("Store Type", ["Type 1", "Type 2", "Type 3"]) # Replace with actual store types
76
-
77
- Perishability = st.selectbox("Perishability", ["Perishable", "Non-Perishable", "Unknown"]) # Replace with actual store types
78
-
79
- # Product_Store_Sales_Total = st.number_input("Total Product Store Sales", min_value=0.0, value=0.0, format="%.2f")
80
-
81
- # Convert user input to dataframe
82
- input_data = {
83
- "Product_Id": Product_Id,
84
- "Product_Weight": Product_Weight,
85
- "Product_Sugar_Content": Product_Sugar_Content,
86
- "Product_Allocated_Area": Product_Allocated_Area,
87
- "Product_Type": Product_Type,
88
- "Product_MRP": Product_MRP,
89
- "Product_Category": Product_Category,
90
- "Store_Id": Store_Id,
91
- "Store_Tenure": Store_Tenure,
92
- "Store_Establishment_Year": Store_Establishment_Year,
93
- "Store_Size": Store_Size,
94
- "Store_Location_City_Type": Store_Location_City_Type,
95
- "Store_Type": Store_Type,
96
- "Perishability": Perishability
97
- }
98
-
99
- # Change as per requirement
100
- if st.button("Predict"):
101
- # response = requests.post("https://siaese/superkart-api/v1/sales", json=input_data.to_dict(orient="records")[0]) # enter user name and space name before running the cell
102
- response = requests.post("https://siaese/superkart-api/v1/sales", json=input_data) # enter user name and space name before running the cell
103
- if response.status_code == 200:
104
- prediction = response.json()["predicted_sales_price"]
105
- st.success(f"Predicted Sales: {prediction}")
106
- else:
107
- st.error("Error making sales prediction")
108
-
109
- # Sales Prediction
110
- st.subheader("Sales Prediction")
 
 
 
 
 
 
 
 
 
 
 
1
+ # import numpy as np
2
+ # import joblib
3
  # import pandas as pd
4
+ # from flask import Flask, request, jsonify
5
+
6
+ # # Initialize the Flask app with a custom name
7
+ # super_kart_predictor_api = Flask("Super Kart Sales Predictor")
8
+
9
+ # # Load the trained model from the specified path
10
+ # # Make sure model_path variable is defined or replace with the actual path string
11
+ # model = joblib.load(model_path)
12
+
13
+ # # Define a route for the home page (GET request)
14
+ # @super_kart_predictor_api.get('/')
15
+ # def home():
16
+ # """
17
+ # This function handles GET requests to the root URL ('/') of the API.
18
+ # It returns a simple welcome message.
19
+ # """
20
+ # return "Welcome to the Super Kart Sales Predictor API!"
21
+
22
+ # # Define a route for predictions (POST request)
23
+ # @super_kart_predictor_api.post("/v1/sales")
24
+ # def predict_sales():
25
+ # """
26
+ # This function handles POST requests to the /v1/sales endpoint.
27
+ # It expects a JSON payload containing commodity sales details and returns
28
+ # the predicted sales as a JSON response
29
+ # """
30
+ # # Get JSON data from the POST request
31
+ # superkart_data = request.get_json
32
+ # print(superkart_data)
33
+
34
+ # # Extract relevant features from the JSON payload into a dictionary
35
+ # sample = {
36
+ # 'Product_Weight': superkart_data['Product_Weight'],
37
+ # 'Product_Allocated_Area': superkart_data['Product_Allocated_Area'],
38
+ # 'Product_MRP': superkart_data['Product_MRP'],
39
+ # 'Store_Tenure': superkart_data['Store_Tenure'],
40
+ # 'Product_Category': superkart_data['Product_Category'],
41
+ # 'Product_Sugar_Content': superkart_data['Product_Sugar_Content'],
42
+ # 'Product_Type': superkart_data['Product_Type'],
43
+ # 'Store_Id': superkart_data['Store_Id'],
44
+ # 'Store_Size': superkart_data['Store_Size'],
45
+ # 'Store_Location_City_Type': superkart_data['Store_Location_City_Type'],
46
+ # 'Store_Type': superkart_data['Store_Type'],
47
+ # 'Perishability': superkart_data['Perishability']
48
+ # }
49
+
50
+ # # Create a DataFrame from the input dictionary for model compatibility
51
+ # input_data = pd.DataFrame([sample])
52
+
53
+ # # Predict sales price using the loaded model
54
+ # predicted_sales_price = model.predict(input_data)[0]
55
+
56
+ # # Convert predicted sales price back from log scale using exponential
57
+ # predicted_sales = np.exp(predicted_sales_price)
58
+
59
+ # # Convert the prediction to a float type with rounding to 2 decimal places
60
+ # predicted_sales = float(predicted_sales, 2)
61
+
62
+ # # Return the prediction as a JSON response
63
+ # return jsonify({"predicted_sales_price": predicted_sales})
64
+ import numpy as np
65
+ import joblib
66
  import pandas as pd
67
+ from flask import Flask, request, jsonify
68
+
69
+ # Initialize the Flask app with a custom name
70
+ super_kart_predictor_api = Flask("Super Kart Sales Predictor")
71
+
72
+ # Load the trained model from the specified path
73
+ # Make sure model_path variable is defined or replace with the actual path string
74
+ model_path = "super_kart_prediction_gbr_tuned_model_v1_0.joblib"
75
+ model = joblib.load(model_path)
76
+
77
+ # Define a route for the home page (GET request)
78
+ @super_kart_predictor_api.get('/')
79
+ def home():
80
+ """
81
+ This function handles GET requests to the root URL ('/') of the API.
82
+ It returns a simple welcome message.
83
+ """
84
+ return "Welcome to the Super Kart Sales Predictor API!"
85
+
86
+ # Define a route for predictions (POST request)
87
+ @super_kart_predictor_api.post("/v1/sales")
88
+ def predict_sales():
89
+ """
90
+ This function handles POST requests to the /v1/sales endpoint.
91
+ It expects a JSON payload containing commodity sales details and returns
92
+ the predicted sales as a JSON response
93
+ """
94
+ # Get JSON data from the POST request
95
+ superkart_data = request.get_json()
96
+ print(f"\nIncoming request data: \n{superkart_data}\n")
97
+
98
+ # Extract relevant features from the JSON payload into a dictionary
99
+ sample = {
100
+ 'Product_Weight': superkart_data['Product_Weight'],
101
+ 'Product_Allocated_Area': superkart_data['Product_Allocated_Area'],
102
+ 'Product_MRP': superkart_data['Product_MRP'],
103
+ 'Product_Sugar_Content': superkart_data['Product_Sugar_Content'],
104
+ 'Product_Type': superkart_data['Product_Type'],
105
+ 'Product_Category': superkart_data['Product_Category'],
106
+ 'Store_Id': superkart_data['Store_Id'],
107
+ 'Store_Establishment_Year': superkart_data['Store_Establishment_Year'],
108
+ 'Store_Size': superkart_data['Store_Size'],
109
+ 'Store_Location_City_Type': superkart_data['Store_Location_City_Type'],
110
+ 'Store_Type': superkart_data['Store_Type'],
111
+ 'Store_Tenure': superkart_data['Store_Tenure'],
112
+ 'Perishability': superkart_data['Perishability'],
113
+ }
114
+
115
+ # Create a DataFrame from the input dictionary for model compatibility
116
+ input_data = pd.DataFrame([sample])
117
+
118
+ # Predict sales price using the loaded model
119
+ predicted_sales_price = model.predict(input_data)[0]
120
+
121
+ # Convert the prediction to a float type with rounding to 3 decimal places
122
+ predicted_sales = round(predicted_sales_price, 3)
123
+
124
+ print(f"\nPredicted Sales Price: {predicted_sales}\n")
125
+
126
+ # Return the prediction as a JSON response
127
+ return jsonify({"predicted_sales_price": predicted_sales})
128
+
129
+
130
+
131
+ # if __name__ == '__main__':
132
+ # super_kart_predictor_api.run()
requirements.txt CHANGED
@@ -1,3 +1,10 @@
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
3
- streamlit==1.43.2
 
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]