Lokiiparihar's picture
Update app.py
2934467 verified
import streamlit as st
import requests
import pandas as pd
import json
# NOTE: Replace "YOUR_BACKEND_API_URL_HERE" with your actual Hugging Face Backend Space URL.
# The URL should look something like: https://<your-hf-username>-<your-space-name>.hf.space/predict
BACKEND_API_URL = "https://lokiiparihar-kartproject.hf.space/predict"
st.set_page_config(page_title="SuperKart Sales Predictor", layout="centered")
st.title("🛒 SuperKart Sales Predictor")
st.markdown("Enter product and store details to predict the total sales.")
# Get unique values for select boxes from the dataset (assuming 'dataset' is available from notebook scope)
# In a real deployed app, these would typically be hardcoded or fetched from a config/metadata file
product_sugar_content_options = ['Low Sugar', 'Regular', 'No Sugar'] # From dataset.Product_Sugar_Content.unique()
product_type_options = ['Meat', 'Snack Foods', 'Hard Drinks', 'Dairy', 'Canned', 'Soft Drinks', 'Health and Hygiene', 'Baking Goods', 'Bread', 'Breakfast', 'Frozen Foods', 'Fruits and Vegetables', 'Household', 'Seafood', 'Starchy Foods', 'Others'] # From dataset.Product_Type.unique()
store_size_options = ['Medium', 'High', 'Small'] # From dataset.Store_Size.unique()
store_location_city_type_options = ['Tier 2', 'Tier 3', 'Tier 1'] # From dataset.Store_Location_City_Type.unique()
store_type_options = ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'] # From dataset.Store_Type.unique()
# Streamlit input widgets
with st.form("sales_prediction_form"):
st.header("Product Details")
col1, col2 = st.columns(2)
with col1:
product_weight = st.number_input(
"Product Weight (kg)",
min_value=4.0,
max_value=22.0,
value=12.65,
step=0.01
)
product_allocated_area = st.number_input(
"Product Allocated Area Ratio",
min_value=0.004,
max_value=0.298,
value=0.068,
format="%.3f",
step=0.001
)
product_mrp = st.number_input(
"Product MRP (₹)",
min_value=31.0,
max_value=266.0,
value=147.03,
step=0.1
)
with col2:
product_sugar_content = st.selectbox("Product Sugar Content", product_sugar_content_options)
product_type = st.selectbox("Product Type", product_type_options)
st.header("Store Details")
col3, col4 = st.columns(2)
with col3:
store_establishment_year = st.slider(
"Store Establishment Year",
min_value=1987,
max_value=2009,
value=2009
)
store_size = st.selectbox("Store Size", store_size_options)
with col4:
store_location_city_type = st.selectbox("Store Location City Type", store_location_city_type_options)
store_type = st.selectbox("Store Type", store_type_options)
submitted = st.form_submit_button("Predict Sales Total")
if submitted:
# Prepare data for prediction, ensuring column order matches training if necessary for some models
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_Establishment_Year": [store_establishment_year],
"Store_Size": [store_size],
"Store_Location_City_Type": [store_location_city_type],
"Store_Type": [store_type]
}
st.write("Sending request to backend...")
try:
response = requests.post(BACKEND_API_URL, json=input_data)
if response.status_code == 200:
predictions = response.json().get("predictions")
if predictions:
st.success(f"#### Predicted Sales Total: ₹{predictions[0]:,.2f}")
else:
st.error("Prediction failed: No predictions returned from API.")
else:
st.error(f"Error from backend: Status Code {response.status_code} - {response.text}")
except requests.exceptions.ConnectionError:
st.error("Could not connect to the backend API. Please ensure the backend is running and the URL is correct.")
except Exception as e:
st.error(f"An unexpected error occurred: {e}")
# Dummy comment to force a new file hash. (Second attempt)