import os import time import streamlit as st import pandas as pd import requests DEFAULT_BACKEND_URL = "https://12ongo-backend.hf.space" # Try env var first; fall back to default backend_url_default = os.environ.get("BACKEND_URL", DEFAULT_BACKEND_URL) backend_url = st.text_input( "Backend URL", value=backend_url_default, help="Override if needed (e.g., local http://127.0.0.1:5000)", ) st.title("SuperKart Sales Prediction") with st.expander("Connection", expanded=False): if st.button("Test connection"): with st.spinner("Pinging backend..."): try: r = requests.get(f"{backend_url}/", timeout=30) st.write(f"Status: {r.status_code}") st.write(r.text[:200]) except Exception as e: st.error(f"Ping failed: {e}") st.subheader("Online Prediction") # Input widgets product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0, step=0.1) product_sugar_content = st.selectbox( "Product Sugar Content", ["low sugar", "regular", "no sugar"], ) product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.15, step=0.01) 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", ], ) product_mrp = st.number_input("Product MRP", min_value=0.0, value=99.0, step=0.5) store_before_2005 = st.checkbox("Store Established Before 2005?", value=True) store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) store_city_type = st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"]) store_type = st.selectbox( "Store Type", ["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"], ) if st.button("Predict Sales"): payload = { "Product_Weight": product_weight, "Product_Sugar_Content": product_sugar_content, "Product_Allocated_Area": product_allocated_area, "Product_Type": product_type, "Product_MRP": product_mrp, "Store_Establishment_Year_Before_2005": bool(store_before_2005), "Store_Size": store_size, "Store_Location_City_Type": store_city_type, "Store_Type": store_type, } try: with st.spinner("Requesting prediction (this may take up to 2 minutes if the backend is sleeping)..."): resp = requests.post(f"{backend_url}/v1/sales", json=payload, timeout=120) if resp.status_code == 200: st.success(f"Predicted Sales: {resp.json().get('Predicted_Sales')}") else: st.error(f"Error {resp.status_code}: {resp.text}") except Exception as e: st.error(f"Request failed: {e}")