KarmaLoop commited on
Commit
cc05f1c
·
verified ·
1 Parent(s): 0e65c35

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -9
  2. app.py +62 -60
  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:churn_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,68 +1,70 @@
1
- import joblib
2
- import pandas as pd
3
- from flask import Flask, request, jsonify
4
- # Initialize Flask app with a name
5
- sale_predictor_api = Flask("Store sale Predictor")
6
 
7
 
8
- # Define a route for the home page
9
- @sale_predictor_api.get('/')
10
- def home():
11
- return "Welcome to the Sale Prediction API!"
12
 
13
- # Define an endpoint to predict sale for a single store
14
- @sale_predictor_api.post('/v1/store')
15
- def predict_sale():
16
- # Get JSON data from the request
17
- store_data = request.get_json()
18
 
19
- # Extract relevant customer features from the input data
20
- sample = {
21
- 'Product_Id': store_data['Product_Id'],
22
- 'Product_Weight': store_data['Product_Weight'],
23
- 'Product_Sugar_Content': store_data['Product_Sugar_Content'],
24
- 'Product_Allocated_Area': store_data['Product_Allocated_Area'],
25
- 'Product_Type': store_data['Product_Type'],
26
- 'Product_MRP': store_data['Product_MRP'],
27
- 'Store_Establishment_Year': store_data['Store_Establishment_Year'],
28
- 'Store_Size': store_data['Store_Size'],
29
- 'Store_Location_City_Type': store_data['Store_Location_City_Type'],
30
- 'Store_Type': store_data['Store_Type']
31
- }
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Convert the extracted data into a DataFrame
34
- input_data = pd.DataFrame([sample])
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Make a sale prediction using the trained model
37
- prediction = model.predict(input_data).tolist()[0]
 
 
 
 
 
 
38
 
39
- # Map prediction result to a human-readable label
40
- prediction_label = "sale" if prediction == 1 else "not sale"
41
 
42
- # Return the prediction as a JSON response
43
- return jsonify({'Prediction': prediction_label})
44
-
45
- # Define an endpoint to predict sale for a batch of customers
46
- @sale_predictor_api.post('/v1/storebatch')
47
- def predict_sale_batch():
48
- # Get the uploaded CSV file from the request
49
- file = request.files['file']
50
-
51
- # Read the file into a DataFrame
52
- input_data = pd.read_csv(file)
53
-
54
- # Make predictions for the batch data and convert raw predictions into a readable format
55
- predictions = [
56
- 'sale' if x == 1
57
- else "Not sale"
58
- for x in model.predict(input_data.drop("CustomerId",axis=1)).tolist()
59
- ]
60
-
61
- cust_id_list = input_data.CustomerId.values.tolist()
62
- output_dict = dict(zip(cust_id_list, predictions))
63
-
64
- return output_dict
65
-
66
- # Run the Flask app in debug mode
67
- if __name__ == '__main__':
68
- app.run(debug=True)
 
1
+ import streamlit as st
 
 
 
 
2
 
3
 
4
+ st.title("Store Sale Prediction")
 
 
 
5
 
6
+ # Batch Prediction
7
+ st.subheader("Online Prediction")
 
 
 
8
 
9
+ # Input fields for Store data
10
+ Product_Id = st.text_input("Product_Id : ")
11
+ Product_Weight = st.number_input("Product_Weight ", min_value=0, max_value=50, value=10)
12
+ Product_Sugar_Content = st.selectbox("Product_Sugar_Content ", ["Low Sugar", "Regular", and "no sugar"])
13
+ Product_Allocated_Area = st.number_input("Product_Allocated_Area", min_value=18, max_value=100, value=30)
14
+ Product_Type = st.selectbox("Product_Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods",
15
+ "Dairy",
16
+ "Household",
17
+ "Baking Goods",
18
+ "Canned",
19
+ "Health and Hygiene",
20
+ "Meat",
21
+ "Soft Drinks",
22
+ "Breads",
23
+ "Hard Drinks",
24
+ "Others",
25
+ "Starchy Foods",
26
+ "Breakfast",
27
+ "Seafood" ])
28
+ Product_MRP = st.number_input("Product_MRP", min_value=0.0, value=1000.0)
29
+ Store_Id = st.selectbox("Store_Id", ["OUT004","OUT001", "OUT003", "OUT002" ])
30
+ Store_Establishment_Year = st.number_input("Store_Establishment_Year", ["Yes", "No"])
31
+ Store_Size = st.selectbox("Store_Size", ["Medium","High","Small"])
32
+ Store_Location_City_Type = st.Store_Location_City_Type("Store_Location_City_Type", ["Tier 2","Tier 1","Tier 3"])
33
+ Store_Type = st.Store_Type("Store_Type", ["Supermarket Type2","Supermarket Type1","Departmental Store","Food Mart"])
34
 
35
+ Store_data = {
36
+ 'Product_Id' : Product_Id,
37
+ 'Product_Weight': Product_Weight,
38
+ 'Product_Sugar_Content': Product_Sugar_Content,
39
+ 'Product_Allocated_Area': Product_Allocated_Area,
40
+ 'Product_Type': Product_Type,
41
+ 'Product_MRP': Product_MRP,
42
+ 'Store_Id': Store_Id,
43
+ 'Store_Establishment_Year': Store_Establishment_Year,
44
+ 'Store_Size': Store_Size,
45
+ 'Store_Location_City_Type': Store_Location_City_Type,
46
+ 'Store_Type': Store_Type
47
+ }
48
 
49
+ if st.button("Predict", type='primary'):
50
+ response = requests.post("https://<user_name>-<space_name>.hf.space/v1/Store", json=Store_data) # enter user name and space name before running the cell
51
+ if response.status_code == 200:
52
+ result = response.json()
53
+ churn_prediction = result["Prediction"] # Extract only the value
54
+ st.write(f"Based on the information provided, the Store with ID {StoreID} is likely to {churn_prediction}.")
55
+ else:
56
+ st.error("Error in API request")
57
 
58
+ # Batch Prediction
59
+ st.subheader("Batch Prediction")
60
 
61
+ file = st.file_uploader("Upload CSV file", type=["csv"])
62
+ if file is not None:
63
+ if st.button("Predict for Batch", type='primary'):
64
+ response = requests.post("https://<user_name>-<space_name>.hf.space/v1/Storebatch", files={"file": file}) # enter user name and space name before running the cell
65
+ if response.status_code == 200:
66
+ result = response.json()
67
+ st.header("Batch Prediction Results")
68
+ st.write(result)
69
+ else:
70
+ st.error("Error in API request")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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