Streamlit / app.py
austinsd's picture
Upload folder using huggingface_hub
7350f23 verified
Raw History Blame Contribute Delete
3.62 kB
import streamlit as st
import requests
import pandas as pd
import numpy as np
# --- Configuration ---
# Replace with your deployed backend API URL
API_URL = "https://austinsd-backendKart.hf.space/predict"
# Median Product_Allocated_Area from training data for handling zero values
MEDIAN_ALLOCATED_AREA = 0.056 # This value was derived from df['Product_Allocated_Area'].median()
st.title('SuperKart Sales Prediction')
st.markdown('Enter product and store details to predict sales total.')
# --- Input Fields ---
st.header('Product Details')
product_weight = st.number_input('Product Weight (e.g., 12.66)', min_value=0.1, max_value=30.0, value=12.66, step=0.01)
product_allocated_area = st.number_input('Product Allocated Area (e.g., 0.027, if 0 will be replaced by median)', min_value=0.0, max_value=0.3, value=0.027, step=0.001)
product_mrp = st.number_input('Product MRP (e.g., 117.08)', min_value=0.0, max_value=300.0, value=117.08, step=0.01)
product_sugar_content = st.selectbox(
'Product Sugar Content',
['Low Sugar', 'Regular', 'No Sugar']
)
product_type = st.selectbox(
'Product Type',
['Fruits and Vegetables', 'Snack Foods', 'Household', 'Frozen Foods', 'Dairy', 'Canned', 'Baking Goods',
'Health and Hygiene', 'Soft Drinks', 'Meat', 'Breads', 'Hard Drinks', 'Breakfast', 'Starchy Foods', 'Seafood', 'Others']
)
st.header('Store Details')
store_establishment_year = st.number_input('Store Establishment Year (e.g., 2009)', min_value=1950, max_value=2023, value=2009, step=1)
store_id = st.selectbox(
'Store ID',
['OUT001', 'OUT003', 'OUT009', 'OUT005', 'OUT004', 'OUT010', 'OUT006', 'OUT007', 'OUT002', 'OUT008']
)
store_size = st.selectbox(
'Store Size',
['High', 'Medium', 'Small']
)
store_location_city_type = st.selectbox(
'Store Location City Type',
['Tier 1', 'Tier 2', 'Tier 3']
)
store_type = st.selectbox(
'Store Type',
['Supermarket Type1', 'Food Mart', 'Supermarket Type2', 'Departmental Store']
)
# --- Prediction Logic ---
if st.button('Predict Sales'):
# Preprocess inputs to match model's expected features
store_age = 2024 - store_establishment_year
product_visibility = product_allocated_area if product_allocated_area != 0 else MEDIAN_ALLOCATED_AREA
input_data = {
'Product_Weight': product_weight,
'Product_Allocated_Area': product_allocated_area, # Keep original for consistency with training data columns
'Product_MRP': product_mrp,
'Store_Age': store_age,
'Product_Visibility': product_visibility,
'Product_Sugar_Content': product_sugar_content,
'Product_Type': product_type,
'Store_Id': store_id,
'Store_Size': store_size,
'Store_Location_City_Type': store_location_city_type,
'Store_Type': store_type
}
# Wrap input_data in a list because the backend expects a list of records
payload = [input_data]
try:
response = requests.post(API_URL, json=payload)
if response.status_code == 200:
prediction = response.json().get('prediction')
if prediction:
st.success(f"Predicted Product Store Sales Total: ${prediction[0]:,.2f}")
else:
st.error("Prediction not found in response.")
else:
st.error(f"Error from API: {response.status_code} - {response.text}")
except requests.exceptions.ConnectionError:
st.error("Could not connect to the API. Please ensure the backend is running and the API_URL is correct.")
except Exception as e:
st.error(f"An unexpected error occurred: {e}")