Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,60 +2,62 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
"Product Type Category",
|
| 25 |
[
|
| 26 |
-
"Fruits and Vegetables","Snack Foods","Household","Frozen Foods","Dairy",
|
| 27 |
-
"Baking Goods","Health and Hygiene","Hard Drinks","Soft Drinks","Meat",
|
| 28 |
-
"Canned","Breads","Breakfast","Seafood","Others"
|
| 29 |
]
|
| 30 |
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
current_year = datetime.now().year
|
| 34 |
payload = {
|
| 35 |
-
"Product_Weight":
|
| 36 |
-
"Product_Sugar_Content":
|
| 37 |
-
"Product_Allocated_Area":
|
| 38 |
-
"Product_MRP":
|
| 39 |
-
"Store_Size":
|
| 40 |
-
"Store_Location_City_Type":
|
| 41 |
-
"Store_Type":
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"Product_Type": Product_Type_Category # map Product_Type_Category -> Product_Type
|
| 45 |
-
# (Optional) If your model expects Store_Id, add another text_input and include it here
|
| 46 |
}
|
| 47 |
|
| 48 |
-
# ---- Call your backend Space (
|
| 49 |
-
API_URL = "https://
|
| 50 |
|
| 51 |
if st.button("Predict", type="primary"):
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
+
# App title and description
|
| 6 |
+
st.set_page_config(page_title="SuperKart Sales Forecasting", page_icon="🛒", layout="centered")
|
| 7 |
+
st.title("🛒 SuperKart Sales Forecasting App")
|
| 8 |
+
st.markdown("Enter product and store attributes to forecast monthly product sales revenue.")
|
| 9 |
+
st.markdown("*All sales are reported in ($) USD.*")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# --- Input fields for product and store data (matching screenshot) ---
|
| 13 |
+
# Corrected input widgets to align with the model's training data
|
| 14 |
+
product_weight = st.number_input("Product Weight (oz)", min_value=0.0, value=12.66, step=0.1)
|
| 15 |
+
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
|
| 16 |
+
product_allocated_area = st.number_input("Product Allocated Area (linear in.)", min_value=0.0, value=100.00, step=1.0)
|
| 17 |
+
product_mrp = st.number_input("Maximum Retail Price (USD)", min_value=0.0, value=150.00, step=0.5)
|
| 18 |
+
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 19 |
+
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 20 |
+
store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
|
| 21 |
+
store_age_years = st.slider("Store Age (years)", min_value=0, max_value=30, value=10, step=1)
|
| 22 |
+
|
| 23 |
+
# Corrected Product Type categories to match the dataset
|
| 24 |
+
product_type_category = st.selectbox(
|
| 25 |
"Product Type Category",
|
| 26 |
[
|
| 27 |
+
"Fruits and Vegetables", "Snack Foods", "Household", "Frozen Foods", "Dairy",
|
| 28 |
+
"Baking Goods", "Health and Hygiene", "Hard Drinks", "Soft Drinks", "Meat",
|
| 29 |
+
"Canned", "Breads", "Breakfast", "Seafood", "Starchy Foods", "Others"
|
| 30 |
]
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# --- Prepare payload for the backend API ---
|
| 34 |
current_year = datetime.now().year
|
| 35 |
payload = {
|
| 36 |
+
"Product_Weight": product_weight,
|
| 37 |
+
"Product_Sugar_Content": product_sugar_content,
|
| 38 |
+
"Product_Allocated_Area": product_allocated_area / 1000, # Normalize based on EDA insights
|
| 39 |
+
"Product_MRP": product_mrp,
|
| 40 |
+
"Store_Size": store_size,
|
| 41 |
+
"Store_Location_City_Type": store_location_city_type,
|
| 42 |
+
"Store_Type": store_type,
|
| 43 |
+
"Store_Establishment_Year": int(current_year - store_age_years),
|
| 44 |
+
"Product_Type": product_type_category
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# ---- Call your backend Space (URL is now correct and hardcoded) ----
|
| 48 |
+
API_URL = "https://gigcode-superkartbackend.hf.space/v1/sales"
|
| 49 |
|
| 50 |
if st.button("Predict", type="primary"):
|
| 51 |
+
with st.spinner('Forecasting...'):
|
| 52 |
+
try:
|
| 53 |
+
resp = requests.post(API_URL, json=payload, timeout=30)
|
| 54 |
+
if resp.status_code == 200:
|
| 55 |
+
result = resp.json()
|
| 56 |
+
predicted_sales = result.get("Predicted Product_Store_Sales_Total", 0)
|
| 57 |
+
st.success(f"Predicted Product Store Sales Total: ${float(predicted_sales):,.2f}")
|
| 58 |
+
else:
|
| 59 |
+
st.error(f"API error [{resp.status_code}]: {resp.text}")
|
| 60 |
+
except requests.exceptions.RequestException as e:
|
| 61 |
+
st.error(f"Request failed: Could not connect to the API. Please ensure the backend is running. Error: {e}")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"An unexpected error occurred: {e}")
|