Tamilvelan commited on
Commit
e47468d
·
verified ·
1 Parent(s): 4db3ed8

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +90 -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,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("🛒 SuperKart Sales Total Predictor")
7
+ st.markdown("Forecasting product sales based on product characteristics and store type.")
8
+
9
+ # --- API Endpoint Configuration (Replace placeholders with your actual Space URL) ---
10
+ # NOTE: Replace <username>-<repo_id> with the actual ID of your Hugging Face Space.
11
+ API_BASE_URL = "https://Tamilvelan-StoreSalesPredictionBackend.hf.space"
12
+ ONLINE_PREDICTION_URL = f"{API_BASE_URL}/v1/sales"
13
+ # -----------------------------------------------------------------------------------
14
+
15
+
16
+ # Section for online prediction
17
+ st.subheader("Predict Single Product-Store Sales")
18
+
19
+ # --- Collect user input for SuperKart features ---
20
+
21
+ # Numerical and Engineered Features
22
+ col1, col2 = st.columns(2)
23
+ with col1:
24
+ product_weight = st.number_input("Product Weight (kg)", min_value=1.0, max_value=25.0, value=12.02, step=0.01)
25
+ product_mrp = st.number_input("Product MRP ($)", min_value=10.0, max_value=300.0, value=141.6, step=0.01)
26
+ store_age = st.number_input("Store Age (Years)", min_value=1, max_value=50, value=25, help="Calculated as (Current Year - Store Establishment Year)")
27
+
28
+ with col2:
29
+ # Categorical Features (We assume the input still needs the RAW categorical feature for the full Pipeline to work)
30
+ product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
31
+ product_type = st.selectbox("Product Type", ['Fruits and Vegetables', 'Snack Foods', 'Soft Drinks', 'Dairy', 'Baking Goods', 'Household', 'Others', 'Meat', 'Frozen Foods', 'Breakfast', 'Canned', 'Starchy Foods', 'Health and Hygiene', 'Fats and Oils', 'Seafood'])
32
+ store_type = st.selectbox("Store Type", ['Supermarket Type 1', 'Departmental Store', 'Supermarket Type 2', 'Food Mart'])
33
+
34
+ # Ordinal Encoded Features (Using the user-friendly category, but mapping to the expected ENCODED value)
35
+ # NOTE: The Backend API assumes it receives the ENCODED value (0, 1, 2, 3), not the raw string!
36
+ store_size_map = {"Low": 0, "Medium": 1, "High": 2}
37
+ store_city_map = {"Tier 3": 1, "Tier 2": 2, "Tier 1": 3}
38
+
39
+ store_size_raw = st.selectbox("Store Size", list(store_size_map.keys()))
40
+ store_location_city_type_raw = st.selectbox("Store Location City Type", list(store_city_map.keys()))
41
+
42
+ # --- Special Engineered Input for Allocated Area Log ---
43
+ # Since the model expects a LOG-TRANSFORMED value, we must either:
44
+ # 1. Ask the user for the raw value and perform log1p here (cleaner)
45
+ # 2. Ask the user for the log value (less intuitive)
46
+
47
+ # We will ask for the raw value and transform it before sending to the API
48
+ product_allocated_area_raw = st.number_input("Product Allocated Area (Raw Value)", min_value=0.0, value=0.05, step=0.01)
49
+ product_allocated_area_log = float(np.log1p(product_allocated_area_raw)) # Calculate log1p(x)
50
+
51
+ # --- Prepare Data Payload ---
52
+ import numpy as np # Import numpy here for log1p calculation
53
+
54
+ # Convert user input into a dictionary matching the API's expected JSON structure
55
+ input_payload = {
56
+ 'Product_Weight': product_weight,
57
+ 'Product_MRP': product_mrp,
58
+ 'Store_Age': store_age,
59
+ 'Product_Allocated_Area_Log': product_allocated_area_log,
60
+
61
+ # Send the encoded integer values for ordinal features as the API expects them
62
+ 'Store_Size_Encoded': store_size_map[store_size_raw],
63
+ 'Store_Location_City_Type_Encoded': store_city_map[store_location_city_type_raw],
64
+
65
+ # Send the raw string values for nominal features (assuming the model pipeline handles OHE)
66
+ 'Product_Sugar_Content': product_sugar_content,
67
+ 'Product_Type': product_type,
68
+ 'Store_Type': store_type,
69
+ }
70
+
71
+
72
+ # Make prediction when the "Predict" button is clicked
73
+ if st.button("Predict Sales Total"):
74
+ try:
75
+ # Send data to Flask API
76
+ response = requests.post(ONLINE_PREDICTION_URL, json=input_payload)
77
+
78
+ if response.status_code == 200:
79
+ prediction = response.json().get('Predicted Sales Total (in dollars)')
80
+ if prediction is not None:
81
+ st.success(f"Predicted Product Sales Total: **${prediction:,.2f}**")
82
+ else:
83
+ st.error("Prediction key not found in API response.")
84
+ else:
85
+ st.error(f"Error making prediction. Status code: {response.status_code}. Response: {response.text}")
86
+ except requests.exceptions.RequestException as e:
87
+ st.error(f"An error occurred while connecting to the API: {e}")
88
+
89
+
90
+ st.markdown("---")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2