model-frontend / app.py
aakash200's picture
Update app.py
32442a6 verified
Raw
History Blame Contribute Delete
2.4 kB
import streamlit as st
import requests
import pandas as pd
# App Title
st.title("Superkart Product Store Sales Prediction")
# --- Online (Single) Prediction ---
st.subheader("Online Prediction")
# Input Fields
store_id = st.number_input("Store ID", min_value=1, step=1, value=1)
item_id = st.number_input("Item ID", min_value=1, step=1, value=1)
item_price = st.number_input("Item Price (₹)", min_value=0.0, step=1.0, value=100.0)
promotion = st.selectbox("Promotion Applied?", ["Yes", "No"])
holiday = st.selectbox("Is it a Holiday or Weekend?", ["Yes", "No"])
day_of_week = st.selectbox("Day of the Week", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])
# Format data as expected by backend
input_data = {
"store_id": store_id,
"item_id": item_id,
"item_price": item_price,
"promotion": 1 if promotion == "Yes" else 0,
"holiday": 1 if holiday == "Yes" else 0,
"day_of_week": day_of_week
}
# Predict Button
if st.button("Predict"):
try:
response = requests.post(
"https://namita2025-superkart-backend.hf.space/predict", # Replace with your backend URL if needed
json=input_data
)
if response.status_code == 200:
result = response.json()
st.success(f"Predicted Sales: {result['predicted_sales']} units")
else:
st.error(f"Error from API: Status Code {response.status_code}")
except Exception as e:
st.error(f"Failed to connect to the API. Error: {e}")
# --- Batch Prediction ---
st.subheader("Batch Prediction (CSV Upload)")
file = st.file_uploader("Upload a CSV file with multiple records", type=["csv"])
if file is not None:
if st.button("Predict Batch"):
try:
response = requests.post(
"https://namita2025-superkart-backend.hf.space/predict_batch", # Replace if needed
files={"file": file}
)
if response.status_code == 200:
result = response.json()
df = pd.DataFrame.from_dict(result, orient='index', columns=["Predicted Sales"])
st.success("Batch Prediction Done!")
st.dataframe(df)
else:
st.error(f"API returned status code {response.status_code}")
except Exception as e:
st.error(f"Failed to perform batch prediction. Error: {e}")