Frontend / app.py
yokesh1999's picture
Upload folder using huggingface_hub
6e65a0b verified
import streamlit as st
import pandas as pd
import requests
# Streamlit UI for SuperKart Sales Revenue Forecasting
st.title("SuperKart Sales Revenue Forecasting App")
st.write("This app predicts the total sales revenue for product-store combinations based on product and store features.")
st.write("Adjust the values below to get a sales prediction.")
# Collect user input using sliders and selectboxes
col1, col2 = st.columns(2)
with col1:
st.subheader("Product Features")
Product_Weight = st.slider("Product Weight", 5.0, 25.0, 12.5, 0.1)
Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
Product_Allocated_Area = st.slider("Product Allocated Area (ratio)", 0.0, 0.5, 0.05, 0.01)
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"
])
Product_MRP = st.slider("Product MRP (Maximum Retail Price)", 50.0, 300.0, 150.0, 1.0)
with col2:
st.subheader("Store Features")
Store_Establishment_Year = st.slider("Store Establishment Year", 1985, 2015, 2000, 1)
Store_Size = st.selectbox("Store Size", ["Small", "Medium", "High"])
Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
Store_Type = st.selectbox("Store Type", [
"Departmental Store", "Supermarket Type1", "Supermarket Type2", "Food Mart"
])
# Create input data dictionary
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
}
if st.button("Predict Sales Revenue", type='primary'):
# Replace with your Hugging Face backend space URL
api_url = "https://<----user-name---->-<---repo name--->.hf.space/v1/sales" # Enter user name and space name
try:
response = requests.post(api_url, json=input_data, timeout=30)
if response.status_code == 200:
result = response.json()
predicted_sales = result["Predicted_Sales_Total"]
st.success(f"💰 Predicted Sales Revenue: **${predicted_sales:,.2f}**")
st.info(f"This prediction is for a {Product_Type} product in a {Store_Size} {Store_Type} located in {Store_Location_City_Type}.")
else:
st.error(f"Error in API request: Status Code {response.status_code}")
st.error(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to API: {str(e)}")
st.info("Please ensure your backend API is deployed and the URL is correct.")
# Batch Prediction
st.subheader("Batch Prediction")
st.write("Upload a CSV file with product and store features to get predictions for multiple combinations.")
file = st.file_uploader("Upload CSV file", type=["csv"])
if file is not None:
# Display preview of uploaded file
df_preview = pd.read_csv(file)
st.write("Preview of uploaded file:")
st.dataframe(df_preview.head())
if st.button("Predict for Batch", type='primary'):
# Replace with your Hugging Face backend space URL
api_url = "https://<----user-name---->-<---repo name--->.hf.space/v1/salesbatch" # Enter user name and space name
try:
files = {"file": (file.name, file, "text/csv")}
response = requests.post(api_url, files=files, timeout=60)
if response.status_code == 200:
result = response.json()
result_df = pd.DataFrame(result)
st.header("Batch Prediction Results")
st.dataframe(result_df)
# Download button
csv = result_df.to_csv(index=False).encode('utf-8')
st.download_button(
label="Download Predictions as CSV",
data=csv,
file_name="predictions.csv",
mime="text/csv"
)
else:
st.error(f"Error in API request: Status Code {response.status_code}")
st.error(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to API: {str(e)}")
st.info("Please ensure your backend API is deployed and the URL is correct.")