Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| import json | |
| import pandas as pd | |
| # --- Configuration --- | |
| # IMPORTANT: Replace this with the URL of your deployed Flask API | |
| # It should look like: https://your-username-your-space-name.hf.space/predict | |
| API_URL = "https://kritish205/supercart-backend/predict" | |
| # --- UI Layout --- | |
| st.set_page_config(page_title="SuperKart Sales Predictor", layout="wide") | |
| st.title("π SuperKart Sales Predictor") | |
| st.markdown(""" | |
| This app predicts the total sales for a product in a given store. | |
| Please provide the details of the product and the store below. | |
| """) | |
| # Create columns for a cleaner layout | |
| col1, col2 = st.columns(2) | |
| # --- Input Fields --- | |
| with col1: | |
| st.header("π¦ Product Details") | |
| product_weight = st.number_input("Product Weight (kg)", min_value=0.0, max_value=30.0, value=10.0, step=0.1) | |
| product_mrp = st.number_input("Product MRP ($)", min_value=0.0, max_value=300.0, value=150.0) | |
| product_sugar_content = st.selectbox("Product Sugar Content", ["No Sugar", "Low Sugar", "Regular"]) | |
| product_allocated_area = st.slider("Product Allocated Area (Ratio)", 0.0, 0.3, 0.05) | |
| # This list should match the categories from the original dataset | |
| product_type_options = [ | |
| 'Snack Foods', 'Household', 'Frozen Foods', 'Fruits and Vegetables', | |
| 'Health and Hygiene', 'Dairy', 'Baking Goods', 'Canned', 'Meat', | |
| 'Soft Drinks', 'Breads', 'Hard Drinks', 'Starchy Foods', 'Breakfast', | |
| 'Seafood', 'Others' | |
| ] | |
| product_type = st.selectbox("Product Type", product_type_options) | |
| with col2: | |
| st.header("πͺ Store Details") | |
| store_age = st.number_input("Store Age (Years)", min_value=0, max_value=50, value=15) | |
| store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) | |
| store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"]) | |
| store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"]) | |
| # --- Prediction Logic --- | |
| if st.button("Predict Sales", type="primary"): | |
| if "YOUR_BACKEND_API_URL_HERE" in API_URL: | |
| st.error("Please update the `API_URL` in the `app.py` script with your backend's URL.") | |
| else: | |
| # Create a dictionary payload for the API | |
| # The keys must EXACTLY match the column names the model was trained on | |
| 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_city_type, | |
| "Store_Type": store_type, | |
| "Store_Age": store_age | |
| } | |
| try: | |
| # Send the data to the Flask API | |
| with st.spinner('Getting prediction...'): | |
| response = requests.post(API_URL, json=payload) | |
| response.raise_for_status() # Raise an exception for bad status codes | |
| result = response.json() | |
| predicted_sales = result['predicted_sales'] | |
| st.success(f"**Predicted Sales:** ${predicted_sales:,.2f}") | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"Error connecting to the API: {e}") | |
| except KeyError: | |
| st.error("Received an unexpected response from the API. Check the backend logs.") |