import os import json import requests import pandas as pd import streamlit as st st.set_page_config(page_title="SuperKart Forecast UI", layout="wide") DEFAULT_BACKEND = os.environ.get("BACKEND_URL", "http://127.0.0.1:8000") BACKEND_URL = st.sidebar.text_input("Backend base URL", DEFAULT_BACKEND) st.sidebar.caption("Example: https://.hf.space") if st.sidebar.button("Test Backend Health"): try: r = requests.get(f"{BACKEND_URL}/health", timeout=15) if r.ok: st.sidebar.success(r.json()) else: st.sidebar.error(f"{r.status_code}: {r.text}") except Exception as e: st.sidebar.error(f"Failed: {e}") st.title("SuperKart Sales Forecast") st.write("Fill in the features below. This UI uses **Store_Establishment_Year** (not Store_Age).") with st.form("single_pred"): col1, col2, col3 = st.columns(3) with col1: store_est_year = st.number_input("Store_Establishment_Year", min_value=1900, max_value=2100, value=2014, step=1) product_weight = st.number_input("Product_Weight (kg)", min_value=0.0, value=0.45, step=0.01, format="%.2f") product_alloc_area = st.number_input("Product_Allocated_Area (0-1)", min_value=0.0, max_value=1.0, value=0.08, step=0.01, format="%.2f") product_mrp = st.number_input("Product_MRP", min_value=0.0, value=1200.0, step=1.0, format="%.2f") with col2: store_size = st.selectbox("Store_Size", ["High", "Medium", "Low"]) store_loc_city_type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"]) store_type = st.selectbox("Store_Type", ["Supermarket Type 1", "Supermarket Type 2", "Departmental Store", "Food Mart"]) with col3: product_sugar_content = st.selectbox("Product_Sugar_Content", ["Regular", "Low Sugar", "No Sugar"]) product_type = st.selectbox( "Product_Type", [ "Meat", "Snack foods", "Hard drinks", "Dairy", "Canned", "Soft drinks", "Health and hygiene", "Baking goods", "Bread", "Breakfast", "Frozen foods", "Fruits and vegetables", "Household", "Seafood", "Starchy foods", "Others" ], index=1 ) submitted = st.form_submit_button("Predict") payload = { "Store_Establishment_Year": int(store_est_year), "Product_Weight": float(product_weight), "Product_Allocated_Area": float(product_alloc_area), "Product_MRP": float(product_mrp), "Store_Size": store_size, "Store_Location_City_Type": store_loc_city_type, "Store_Type": store_type, "Product_Sugar_Content": product_sugar_content, "Product_Type": product_type, } with st.expander("Request JSON preview"): st.code(json.dumps(payload, indent=2), language="json") if submitted: try: r = requests.post(f"{BACKEND_URL}/predict", json=payload, timeout=25) if r.ok: st.success(r.json()) else: st.error(f"{r.status_code}: {r.text}") except Exception as e: st.error(f"Prediction failed: {e}") st.markdown("---") st.subheader("Batch prediction via CSV") st.caption("Include training columns (e.g., Store_Establishment_Year, Product_Weight, Product_Allocated_Area, Product_MRP, Store_Size, Store_Location_City_Type, Store_Type, Product_Sugar_Content, Product_Type).") uploaded = st.file_uploader("Upload CSV", type=["csv"]) if uploaded is not None: try: df_preview = pd.read_csv(uploaded) st.write("Preview:", df_preview.head()) uploaded.seek(0) files = {"file": ("batch.csv", uploaded, "text/csv")} r = requests.post(f"{BACKEND_URL}/predict-csv", files=files, timeout=60) if r.ok: resp = r.json() st.success(f"Rows: {resp.get('n')}") st.json(resp if len(str(resp)) < 1200 else {"message": "Predictions received.", "keys": list(resp.keys())}) else: st.error(f"{r.status_code}: {r.text}") except Exception as e: st.error(f"CSV prediction failed: {e}")