abhishek1504's picture
Upload folder using huggingface_hub
49902a4 verified
import streamlit as st
import pandas as pd
import requests
# ==================================
# Streamlit UI
# ==================================
st.set_page_config(page_title="Product Store Sales Prediction", layout="centered")
st.title("Product Store Sales Prediction App")
st.write("Predict total sales for a product in a store using machine learning.")
# ==================================
# Input Fields — Single Record
# ==================================
st.header("Single Prediction")
# --- Product Features ---
st.subheader("Product Details")
Product_Code = st.selectbox(
"Product Code",
["NC", "FD", "RC"],
index=0
)
Product_Weight = st.number_input("Product Weight (kg)", min_value=0.0, step=0.01, value=14.80)
Product_Sugar_Content = st.selectbox(
"Sugar Content",
["Low Sugar", "No Sugar", "Regular"]
)
Product_Allocated_Area = st.number_input("Allocated Area (sq.m)", min_value=0.0, step=0.001, value=0.016)
Product_Type = st.selectbox(
"Product Type",
["Food", "Health and Hygiene", "Household", "Soft Drinks"]
)
Product_MRP = st.number_input("Maximum Retail Price (MRP)", min_value=0.0, step=0.1, value=140.53)
# --- Store Features ---
st.subheader("Store Details")
Store_Id = st.selectbox(
"Store ID",
["OUT001", "OUT002", "OUT003", "OUT004", "OUT005"]
)
Store_Age = st.number_input("Store Age (years)", min_value=0, step=1, value=20)
Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
Store_Location_City_Type = st.selectbox("City Type", ["Tier 1", "Tier 2", "Tier 3"])
Store_Type = st.selectbox(
"Store Type",
["Supermarket Type1", "Supermarket Type2", "Food Mart", "Departmental Store"]
)
# ==================================
# Predict Button — Single
# ==================================
if st.button("Predict Sales", type="primary"):
input_data = {
"Product_Code": Product_Code,
"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_Age": Store_Age,
"Store_Size": Store_Size,
"Store_Location_City_Type": Store_Location_City_Type,
"Store_Type": Store_Type
}
API_URL = "https://abhishek1504-learning.hf.space/v1/sales"
with st.spinner("Fetching prediction..."):
response = requests.post(API_URL, json=input_data)
if response.status_code == 200:
result = response.json()
if "Predicted_Product_Store_Sales_Total" in result:
predicted_sales = result["Predicted_Product_Store_Sales_Total"]
st.success(f"Predicted Total Sales: **{predicted_sales:.2f} units**")
else:
st.error(f"Unexpected response format: {result}")
else:
st.error(f"API Error {response.status_code}: {response.text}")
# ==================================
# Batch Prediction Section
# ==================================
st.header("Batch Prediction")
file = st.file_uploader("Upload CSV file (must include same column names as model)", type=["csv"])
if file is not None:
if st.button("Predict for Batch", type="primary"):
API_BATCH_URL = "https://abhishek1504-learning.hf.space/v1/salesbatch"
with st.spinner("Processing batch predictions..."):
response = requests.post(API_BATCH_URL, files={"file": file})
if response.status_code == 200:
result = response.json()
try:
df_result = pd.DataFrame(result)
st.dataframe(df_result, use_container_width=True)
st.success("Batch predictions completed successfully.")
except Exception:
st.write(result)
else:
st.error(f"API Error {response.status_code}: {response.text}")