Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +125 -0
- 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,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# -------------------------
|
| 7 |
+
# SuperKart - Sales Forecast Streamlit App (Online Prediction only)
|
| 8 |
+
# -------------------------
|
| 9 |
+
st.set_page_config(page_title="SuperKart Sales Forecast", layout="wide")
|
| 10 |
+
st.title("SuperKart — Outlet Sales Forecasting")
|
| 11 |
+
st.markdown(
|
| 12 |
+
"Use this app to get **online** sales forecasts for a product at a specific store."
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# ----- Online prediction -----
|
| 16 |
+
st.subheader("Online Prediction (single product-store pair)")
|
| 17 |
+
|
| 18 |
+
# Product fields
|
| 19 |
+
with st.expander("Product details", expanded=True):
|
| 20 |
+
product_id = st.text_input("Product_Id (e.g. PR123)", value="PR1001")
|
| 21 |
+
product_weight = st.number_input("Product_Weight (numeric)", min_value=0.0, value=250.0, format="%.3f")
|
| 22 |
+
product_sugar_content = st.selectbox(
|
| 23 |
+
"Product_Sugar_Content",
|
| 24 |
+
["Low Sugar", "Regular", "No Sugar"]
|
| 25 |
+
)
|
| 26 |
+
product_allocated_area = st.number_input(
|
| 27 |
+
"Product_Allocated_Area (ratio 0-1)",
|
| 28 |
+
min_value=0.0, max_value=1.0, value=0.05, format="%.4f"
|
| 29 |
+
)
|
| 30 |
+
product_type = st.selectbox(
|
| 31 |
+
"Product_Type",
|
| 32 |
+
[
|
| 33 |
+
"meat", "snack foods", "hard drinks", "dairy", "canned", "soft drinks",
|
| 34 |
+
"health and hygiene", "baking goods", "bread", "breakfast", "frozen foods",
|
| 35 |
+
"fruits and vegetables", "household", "seafood", "starchy foods", "others"
|
| 36 |
+
]
|
| 37 |
+
)
|
| 38 |
+
product_mrp = st.number_input("Product_MRP (maximum retail price)", min_value=0.0, value=99.0, format="%.2f")
|
| 39 |
+
|
| 40 |
+
# Store fields
|
| 41 |
+
with st.expander("Store details", expanded=True):
|
| 42 |
+
store_id = st.text_input("Store_Id (e.g. ST100)", value="ST100")
|
| 43 |
+
store_est_year = st.number_input(
|
| 44 |
+
"Store_Establishment_Year",
|
| 45 |
+
min_value=1900,
|
| 46 |
+
max_value=datetime.now().year,
|
| 47 |
+
value=2015,
|
| 48 |
+
step=1
|
| 49 |
+
)
|
| 50 |
+
store_size = st.selectbox("Store_Size", ["Low", "Medium", "High"])
|
| 51 |
+
store_location_city_type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 52 |
+
store_type = st.selectbox("Store_Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"])
|
| 53 |
+
|
| 54 |
+
# Prepare input dataframe
|
| 55 |
+
input_data = pd.DataFrame([{
|
| 56 |
+
"Product_Id": product_id,
|
| 57 |
+
"Product_Weight": product_weight,
|
| 58 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 59 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 60 |
+
"Product_Type": product_type,
|
| 61 |
+
"Product_MRP": product_mrp,
|
| 62 |
+
"Store_Id": store_id,
|
| 63 |
+
"Store_Establishment_Year": int(store_est_year),
|
| 64 |
+
"Store_Size": store_size,
|
| 65 |
+
"Store_Location_City_Type": store_location_city_type,
|
| 66 |
+
"Store_Type": store_type
|
| 67 |
+
}])
|
| 68 |
+
|
| 69 |
+
st.markdown("**Preview of the input that will be sent to the model endpoint:**")
|
| 70 |
+
st.dataframe(input_data)
|
| 71 |
+
|
| 72 |
+
# -------------------------
|
| 73 |
+
# Configure your backend endpoint here
|
| 74 |
+
# -------------------------
|
| 75 |
+
PREDICTION_API_URL = "https://viveksardey-superkartsalesrevpredictionbackend.hf.space/v1/forecast" # single-record prediction (POST JSON)
|
| 76 |
+
|
| 77 |
+
# Make prediction when the "Predict" button is clicked
|
| 78 |
+
if st.button("Predict Sales (Online)"):
|
| 79 |
+
try:
|
| 80 |
+
payload = input_data.to_dict(orient="records")[0]
|
| 81 |
+
response = requests.post(PREDICTION_API_URL, json=payload, timeout=30)
|
| 82 |
+
if response.status_code == 200:
|
| 83 |
+
try:
|
| 84 |
+
resp_json = response.json()
|
| 85 |
+
except ValueError:
|
| 86 |
+
st.success(f"Prediction response (non-JSON): {response.text}")
|
| 87 |
+
st.stop()
|
| 88 |
+
|
| 89 |
+
# common keys to search for in response
|
| 90 |
+
predicted = None
|
| 91 |
+
for key in ("predicted_sales", "Predicted Sales", "Predicted_Sales", "prediction", "sales", "predicted"):
|
| 92 |
+
if key in resp_json:
|
| 93 |
+
predicted = resp_json[key]
|
| 94 |
+
break
|
| 95 |
+
|
| 96 |
+
# fallback handling
|
| 97 |
+
if predicted is None:
|
| 98 |
+
if isinstance(resp_json, (int, float, str)):
|
| 99 |
+
predicted = resp_json
|
| 100 |
+
elif isinstance(resp_json, dict) and len(resp_json) == 1:
|
| 101 |
+
predicted = list(resp_json.values())[0]
|
| 102 |
+
|
| 103 |
+
if predicted is not None:
|
| 104 |
+
st.success(f"Predicted Sales Revenue for the quarter: {predicted}")
|
| 105 |
+
else:
|
| 106 |
+
st.write(resp_json)
|
| 107 |
+
st.info("Couldn't find a standard 'predicted_sales' field — raw response shown above.")
|
| 108 |
+
else:
|
| 109 |
+
st.error(f"Prediction API returned status {response.status_code}")
|
| 110 |
+
try:
|
| 111 |
+
st.write(response.json())
|
| 112 |
+
except Exception:
|
| 113 |
+
st.write(response.text)
|
| 114 |
+
except requests.exceptions.RequestException as e:
|
| 115 |
+
st.error(f"Error connecting to prediction API: {e}")
|
| 116 |
+
|
| 117 |
+
# ----- Helpful notes -----
|
| 118 |
+
st.markdown("---")
|
| 119 |
+
st.markdown(
|
| 120 |
+
"**Notes & tips:**\n\n"
|
| 121 |
+
"- Replace `PREDICTION_API_URL` with your deployed model endpoint.\n"
|
| 122 |
+
"- Ensure your backend accepts the same column names and formats as used in this frontend.\n"
|
| 123 |
+
"- For production, secure your endpoint (authentication, TLS) and validate inputs on the backend.\n"
|
| 124 |
+
"- Common backend response format: `{\"predicted_sales\": <number>}`.\n"
|
| 125 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
requests==2.28.1
|
| 3 |
+
streamlit==1.43.2
|