import streamlit as st import requests import pandas as pd st.set_page_config(page_title="SuperKart Sales Prediction", page_icon="🛒", layout="centered") st.title("🛒 SuperKart Sales Prediction") # --- API base (set your HF Space URL here) --- # api_url = st.text_input( # "API Base URL", # value="https://vinodcwanted-SuperKart.hf.space", # help="Your Flask API base (no trailing slash). Example: https://thiresh-rentalpricepredictionbackend.hf.space", # ) api_url ="https://vinodcwanted-SuperKart.hf.space" st.markdown("### Product Details") col1, col2 = st.columns(2) with col1: product_weight = st.number_input("Product_Weight", min_value=0.0, value=12.5, step=0.1) product_alloc_area = st.number_input("Product_Allocated_Area", min_value=0.0, value=0.05, step=0.001, format="%.3f") product_mrp = st.number_input("Product_MRP", min_value=0.0, value=150.0, step=0.1) product_id = st.text_input("Product_Id (optional)", value="", help="If provided, API derives product_categories from its prefix (FD/NC/DR).") with col2: product_sugar_content = st.selectbox("Product_Sugar_Content", ["Low Sugar", "Regular", "No Sugar"]) product_type = st.selectbox( "Product_Type", [ "Frozen Foods","Dairy","Canned","Baking Goods","Health and Hygiene","Snack Foods","Meat", "Household","Hard Drinks","Fruits and Vegetables","Breads","Soft Drinks","Breakfast", "Others","Starchy Foods","Seafood" ], ) product_categories = st.selectbox( "product_categories", ["FD","NC","DR"], help="'FD':'Food_and_Vegetables','NC':'Non_Consumable','DR':'Drinks'" ) st.markdown("### Store Details") col3, col4 = st.columns(2) with col3: store_size = st.selectbox("Store_Size", ["Small", "Medium", "High"]) store_type = st.selectbox("Store_Type", ["Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"]) with col4: store_city_type = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"]) store_est_year = st.number_input("Store_Establishment_Year", min_value=1900, max_value=2025, value=2005, step=1, help="API will compute Establishment_age = 2025 - this year.") # Build payload (single JSON) payload = { "Product_Weight": product_weight, "Product_Sugar_Content": product_sugar_content, "Product_Allocated_Area": product_alloc_area, "Product_Type": product_type, "Product_MRP": product_mrp, "Store_Establishment_Year": int(store_est_year), "Store_Size": store_size, "Store_Location_City_Type": store_city_type, "Store_Type": store_type, } # Optional fields if product_id.strip(): payload["Product_Id"] = product_id.strip() if product_categories != "(leave blank)": payload["product_categories"] = product_categories st.markdown("---") if st.button("Predict"): if not api_url.strip(): st.error("Please enter your API Base URL.") else: try: endpoint = api_url.rstrip("/") + "/v1/sale" with st.spinner("Contacting API..."): resp = requests.post(endpoint, json=payload, timeout=30) if resp.status_code == 200: data = resp.json() if "prediction" in data: st.success(f"Predicted Sales: **{data['prediction']}**") else: st.warning(f"API responded without 'prediction' key:\n{data}") else: st.error(f"API error {resp.status_code}: {resp.text}") except Exception as e: st.error(f"Request failed: {e}") with st.expander("Show request payload"): st.json(payload)