import streamlit as st import pandas as pd import requests # Set page configuration st.set_page_config(page_title="SuperKart Sales Forecasting App", layout="centered") # App title and description st.title("SuperKart Sales Forecasting App") st.write( "This tool predicts the sales revenue of products across SuperKart stores " "based on product and store attributes." ) # Section for online prediction st.subheader("Online Prediction (Single Product-Store Entry)") # Collect user input for product & store features product_weight = st.number_input("Product Weight (kg)", min_value=4.0, max_value=22.0, value=12.65, step=0.1) product_sugar_content = st.selectbox("Sugar Content", ["Low Sugar", "Regular", "No Sugar", "Non-edible"]) product_allocated_area = st.number_input("Allocated Area (ratio)", min_value=0.004, max_value=0.298, value=0.068, step=0.001) product_type = st.selectbox("Product Type", [ "Fruits and Vegetables", "Snack Foods", "Dairy", "Canned", "Soft Drinks", "Health and Hygiene", "Baking Goods", "Bread", "Breakfast", "Frozen Foods", "Household", "Seafood", "Starchy Foods", "Meat", "Hard Drinks", "Others" ]) product_mrp = st.number_input("Product MRP", min_value=31.0, max_value=266.0, value=147.0, step=1.0) store_id = st.selectbox("Store ID", ["OUT001","OUT002","OUT003","OUT004"]) store_establishment_year = st.number_input("Store Establishment Year", min_value=1987, max_value=2025, value=2002, step=1) store_size = st.selectbox("Store Size", ["High", "Medium", "Low"]) store_location_city_type = st.selectbox("Store City Type", ["Tier 1", "Tier 2", "Tier 3"]) store_type = st.selectbox("Store Type", ["Supermarket Type1", "Supermarket Type2", "Supermarket Type3", "Food Mart"]) # Input DataFrame with EXACT column names as training input_data = pd.DataFrame([{ "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_Id": "store_id", "Store_Establishment_Year": store_establishment_year, "Store_Size": store_size, "Store_Location_City_Type": store_location_city_type, "Store_Type": store_type }]) # Make prediction when "Forecast Sales" is clicked if st.button("Forecast Sales"): try: response = requests.post( "https://nsa9-ProductStoreSalesBackend.hf.space/v1/forecast", # Flask backend endpoint json=input_data.to_dict(orient="records")[0] ) if response.status_code == 200: # First, get the JSON data response_json = response.json() # Then, check if the key exists before trying to access it if "Predicted Sales (in dollars)" in response_json: prediction = response_json.get("Predicted Sales (in dollars)") st.success(f"Predicted Sales Revenue: ${prediction:,.2f}") else: st.error("Invalid response format from backend. The expected key 'Predicted Sales (in dollars)' was not found.") st.write("Full response from backend:", response_json) else: st.error(f"Error from backend: {response.status_code}") st.write("Full response text:", response.text) except requests.exceptions.RequestException as e: st.error(f"Request failed: {e}") # Section for batch prediction st.subheader("Batch Prediction (Upload CSV)") uploaded_file = st.file_uploader("Upload a CSV file with multiple product-store entries", type=["csv"]) if uploaded_file is not None and st.button("Predict Batch"): try: response = requests.post( "https://nsa9-ProductStoreSalesBackend.hf.space/v1/forecastbatch", # Flask backend batch endpoint files={"file": uploaded_file} ) if response.status_code == 200: predictions = response.json() st.success("Batch predictions completed!") st.write(pd.DataFrame(predictions, index=["Predicted Sales"])) # Display nicely as a table else: st.error(f"Error from backend: {response.status_code}") except Exception as e: st.error(f"Batch request failed: {e}")