Himadri1102 commited on
Commit
d232a2b
·
verified ·
1 Parent(s): dc0f2f9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +101 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import json # Used for debugging output
5
+
6
+ # CRITICAL FIX: Use the actual URL of your running Docker Backend Space
7
+ # Replace 'Himadri1102-SuperKart-Model-Backend11' with the exact URL slug if different
8
+ BACKEND_URL = "https://Himadri1102-SuperKart-Model-Backend11.hf.space"
9
+ SINGLE_PREDICT_ENDPOINT = f"{BACKEND_URL}/v1/sales"
10
+ BATCH_PREDICT_ENDPOINT = f"{BACKEND_URL}/v1/salesbatch"
11
+
12
+ # --- UI Setup ---
13
+ st.title("SuperKart Sales Revenue Forecast")
14
+ st.markdown("Predicts the total sales revenue for a specific product and store combination.")
15
+
16
+ st.subheader("1. Product and Store Inputs")
17
+ st.markdown("⚠️ **WARNING**: The model requires ALL 29+ features (scaled/encoded). This UI collects only key features and defaults the rest for demonstration.")
18
+
19
+ # --- Collect Key User Input (Based on Bivariate Analysis) ---
20
+ product_mrp = st.number_input("Product MRP (Maximum Retail Price)", min_value=30.0, max_value=300.0, value=150.0, step=5.0)
21
+ product_weight = st.number_input("Product Weight", min_value=4.0, max_value=22.0, value=12.0, step=0.1)
22
+ store_age = st.number_input("Store Age (Years)", min_value=5, max_value=40, value=20)
23
+ store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
24
+
25
+ # --- Feature Preparation (Simulating Preprocessing Pipeline) ---
26
+
27
+ # CRITICAL: This mapping MUST match the Label Encoding in your training/pipeline code
28
+ store_size_encoded = {'Small': 0, 'Medium': 1, 'High': 2}[store_size]
29
+
30
+ # In a real pipeline, the model input must be SCALED (StandardScaler).
31
+ # For simplicity here, we assume the API pipeline handles the scaling,
32
+ # but the raw inputs are passed. However, the final pipeline expects the scaled value.
33
+ # To make this functional, we must provide mock OHE values.
34
+
35
+ def create_payload(mrp, weight, age, size_enc):
36
+ """
37
+ Creates a full feature payload, defaulting OHE features to zero,
38
+ and passing the key raw numerical/encoded values.
39
+ """
40
+ # NOTE: In a real environment, you must send the actual SCALED values
41
+ # of MRP, Weight, and Age, not the raw ones.
42
+ payload = {
43
+ 'Product_MRP': mrp,
44
+ 'Product_Weight': weight,
45
+ 'Store_Age': age,
46
+ 'Store_Size_Encoded': size_enc, # This is the encoded value
47
+
48
+ # Mocking the OHE features (defaulting most categories to False/0)
49
+ 'Product_Sugar_Content_No Sugar': False,
50
+ 'Product_Sugar_Content_Regular': True, # Default to Regular for testing
51
+ 'Product_Type_Snack Foods': True, # Default to Snack Foods for testing
52
+ 'Product_Type_Baking Goods': False,
53
+ 'Product_Type_Breads': False,
54
+ 'Store_Location_City_Type_Tier 2': True,
55
+ 'Store_Type_Supermarket Type1': True,
56
+ # ... and all other 20+ OHE features from your model input list...
57
+ # We only list a few for demonstration clarity.
58
+ }
59
+
60
+ # You need ALL 29+ feature keys here for the API call to work correctly.
61
+ # The current list is incomplete and will likely fail the model predict step.
62
+ return payload
63
+
64
+
65
+ # --- Prediction Logic ---
66
+ if st.button("Predict Sales Revenue"):
67
+
68
+ # 1. Create the full data payload
69
+ input_payload = create_payload(product_mrp, product_weight, store_age, store_size_encoded)
70
+
71
+ # 2. Call the Backend API
72
+ response = requests.post(SINGLE_PREDICT_ENDPOINT, json=input_payload)
73
+
74
+ if response.status_code == 200:
75
+ try:
76
+ prediction = response.json()['Predicted Total Sales']
77
+ st.success(f"📈 Predicted Sales Revenue: **${prediction:,.2f}**")
78
+ except KeyError:
79
+ st.error("Prediction successful but key 'Predicted Total Sales' was missing from API response.")
80
+ st.json(response.json())
81
+ else:
82
+ st.error(f"Error making prediction. Status Code: {response.status_code}")
83
+ st.json(response.json())
84
+
85
+
86
+ # --- Batch Prediction Section ---
87
+ st.subheader("2. Batch Prediction (CSV Upload)")
88
+
89
+ uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
90
+
91
+ if uploaded_file is not None:
92
+ if st.button("Predict Batch Sales"):
93
+ response = requests.post(BATCH_PREDICT_ENDPOINT, files={"file": uploaded_file})
94
+
95
+ if response.status_code == 200:
96
+ predictions = response.json()
97
+ st.success("Batch predictions completed!")
98
+ st.dataframe(pd.DataFrame(list(predictions.items()), columns=['ID', 'Predicted Sales']))
99
+ else:
100
+ st.error(f"Error making batch prediction. Status Code: {response.status_code}")
101
+ st.json(response.json())
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2