Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +16 -0
- app.py +83 -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,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="SuperKart Sales Forecaster", page_icon="๐", layout="wide")
|
| 6 |
+
|
| 7 |
+
# Backend API URL โ points to your deployed Flask backend
|
| 8 |
+
BACKEND_URL = "https://codedfortamara-superkart-backend.hf.space/predict"
|
| 9 |
+
|
| 10 |
+
st.title("๐ SuperKart Sales Revenue Forecaster")
|
| 11 |
+
st.markdown("Predict sales revenue for any product-store combination using our trained ML model.")
|
| 12 |
+
st.divider()
|
| 13 |
+
|
| 14 |
+
col1, col2 = st.columns(2)
|
| 15 |
+
|
| 16 |
+
with col1:
|
| 17 |
+
st.subheader("๐ฆ Product Details")
|
| 18 |
+
product_weight = st.number_input("Product Weight (kg)", min_value=1.0, max_value=25.0, value=12.0, step=0.1)
|
| 19 |
+
product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 20 |
+
product_allocated_area = st.slider("Allocated Display Area (ratio)", min_value=0.0, max_value=0.30, value=0.05, step=0.005, format="%.3f")
|
| 21 |
+
product_type = st.selectbox("Product Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods", "Dairy",
|
| 22 |
+
"Household", "Baking Goods", "Canned", "Health and Hygiene", "Meat", "Soft Drinks",
|
| 23 |
+
"Breads", "Hard Drinks", "Others", "Starchy Foods", "Breakfast", "Seafood"])
|
| 24 |
+
product_mrp = st.number_input("Product MRP", min_value=30.0, max_value=270.0, value=140.0, step=1.0)
|
| 25 |
+
product_category = st.selectbox("Product Category", ["FD", "NC", "DR"],
|
| 26 |
+
format_func=lambda x: {"FD": "FD โ Food", "NC": "NC โ Non-Consumable", "DR": "DR โ Drinks"}[x])
|
| 27 |
+
|
| 28 |
+
with col2:
|
| 29 |
+
st.subheader("๐ฌ Store Details")
|
| 30 |
+
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 31 |
+
store_location = st.selectbox("City Tier", ["Tier 1", "Tier 2", "Tier 3"])
|
| 32 |
+
store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
|
| 33 |
+
store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=20, step=1)
|
| 34 |
+
|
| 35 |
+
st.divider()
|
| 36 |
+
|
| 37 |
+
if st.button("๐ฎ Predict Sales Revenue", type="primary", use_container_width=True):
|
| 38 |
+
payload = {
|
| 39 |
+
"Product_Weight": product_weight,
|
| 40 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 41 |
+
"Product_Allocated_Area": product_allocated_area,
|
| 42 |
+
"Product_Type": product_type,
|
| 43 |
+
"Product_MRP": product_mrp,
|
| 44 |
+
"Store_Size": store_size,
|
| 45 |
+
"Store_Location_City_Type": store_location,
|
| 46 |
+
"Store_Type": store_type,
|
| 47 |
+
"Store_Age": store_age,
|
| 48 |
+
"Product_Category": product_category,
|
| 49 |
+
}
|
| 50 |
+
try:
|
| 51 |
+
response = requests.post(BACKEND_URL, json=payload, timeout=30)
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
prediction = response.json()["predictions"][0]
|
| 54 |
+
st.success(f"### ๐ฐ Predicted Sales Revenue: โน{prediction:,.2f}")
|
| 55 |
+
st.markdown("#### Input Summary")
|
| 56 |
+
st.dataframe(pd.DataFrame([payload]), use_container_width=True)
|
| 57 |
+
else:
|
| 58 |
+
st.error(f"API Error: {response.json().get('error', 'Unknown error')}")
|
| 59 |
+
except requests.exceptions.ConnectionError:
|
| 60 |
+
st.error("Could not connect to the backend. Check if the backend space is running.")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
st.error(f"Error: {str(e)}")
|
| 63 |
+
|
| 64 |
+
st.divider()
|
| 65 |
+
st.subheader("๐ Batch Prediction")
|
| 66 |
+
uploaded_file = st.file_uploader("Upload CSV for batch predictions", type=["csv"])
|
| 67 |
+
|
| 68 |
+
if uploaded_file is not None:
|
| 69 |
+
batch_df = pd.read_csv(uploaded_file)
|
| 70 |
+
st.dataframe(batch_df.head(), use_container_width=True)
|
| 71 |
+
if st.button("Run Batch Prediction", use_container_width=True):
|
| 72 |
+
try:
|
| 73 |
+
response = requests.post(BACKEND_URL, json=batch_df.to_dict(orient='records'), timeout=60)
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
batch_df["Predicted_Sales"] = response.json()["predictions"]
|
| 76 |
+
st.success(f"Predictions generated for {len(batch_df)} records!")
|
| 77 |
+
st.dataframe(batch_df, use_container_width=True)
|
| 78 |
+
st.download_button("Download Predictions", batch_df.to_csv(index=False),
|
| 79 |
+
"superkart_predictions.csv", "text/csv")
|
| 80 |
+
else:
|
| 81 |
+
st.error(f"API Error: {response.json().get('error', 'Unknown error')}")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
st.error(f"Error: {str(e)}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.45.1
|
| 2 |
+
requests==2.32.4
|
| 3 |
+
pandas==2.2.2
|