import streamlit as st import requests import json import pandas as pd # --- Page Configuration --- st.set_page_config(page_title='SuperKart Sales Revenue Forecaster', layout='wide') st.title('SuperKart Sales Revenue Forecaster') # --- Backend API URL --- # Make sure to update this with your deployed Flask API URL # During local development, it might be 'http://localhost:5000/forecast_revenue' # For Hugging Face Space, it will be the URL of your deployed backend space, e.g., 'https://.hf.space/forecast_revenue' BACKEND_URL = 'https://sagarathf-superkart.hf.space/v1/forecastrevenue' # API URL for POST st.markdown(""" This application predicts the total sales revenue for a product in a given store. Please fill in the details below to get a sales forecast. """) # --- Input Widgets for Features --- st.subheader('Product Details') col1, col2, col3 = st.columns(3) with col1: product_weight = st.number_input('Product Weight (kg)', min_value=0.1, max_value=50.0, value=10.0, step=0.1) product_sugar_content = st.selectbox( 'Product Sugar Content', ['Low Sugar', 'Regular', 'No Sugar', 'Others'] ) with col2: product_allocated_area = st.number_input('Product Allocated Area Ratio', min_value=0.001, max_value=0.5, value=0.05, step=0.001, format="%.3f") product_type = st.selectbox( 'Product Type', ['Dairy', 'Soft Drinks', 'Meat', 'Fruits and Vegetables', 'Household', 'Baking Goods', 'Snack Foods', 'Frozen Foods', 'Breakfast', 'Health and Hygiene', 'Hard Drinks', 'Canned', 'Breads', 'Starchy Foods', 'Others', 'Seafood'] ) with col3: product_mrp = st.number_input('Product MRP (Max. Retail Price)', min_value=10.0, max_value=500.0, value=150.0, step=1.0) st.subheader('Store Details') col4, col5, col6 = st.columns(3) with col4: store_id = st.selectbox( 'Store ID', ['OUT003', 'OUT002', 'OUT001', 'OUT004'] ) store_establishment_year = st.number_input('Store Establishment Year', min_value=1950, max_value=2024, value=2000, step=1) with col5: store_size = st.selectbox( 'Store Size', ['Medium', 'High', 'Small'] ) store_location_city_type = st.selectbox( 'Store Location City Type', ['Tier 1', 'Tier 2', 'Tier 3'] ) with col6: store_type = st.selectbox( 'Store Type', ['Departmental Store', 'Supermarket Type1', 'Food Mart', 'Supermarket Type2'] ) # --- Prediction Button and Logic --- if st.button('Predict Sales Revenue'): # Collect input data into a dictionary input_data = { "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 } # Display collected data (for debugging purposes) st.json(input_data) try: # Send POST request to the backend API response = requests.post(BACKEND_URL, json=input_data) # Check if the request was successful if response.status_code == 200: prediction_result = response.json() predicted_sales = prediction_result.get('predicted_sales') if predicted_sales is not None: st.success(f"Predicted Sales Revenue: ₹{predicted_sales:,.2f}") else: st.error("Prediction result not found in the API response.") else: st.error(f"Error from backend API: {response.status_code} - {response.text}") except requests.exceptions.ConnectionError: st.error("Could not connect to the backend API. Please ensure the API is running and the URL is correct.") except Exception as e: st.error(f"An unexpected error occurred: {e}")