kutkarsh200 commited on
Commit
0d13f2f
·
verified ·
1 Parent(s): e741d55

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -9
  2. app.py +52 -55
  3. requirements.txt +2 -7
Dockerfile CHANGED
@@ -1,16 +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:future_sale_predictor_api"]
 
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 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
+
16
+ # NOTE: Disable XSRF protection for easier external access in order to make batch predictions
 
app.py CHANGED
@@ -1,67 +1,64 @@
1
- import joblib
 
2
  import pandas as pd
3
- from flask import Flask, request, jsonify
4
 
5
- # Initialize Flask app with a name
6
- future_sale_predictor_api = Flask("SuperKart Sales Predictor")
7
 
8
- # Load the trained sales prediction model
9
- model = joblib.load("deployment_files/xgb_tuned_model.joblib")
10
 
11
- # Define a route for the home page
12
- @future_sale_predictor_api.get('/')
13
- def home():
14
- return "Welcome to the SuperKart Sales Prediction API!, Created by Kumar Utkarsh"
 
 
 
 
 
 
 
15
 
16
- # Define an endpoint to predict sales for a single product-store combination
17
- @future_sale_predictor_api.post('/v1/predict_sale')
18
- def predict_sale():
19
- # Get JSON data from the request
20
- sale_data = request.get_json()
21
 
22
- # Extract relevant product-store information from the input data
23
- # Ensure these keys match the expected input features for your trained model
24
- sample = {
25
- 'Product_Weight': sale_data['Product_Weight'],
26
- 'Product_Sugar_Content': sale_data['Product_Sugar_Content'],
27
- 'Product_Allocated_Area': sale_data['Product_Allocated_Area'],
28
- 'Product_Type': sale_data['Product_Type'],
29
- 'Product_MRP': sale_data['Product_MRP'],
30
- 'Store_Id': sale_data['Store_Id'],
31
- 'Store_Establishment_Year': sale_data['Store_Establishment_Year'],
32
- 'Store_Size': sale_data['Store_Size'],
33
- 'Store_Location_City_Type': sale_data['Store_Location_City_Type'],
34
- 'Store_Type': sale_data['Store_Type']
35
- }
36
 
37
- # Convert the extracted data into a DataFrame
38
- input_data = pd.DataFrame([sample])
39
 
40
- # Make a sales prediction using the trained model
41
- prediction = model.predict(input_data).tolist()[0]
 
 
 
 
 
 
 
42
 
43
- # Return the prediction as a JSON response
44
- return jsonify({'Predicted_Sales': prediction})
45
 
46
- # Define an endpoint to predict sales for a batch of product-store combinations
47
- @future_sale_predictor_api.post('/v1/predict_sales_batch')
48
- def predict_sales_batch():
49
- # Get the uploaded CSV file from the request
50
- file = request.files['file']
51
 
52
- # Read the file into a DataFrame
53
- input_data = pd.read_csv(file)
 
 
54
 
55
- # Make predictions for the batch data
56
- # Assuming the input CSV for batch prediction has the same columns as the training data
57
- predictions = model.predict(input_data).tolist()
58
-
59
- # You might want to return predictions linked to an identifier if available in the batch input
60
- # For simplicity, returning a list of predictions
61
- return jsonify({'Predicted_Sales_Batch': predictions})
62
-
63
-
64
- # Run the Flask app in debug mode
65
- if __name__ == '__main__':
66
- # Port 7860 is used
67
- future_sale_predictor_api.run(debug=True, host='0.0.0.0', port=7860)
 
1
+ import requests
2
+ import streamlit as st
3
  import pandas as pd
 
4
 
5
+ st.title("SuperKart Sales Prediction")
 
6
 
7
+ # Online Prediction (Single Product-Store Combination)
8
+ st.subheader("Online Prediction")
9
 
10
+ # Input fields for product-store data
11
+ Product_Weight = st.number_input("Product Weight", min_value=0.0, value=10.0)
12
+ Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
13
+ Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, value=0.1)
14
+ Product_Type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Household', 'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood'])
15
+ Product_MRP = st.number_input("Product MRP", min_value=0.0, value=100.0)
16
+ Store_Id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
17
+ Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1900, max_value=2024, value=2000)
18
+ Store_Size = st.selectbox("Store Size", ["Medium", "High", "Small"])
19
+ Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 2", "Tier 1", "Tier 3"])
20
+ Store_Type = st.selectbox("Store Type", ["Supermarket Type2", "Departmental Store", "Supermarket Type1", "Food Mart"])
21
 
 
 
 
 
 
22
 
23
+ product_store_data = {
24
+ 'Product_Weight': Product_Weight,
25
+ 'Product_Sugar_Content': Product_Sugar_Content,
26
+ 'Product_Allocated_Area': Product_Allocated_Area,
27
+ 'Product_Type': Product_Type,
28
+ 'Product_MRP': Product_MRP,
29
+ 'Store_Id': Store_Id,
30
+ 'Store_Establishment_Year': Store_Establishment_Year,
31
+ 'Store_Size': Store_Size,
32
+ 'Store_Location_City_Type': Store_Location_City_Type,
33
+ 'Store_Type': Store_Type
34
+ }
 
 
35
 
36
+ backend_url = "https://kutkarsh200-SuperKart.hf.space"
 
37
 
38
+ if st.button("Predict Sale", type='primary'):
39
+ response = requests.post(f"{backend_url}/v1/predict_sale", json=product_store_data)
40
+ if response.status_code == 200:
41
+ result = response.json()
42
+ predicted_sale = result["Predicted_Sales"]
43
+ st.write(f"The predicted sales for this product-store combination is: {predicted_sale:.2f}")
44
+ else:
45
+ st.error(f"Error in API request: {response.status_code}")
46
+ st.error(f"Response: {response.text}")
47
 
 
 
48
 
49
+ # Batch Prediction
50
+ st.subheader("Batch Prediction")
 
 
 
51
 
52
+ file = st.file_uploader("Upload CSV file for Batch Prediction", type=["csv"])
53
+ if file is not None:
54
+ if st.button("Predict for Batch", type='primary'):
55
+ response = requests.post(f"{backend_url}/v1/predict_sales_batch", files={"file": file})
56
 
57
+ if response.status_code == 200:
58
+ result = response.json()
59
+ st.header("Batch Prediction Results")
60
+ predictions_df = pd.DataFrame(result['Predicted_Sales_Batch'], columns=['Predicted_Sales'])
61
+ st.write(predictions_df)
62
+ else:
63
+ st.error(f"Error in API request: {response.status_code}")
64
+ st.error(f"Response: {response.text}")
 
 
 
 
 
requirements.txt CHANGED
@@ -1,8 +1,3 @@
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
 
1
  pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2