sahilsingla's picture
Upload folder using huggingface_hub
e123a92 verified
import streamlit as st
import requests
import json
# Streamlit app title
st.title("SuperKart Sales Prediction")
# Form for user inputs
with st.form("prediction_form"):
st.header("Enter Product and Store Details")
# Numerical inputs with ranges based on dataset head and payload
product_weight = st.number_input(
"Product Weight (e.g., 9.57 to 16.54)",
min_value=0.0,
max_value=50.0,
value=13.5,
step=0.01
)
product_allocated_area = st.number_input(
"Product Allocated Area (ratio, e.g., 0.01 to 0.144)",
min_value=0.0,
max_value=1.0,
value=0.075,
step=0.001
)
product_mrp = st.number_input(
"Product MRP (e.g., 117.08 to 186.31)",
min_value=0.0,
max_value=500.0,
value=155.0,
step=0.01
)
store_establishment_year = st.number_input(
"Store Establishment Year (e.g., 1987 to 2009)",
min_value=1900,
max_value=2025,
value=2009,
step=1,
format="%d"
)
# Categorical inputs with options from data dictionary
product_sugar_content = st.selectbox(
"Product Sugar Content",
["Low Sugar", "Regular", "No Sugar"],
index=0 # Default to "Low Sugar"
)
product_type = st.selectbox(
"Product Type",
[
"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"
],
index=1 # Default to "Snack Foods"
)
store_size = st.selectbox(
"Store Size",
["Low", "Medium", "High"],
index=1 # Default to "Medium"
)
store_location_city_type = st.selectbox(
"Store Location City Type",
["Tier 1", "Tier 2", "Tier 3"],
index=1 # Default to "Tier 2"
)
store_type = st.selectbox(
"Store Type",
["Departmental Store", "Supermarket Type 1", "Supermarket Type 2", "Food Mart"],
index=2 # Default to "Supermarket Type2"
)
# Submit button
submitted = st.form_submit_button("Predict Sales")
# API call when form is submitted
if submitted:
# Prepare payload for API
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_Establishment_Year": int(store_establishment_year), # Ensure integer
"Store_Size": store_size,
"Store_Location_City_Type": store_location_city_type,
"Store_Type": store_type
}
# Send POST request to backend API
model_url = "https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/sales"
try:
response = requests.post(model_url, json=payload)
response.raise_for_status() # Raise exception for bad status codes
result = response.json()
prediction = result.get("prediction", None)
if prediction is not None:
st.success(f"Predicted Sales: ${float(prediction):.2f}")
else:
st.error("No prediction returned from API")
except requests.exceptions.RequestException as e:
st.error(f"Error contacting the API: {e}")
except ValueError:
st.error("Invalid response format from API")
# Section for batch prediction
st.subheader("Batch Prediction")
st.write("Upload a CSV file with columns: Product_Weight, Product_Sugar_Content, Product_Allocated_Area, Product_Type, Product_MRP, Store_Establishment_Year, Store_Size, Store_Location_City_Type, Store_Type")
# Allow users to upload a CSV file
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
if uploaded_file is not None:
if st.button("Predict Batch"):
try:
# Prepare file payload correctly
files = {"file": (uploaded_file.name, uploaded_file, "text/csv")}
# Send the POST request to the Flask API
response = requests.post(
"https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/salesbatch",
files=files
)
# Check response status
if response.status_code == 200:
predictions = response.json()
st.success("Batch predictions completed!")
st.write(predictions)
else:
st.error(f"Error: {response.status_code}")
st.error(response.text)
except Exception as e:
st.error("An unexpected error occurred during prediction.")
st.exception(e)