austinsd commited on
Commit
14d8481
·
verified ·
1 Parent(s): 608cfc9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -13
  2. app.py +85 -0
  3. requirements.txt +3 -2
Dockerfile CHANGED
@@ -1,20 +1,16 @@
1
- FROM python:3.13.5-slim
 
2
 
 
3
  WORKDIR /app
4
 
5
- RUN apt-get update && apt-get install -y \
6
- build-essential \
7
- curl \
8
- git \
9
- && rm -rf /var/lib/apt/lists/*
10
-
11
- COPY requirements.txt ./
12
- COPY src/ ./src/
13
 
 
14
  RUN pip3 install -r requirements.txt
15
 
16
- EXPOSE 8501
17
-
18
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
19
 
20
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
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,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+ # --- Configuration ---
7
+ # Replace with your deployed backend API URL
8
+ API_URL = "https://<YOUR_HF_USERNAME>-<YOUR_HF_SPACE_NAME>.hf.space/predict"
9
+ # Median Product_Allocated_Area from training data for handling zero values
10
+ MEDIAN_ALLOCATED_AREA = 0.056 # This value was derived from df['Product_Allocated_Area'].median()
11
+
12
+ st.title('SuperKart Sales Prediction')
13
+ st.markdown('Enter product and store details to predict sales total.')
14
+
15
+ # --- Input Fields ---
16
+ st.header('Product Details')
17
+ product_weight = st.number_input('Product Weight (e.g., 12.66)', min_value=0.1, max_value=30.0, value=12.66, step=0.01)
18
+ product_allocated_area = st.number_input('Product Allocated Area (e.g., 0.027, if 0 will be replaced by median)', min_value=0.0, max_value=0.3, value=0.027, step=0.001)
19
+ product_mrp = st.number_input('Product MRP (e.g., 117.08)', min_value=0.0, max_value=300.0, value=117.08, step=0.01)
20
+ product_sugar_content = st.selectbox(
21
+ 'Product Sugar Content',
22
+ ['Low Sugar', 'Regular', 'No Sugar']
23
+ )
24
+ product_type = st.selectbox(
25
+ 'Product Type',
26
+ ['Fruits and Vegetables', 'Snack Foods', 'Household', 'Frozen Foods', 'Dairy', 'Canned', 'Baking Goods',
27
+ 'Health and Hygiene', 'Soft Drinks', 'Meat', 'Breads', 'Hard Drinks', 'Breakfast', 'Starchy Foods', 'Seafood', 'Others']
28
+ )
29
+
30
+ st.header('Store Details')
31
+ store_establishment_year = st.number_input('Store Establishment Year (e.g., 2009)', min_value=1950, max_value=2023, value=2009, step=1)
32
+ store_id = st.selectbox(
33
+ 'Store ID',
34
+ ['OUT001', 'OUT003', 'OUT009', 'OUT005', 'OUT004', 'OUT010', 'OUT006', 'OUT007', 'OUT002', 'OUT008']
35
+ )
36
+ store_size = st.selectbox(
37
+ 'Store Size',
38
+ ['High', 'Medium', 'Small']
39
+ )
40
+ store_location_city_type = st.selectbox(
41
+ 'Store Location City Type',
42
+ ['Tier 1', 'Tier 2', 'Tier 3']
43
+ )
44
+ store_type = st.selectbox(
45
+ 'Store Type',
46
+ ['Supermarket Type1', 'Food Mart', 'Supermarket Type2', 'Departmental Store']
47
+ )
48
+
49
+ # --- Prediction Logic ---
50
+ if st.button('Predict Sales'):
51
+ # Preprocess inputs to match model's expected features
52
+ store_age = 2024 - store_establishment_year
53
+ product_visibility = product_allocated_area if product_allocated_area != 0 else MEDIAN_ALLOCATED_AREA
54
+
55
+ input_data = {
56
+ 'Product_Weight': product_weight,
57
+ 'Product_Allocated_Area': product_allocated_area, # Keep original for consistency with training data columns
58
+ 'Product_MRP': product_mrp,
59
+ 'Store_Age': store_age,
60
+ 'Product_Visibility': product_visibility,
61
+ 'Product_Sugar_Content': product_sugar_content,
62
+ 'Product_Type': product_type,
63
+ 'Store_Id': store_id,
64
+ 'Store_Size': store_size,
65
+ 'Store_Location_City_Type': store_location_city_type,
66
+ 'Store_Type': store_type
67
+ }
68
+
69
+ # Wrap input_data in a list because the backend expects a list of records
70
+ payload = [input_data]
71
+
72
+ try:
73
+ response = requests.post(API_URL, json=payload)
74
+ if response.status_code == 200:
75
+ prediction = response.json().get('prediction')
76
+ if prediction:
77
+ st.success(f"Predicted Product Store Sales Total: ${prediction[0]:,.2f}")
78
+ else:
79
+ st.error("Prediction not found in response.")
80
+ else:
81
+ st.error(f"Error from API: {response.status_code} - {response.text}")
82
+ except requests.exceptions.ConnectionError:
83
+ st.error("Could not connect to the API. Please ensure the backend is running and the API_URL is correct.")
84
+ except Exception as e:
85
+ st.error(f"An unexpected error occurred: {e}")
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
- altair
 
2
  pandas
3
- streamlit
 
1
+ streamlit
2
+ requests
3
  pandas
4
+ numpy