nlauchande commited on
Commit
5fde683
·
verified ·
1 Parent(s): 081e655

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -9
  2. app.py +78 -63
  3. requirements.txt +0 -8
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:forecast_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,70 +1,85 @@
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
 
7
- # Initialize the Flask application
8
- forecast_predictor_api = Flask("Forecast Predictor")
9
 
10
- # Load the trained machine learning model
11
- model = joblib.load("SuperKartbest_xgb.joblib")
12
 
13
- @forecast_predictor_api.get('/')
14
- def home():
15
- return "Welcome to the Sales Prediction API!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- @forecast_predictor_api.post('/v1/predict')
18
- def predict_single():
19
- # Get the JSON data from the request body
20
- product_data = request.get_json()
21
-
22
- # Extract relevant features from the JSON data
23
- sample = {
24
- 'Product_Weight': product_data['Product_Weight'],
25
- 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
26
- 'Product_MRP': product_data['Product_MRP'],
27
- 'Store_Establishment_Year': product_data['Store_Establishment_Year'],
28
- 'Product_Sugar_Content_No Sugar': product_data['Product_Sugar_Content_No Sugar'],
29
- 'Product_Sugar_Content_Regular': product_data['Product_Sugar_Content_Regular'],
30
- 'Product_Sugar_Content_reg': product_data['Product_Sugar_Content_reg'],
31
- 'Product_Type_Breads': product_data['Product_Type_Breads'],
32
- 'Product_Type_Breakfast': product_data['Product_Type_Breakfast'],
33
- 'Product_Type_Canned': product_data['Product_Type_Canned'],
34
- 'Product_Type_Dairy': product_data['Product_Type_Dairy'],
35
- 'Product_Type_Frozen Foods': product_data['Product_Type_Frozen Foods'],
36
- 'Product_Type_Fruits and Vegetables': product_data['Product_Type_Fruits and Vegetables'],
37
- 'Product_Type_Hard Drinks': product_data['Product_Type_Hard Drinks'],
38
- 'Product_Type_Health and Hygiene': product_data['Product_Type_Health and Hygiene'],
39
- 'Product_Type_Household': product_data['Product_Type_Household'],
40
- 'Product_Type_Meat': product_data['Product_Type_Meat'],
41
- 'Product_Type_Others': product_data['Product_Type_Others'],
42
- 'Product_Type_Seafood': product_data['Product_Type_Seafood'],
43
- 'Product_Type_Snack Foods': product_data['Product_Type_Snack Foods'],
44
- 'Product_Type_Soft Drinks': product_data['Product_Type_Soft Drinks'],
45
- 'Product_Type_Starchy Foods': product_data['Product_Type_Starchy Foods'],
46
- 'Store_Size_Medium': product_data['Store_Size_Medium'],
47
- 'Store_Size_Small': product_data['Store_Size_Small'],
48
- 'Store_Location_City_Type_Tier 2': product_data['Store_Location_City_Type_Tier 2'],
49
- 'Store_Location_City_Type_Tier 3': product_data['Store_Location_City_Type_Tier 3'],
50
- 'Store_Type_Food Mart': product_data['Store_Type_Food Mart'],
51
- 'Store_Type_Supermarket Type1': product_data['Store_Type_Supermarket Type1'],
52
- 'Store_Type_Supermarket Type2': product_data['Store_Type_Supermarket Type2']
53
- }
54
- # Convert the extracted data into a Pandas DataFrame
55
- input_data = pd.DataFrame([sample])
56
 
57
- # Make prediction (get log_price)
58
- predicted_price = model.predict(input_data)[0]
59
 
60
- # Convert predicted_price to Python float
61
- predicted_price = round(float(predicted_price), 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 Price (in dollars)': predicted_price})
67
-
68
- # Run the Flask application in debug mode if this script is executed directly
69
- if __name__ == '__main__':
70
- forecast_predictor_api.run(debug=True)
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
 
 
4
 
5
+ # Set the title of the Streamlit app
6
+ st.title("Sales Prediction")
7
 
8
+ # Section for online prediction
9
+ st.subheader("Online Prediction")
10
 
11
+ # Collect user input for product and store features
12
+ Product_Weight = st.number_input("Product Weight", min_value=0.0, value=15.0)
13
+ Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, value=200.0)
14
+ Product_MRP = st.number_input("Product MRP", min_value=0.0, value=100.0)
15
+ Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1900, max_value=2024, value=2000)
16
+ Product_Sugar_Content_No_Sugar = st.selectbox("Product Sugar Content No Sugar", [0, 1])
17
+ Product_Sugar_Content_Regular = st.selectbox("Product Sugar Content Regular", [0, 1])
18
+ Product_Sugar_Content_reg = st.selectbox("Product Sugar Content reg", [0, 1])
19
+ Product_Type_Breads = st.selectbox("Product Type Breads", [0, 1])
20
+ Product_Type_Breakfast = st.selectbox("Product Type Breakfast", [0, 1])
21
+ Product_Type_Canned = st.selectbox("Product Type Canned", [0, 1])
22
+ Product_Type_Dairy = st.selectbox("Product Type Dairy", [0, 1])
23
+ Product_Type_Frozen_Foods = st.selectbox("Product Type Frozen Foods", [0, 1])
24
+ Product_Type_Fruits_and_Vegetables = st.selectbox("Product Type Fruits and Vegetables", [0, 1])
25
+ Product_Type_Hard_Drinks = st.selectbox("Product Type Hard Drinks", [0, 1])
26
+ Product_Type_Health_and_Hygiene = st.selectbox("Product Type Health and Hygiene", [0, 1])
27
+ Product_Type_Household = st.selectbox("Product Type Household", [0, 1])
28
+ Product_Type_Meat = st.selectbox("Product Type Meat", [0, 1])
29
+ Product_Type_Others = st.selectbox("Product Type Others", [0, 1])
30
+ Product_Type_Seafood = st.selectbox("Product Type Seafood", [0, 1])
31
+ Product_Type_Snack_Foods = st.selectbox("Product Type Snack Foods", [0, 1])
32
+ Product_Type_Soft_Drinks = st.selectbox("Product Type Soft Drinks", [0, 1])
33
+ Product_Type_Starchy_Foods = st.selectbox("Product Type Starchy Foods", [0, 1])
34
+ Store_Size_Medium = st.selectbox("Store Size Medium", [0, 1])
35
+ Store_Size_Small = st.selectbox("Store Size Small", [0, 1])
36
+ Store_Location_City_Type_Tier_2 = st.selectbox("Store Location City Type Tier 2", [0, 1])
37
+ Store_Location_City_Type_Tier_3 = st.selectbox("Store Location City Type Tier 3", [0, 1])
38
+ Store_Type_Food_Mart = st.selectbox("Store Type Food Mart", [0, 1])
39
+ Store_Type_Supermarket_Type1 = st.selectbox("Store Type Supermarket Type1", [0, 1])
40
+ Store_Type_Supermarket_Type2 = st.selectbox("Store Type Supermarket Type2", [0, 1])
41
 
42
+ # Convert user input into a DataFrame
43
+ input_data = pd.DataFrame([{
44
+ 'Product_Weight': Product_Weight,
45
+ 'Product_Allocated_Area': Product_Allocated_Area,
46
+ 'Product_MRP': Product_MRP,
47
+ 'Store_Establishment_Year': Store_Establishment_Year,
48
+ 'Product_Sugar_Content_No Sugar': Product_Sugar_Content_No_Sugar,
49
+ 'Product_Sugar_Content_Regular': Product_Sugar_Content_Regular,
50
+ 'Product_Sugar_Content_reg': Product_Sugar_Content_reg,
51
+ 'Product_Type_Breads': Product_Type_Breads,
52
+ 'Product_Type_Breakfast': Product_Type_Breakfast,
53
+ 'Product_Type_Canned': Product_Type_Canned,
54
+ 'Product_Type_Dairy': Product_Type_Dairy,
55
+ 'Product_Type_Frozen Foods': Product_Type_Frozen_Foods,
56
+ 'Product_Type_Fruits and Vegetables': Product_Type_Fruits_and_Vegetables,
57
+ 'Product_Type_Hard Drinks': Product_Type_Hard_Drinks,
58
+ 'Product_Type_Health and Hygiene': Product_Type_Health_and_Hygiene,
59
+ 'Product_Type_Household': Product_Type_Household,
60
+ 'Product_Type_Meat': Product_Type_Meat,
61
+ 'Product_Type_Others': Product_Type_Others,
62
+ 'Product_Type_Seafood': Product_Type_Seafood,
63
+ 'Product_Type_Snack Foods': Product_Type_Snack_Foods,
64
+ 'Product_Type_Soft Drinks': Product_Type_Soft_Drinks,
65
+ 'Product_Type_Starchy Foods': Product_Type_Starchy_Foods,
66
+ 'Store_Size_Medium': Store_Size_Medium,
67
+ 'Store_Size_Small': Store_Size_Small,
68
+ 'Store_Location_City_Type_Tier 2': Store_Location_City_Type_Tier_2,
69
+ 'Store_Location_City_Type_Tier 3': Store_Location_City_Type_Tier_3,
70
+ 'Store_Type_Food Mart': Store_Type_Food_Mart,
71
+ 'Store_Type_Supermarket Type1': Store_Type_Supermarket_Type1,
72
+ 'Store_Type_Supermarket Type2': Store_Type_Supermarket_Type2
73
+ }])
 
 
 
 
 
 
 
74
 
 
 
75
 
76
+ https://huggingface.co/spaces/nlauchande/ForecastBackend/v1/predict
 
 
 
77
 
78
+ # Make prediction when the "Predict" button is clicked
79
+ if st.button("Predict"):
80
+ response = requests.post("https://nlauchande-nlauchande/ForecastBackend.hf.space/v1/predict", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
81
+ if response.status_code == 200:
82
+ prediction = response.json()['Predicted Sales']
83
+ st.success(f"Predicted Sales: {prediction}")
84
+ else:
85
+ st.error("Error making prediction.")
requirements.txt CHANGED
@@ -1,11 +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
9
  requests==2.28.1
10
- uvicorn[standard]
11
  streamlit==1.43.2
 
1
  pandas==2.2.2
 
 
 
 
 
 
 
2
  requests==2.28.1
 
3
  streamlit==1.43.2