SalesPredictionFrontEnd / app_frontend.py
agawale's picture
Upload folder using huggingface_hub
36d0793 verified
Raw History Blame Contribute Delete
4.67 kB
import streamlit as st
import pandas as pd
import requests
# Page config
st.set_page_config(page_title="SuperKart Sales Prediction", layout="centered")
BACKEND_URL = "https://agawale-salespredictionbackend.hf.space"
st.write("")
#st.write("Backend URL:", BACKEND_URL)
# Replace this with your actual backend Hugging Face Space URL
# Title
st.title("SuperKart Sales Prediction")
st.write("Predict product-store sales using the deployed SuperKart backend API.")
# Online prediction section
st.subheader("Online Prediction")
col1, col2 = st.columns(2)
with col1:
Product_Weight = st.number_input("Product Weight", min_value=0.0, value=12.66)
Product_MRP = st.number_input("Product MRP", min_value=0.0, value=147.0)
Store_Id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"])
Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
Product_Id_char = st.selectbox("Product ID Category", ["FD", "DR", "NC"])
with col2:
Store_Location_City_Type = st.selectbox(
"Store Location City Type",
["Tier 1", "Tier 2", "Tier 3"]
)
Store_Type = st.selectbox(
"Store Type",
["Supermarket Type2", "Departmental Store", "Supermarket Type1", "Food Mart"]
)
Product_Type = st.selectbox(
"Product Type",
[
"Snack Foods",
"Dairy",
"Soft Drinks",
"Fruits and Vegetables",
"Household",
"Baking Goods",
"Frozen Foods",
"Breakfast",
"Health and Hygiene",
"Hard Drinks",
"Canned",
"Breads",
"Meat",
"Others",
"Seafood",
"Starchy Foods"
]
)
Store_Age = st.number_input("Store Age", min_value=0, value=15)
# Build JSON payload
input_data = {
"Product_Weight": Product_Weight,
"Product_MRP": Product_MRP,
"Store_Id": Store_Id,
"Store_Size": Store_Size,
"Store_Location_City_Type": Store_Location_City_Type,
"Store_Type": Store_Type,
"Product_Id_char": Product_Id_char,
"Product_Type": Product_Type,
"Store_Age": Store_Age
}
# Predict single record
if st.button("Predict Sales"):
try:
response = requests.post(
f"{BACKEND_URL}/v1/sales",
json=input_data,
timeout=60
)
if response.status_code == 200:
prediction = response.json()["Predicted Store Sales"]
st.success(f"Predicted Store Sales: {prediction}")
else:
st.error(f"Error making prediction. Status code: {response.status_code}")
st.write(response.text)
except Exception as e:
st.error(f"Request failed: {e}")
# Batch prediction section
st.subheader("Batch Prediction")
st.write(
"Upload a CSV file with these columns: "
"Product_Weight, Product_MRP, Store_Id, Store_Size, "
"Store_Location_City_Type, Store_Type, Product_Id_char, Product_Type, Store_Age"
)
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
if uploaded_file is not None:
try:
preview_df = pd.read_csv(uploaded_file)
st.write("Preview of uploaded file:")
st.dataframe(preview_df.head())
# Reset pointer after reading preview
uploaded_file.seek(0)
if st.button("Predict Batch"):
files = {"file": (uploaded_file.name, uploaded_file, "text/csv")}
response = requests.post(
f"{BACKEND_URL}/v1/salesbatch",
files=files,
timeout=120
)
if response.status_code == 200:
predictions = response.json()
st.success("Batch predictions completed!")
st.write(predictions)
# Optional: create downloadable results
result_df = preview_df.copy()
if isinstance(predictions, dict):
result_df["Predicted_Store_Sales"] = list(predictions.values())
else:
result_df["Predicted_Store_Sales"] = predictions
csv = result_df.to_csv(index=False).encode("utf-8")
st.download_button(
label="Download Predictions CSV",
data=csv,
file_name="superkart_sales_predictions.csv",
mime="text/csv"
)
else:
st.error(f"Error making batch prediction. Status code: {response.status_code}")
st.write(response.text)
except Exception as e:
st.error(f"Batch request failed: {e}")