Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| st.set_page_config(page_title="SuperKart Sales Forecaster", page_icon="๐", layout="wide") | |
| # Backend API URL pointing to deployed Flask backend | |
| BACKEND_URL = "https://codedfortamara-superkart-backend.hf.space/predict" | |
| st.title("๐ SuperKart Sales Revenue Forecaster") | |
| st.markdown("Predict sales revenue for any product-store combination using our trained ML model.") | |
| st.divider() | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.subheader("๐ฆ Product Details") | |
| product_weight = st.number_input("Product Weight (kg)", min_value=1.0, max_value=25.0, value=12.0, step=0.1) | |
| product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar"]) | |
| product_allocated_area = st.slider("Allocated Display Area (ratio)", min_value=0.0, max_value=0.30, value=0.05, step=0.005, format="%.3f") | |
| product_type = st.selectbox("Product Type", ["Fruits and Vegetables", "Snack Foods", "Frozen Foods", "Dairy", | |
| "Household", "Baking Goods", "Canned", "Health and Hygiene", "Meat", "Soft Drinks", | |
| "Breads", "Hard Drinks", "Others", "Starchy Foods", "Breakfast", "Seafood"]) | |
| product_mrp = st.number_input("Product MRP", min_value=30.0, max_value=270.0, value=140.0, step=1.0) | |
| product_category = st.selectbox("Product Category", ["FD", "NC", "DR"], | |
| format_func=lambda x: {"FD": "FD โ Food", "NC": "NC โ Non-Consumable", "DR": "DR โ Drinks"}[x]) | |
| with col2: | |
| st.subheader("๐ฌ Store Details") | |
| store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) | |
| store_location = st.selectbox("City Tier", ["Tier 1", "Tier 2", "Tier 3"]) | |
| store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"]) | |
| store_age = st.number_input("Store Age (years)", min_value=1, max_value=50, value=20, step=1) | |
| st.divider() | |
| if st.button("๐ฎ Predict Sales Revenue", type="primary", use_container_width=True): | |
| 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_Size": store_size, | |
| "Store_Location_City_Type": store_location, | |
| "Store_Type": store_type, | |
| "Store_Age": store_age, | |
| "Product_Category": product_category, | |
| } | |
| try: | |
| response = requests.post(BACKEND_URL, json=payload, timeout=30) | |
| if response.status_code == 200: | |
| prediction = response.json()["predictions"][0] | |
| st.success(f"### ๐ฐ Predicted Sales Revenue: โน{prediction:,.2f}") | |
| st.markdown("#### Input Summary") | |
| st.dataframe(pd.DataFrame([payload]), use_container_width=True) | |
| else: | |
| st.error(f"API Error: {response.json().get('error', 'Unknown error')}") | |
| except requests.exceptions.ConnectionError: | |
| st.error("Could not connect to the backend. Check if the backend space is running.") | |
| except Exception as e: | |
| st.error(f"Error: {str(e)}") | |
| st.divider() | |
| st.subheader("๐ Batch Prediction") | |
| uploaded_file = st.file_uploader("Upload CSV for batch predictions", type=["csv"]) | |
| if uploaded_file is not None: | |
| batch_df = pd.read_csv(uploaded_file) | |
| st.dataframe(batch_df.head(), use_container_width=True) | |
| if st.button("Run Batch Prediction", use_container_width=True): | |
| try: | |
| response = requests.post(BACKEND_URL, json=batch_df.to_dict(orient='records'), timeout=60) | |
| if response.status_code == 200: | |
| batch_df["Predicted_Sales"] = response.json()["predictions"] | |
| st.success(f"Predictions generated for {len(batch_df)} records!") | |
| st.dataframe(batch_df, use_container_width=True) | |
| st.download_button("Download Predictions", batch_df.to_csv(index=False), | |
| "superkart_predictions.csv", "text/csv") | |
| else: | |
| st.error(f"API Error: {response.json().get('error', 'Unknown error')}") | |
| except Exception as e: | |
| st.error(f"Error: {str(e)}") | |