import streamlit as st import requests # --- Streamlit UI config --- st.set_page_config(page_title="SuperKart Sales Prediction", layout="centered") st.title("🛒 SuperKart Sales Prediction") st.write("Enter product and store features below to get a sales forecast.") # --- INPUT FIELDS --- product_weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.1, value=12.0) product_sugar = st.selectbox("Product Sugar Content", [0, 1]) product_alloc_area = st.number_input("Allocated Display Area (sq. m)", min_value=0.0, step=0.01, value=0.05) product_mrp = st.number_input("Product MRP", min_value=1.0, step=0.5, value=150.0) store_size = st.selectbox("Store Size", [0, 1, 2]) store_city_type = st.selectbox("Store Location City Type", [0, 1, 2]) store_type = st.selectbox("Store Type", [0, 1, 2, 3]) store_age = st.slider("Store Age (Years)", 0, 30, 10) product_type = st.selectbox("Product Category", [ "Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables", "Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood", "Snack Foods", "Soft Drinks", "Starchy Foods" ]) # --- One-hot encode the product type --- product_type_features = { f"Product_Type_{pt}": int(pt == product_type) for pt in [ "Breads", "Breakfast", "Canned", "Dairy", "Frozen Foods", "Fruits and Vegetables", "Hard Drinks", "Health and Hygiene", "Household", "Meat", "Others", "Seafood", "Snack Foods", "Soft Drinks", "Starchy Foods" ] } # --- Create input JSON --- input_data = { "Product_Weight": product_weight, "Product_Sugar_Content": product_sugar, "Product_Allocated_Area": product_alloc_area, "Product_MRP": product_mrp, "Store_Size": store_size, "Store_Location_City_Type": store_city_type, "Store_Type": store_type, "Store_Age": store_age, **product_type_features } if st.button("Predict Sales"): with st.spinner("Fetching prediction from backend..."): try: response = requests.post( "https://lokiiparihar-SuperkartBackendModalDeploy-XGBoost.hf.space/predict", json=input_data ) if response.status_code == 200: try: result = response.json() st.subheader("Raw Backend Response") #st.json(result) # SHOW FULL JSON RETURNED prediction = result.get("Predicted_Sales", None) except ValueError: prediction = response.text st.warning("⚠ Backend did not return JSON, showing raw text:") st.code(prediction) try: prediction = float(prediction) st.success(f"Predicted Sales: **{prediction:.2f} units**") except (ValueError, TypeError): st.error(f" Could not convert prediction to number: {prediction}") else: st.error(f"API Error: Status code {response.status_code}") st.text(response.text) except Exception as e: st.error(f" Request failed: {e}")