Spaces:
Runtime error
Runtime error
| # app.py | |
| import json | |
| import requests | |
| import streamlit as st | |
| API_URL = "https://lokiiparihar-superkart-api.hf.space/predict" | |
| st.set_page_config(page_title="Superkart Sales Predictor", layout="centered") | |
| st.title("Superkart Prediction App") | |
| st.caption("Calls the HF Space API and shows the prediction response.") | |
| with st.sidebar: | |
| st.subheader("Model") | |
| model = st.selectbox("Choose model", ["dt", "rf", "xgb", "lr"], index=0) | |
| st.subheader("Enter product & store 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_sugar = st.number_input("Product_Sugar_Content", min_value=0.0, value=0.0, step=0.1) | |
| product_area = st.number_input("Product_Allocated_Area", min_value=0.0, value=0.08, step=0.01) | |
| product_type = st.number_input("Product_Type (encoded)", min_value=0, value=0, step=1) | |
| with col2: | |
| product_mrp = st.number_input("Product_MRP", min_value=0.0, value=249.99, step=1.0) | |
| store_size = st.number_input("Store_Size (encoded)", min_value=0, value=1, step=1) | |
| store_city = st.number_input("Store_Location_City_Type (encoded)", min_value=0, value=0, step=1) | |
| store_type = st.number_input("Store_Type (encoded)", min_value=0, value=1, step=1) | |
| store_age = st.number_input("Store_Age", min_value=0, value=15, step=1) | |
| payload = { | |
| "Product_Weight": float(product_weight), | |
| "Product_Sugar_Content": float(product_sugar), | |
| "Product_Allocated_Area": float(product_area), | |
| "Product_Type": int(product_type), | |
| "Product_MRP": float(product_mrp), | |
| "Store_Size": int(store_size), | |
| "Store_Location_City_Type": int(store_city), | |
| "Store_Type": int(store_type), | |
| "Store_Age": int(store_age), | |
| "model": model, | |
| } | |
| st.write("### Payload") | |
| st.json(payload) | |
| if st.button("Predict", type="primary"): | |
| headers = {"Content-Type": "application/json"} | |
| try: | |
| with st.spinner("Calling API..."): | |
| resp = requests.post(API_URL, json=payload, headers=headers, timeout=30) | |
| st.write("### Response") | |
| st.write("Status Code:", resp.status_code) | |
| content_type = resp.headers.get("content-type", "") | |
| if content_type.startswith("application/json"): | |
| data = resp.json() | |
| st.json(data) | |
| # If your API returns something like {"prediction": ...}, show it nicely: | |
| if isinstance(data, dict): | |
| for key in ["prediction", "pred", "output", "result"]: | |
| if key in data: | |
| st.success(f"{key}: {data[key]}") | |
| break | |
| else: | |
| st.code(resp.text) | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Request failed: {e}") | |
| st.divider() | |
| st.caption(f"API URL: {API_URL}") |