SuperKart / app.py
Garg06's picture
Upload folder using huggingface_hub
2d043d7 verified
import streamlit as st
import pandas as pd
import requests
# Set the title of the Streamlit app
st.title("SuperKart Product Store Sales Prediction")
# Section for online prediction
st.subheader("Predict Single Product Sales")
# Collect user input for product and store features
product_id = st.text_input("Product ID")
product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0)
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar", "low sugar"])
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene', 'Snack Foods', 'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others', 'Starchy Foods', 'Breakfast', 'Seafood', 'Fruits and Vegetables', 'Household'])
product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0)
store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, value=2000)
store_size = st.selectbox("Store Size", ["Medium", "High", "Small"])
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 2", "Tier 1", "Tier 3"])
store_type = st.selectbox("Store Type", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1', 'Food Mart'])
# Calculate engineered features (ensure these match the backend)
current_year = pd.to_datetime('now').year
store_age = current_year - store_establishment_year
perishables = ['Fruits and Vegetables', 'Dairy', 'Meat', 'Seafood', 'Breads', 'Breakfast']
product_category_type = 'Perishables' if product_type in perishables else 'Non Perishables'
# Create a dictionary with the input data
input_data = {
'Product_Id': product_id,
'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_Establishment_Year': store_establishment_year,
'Store_Size': store_size,
'Store_Location_City_Type': store_location_city_type,
'Store_Type': store_type,
'Product_Category_from_ID': product_id[:2] if product_id else '', # Assuming product_id is not empty
'Store_Age': store_age,
'Product_Category_Type': product_category_type
}
# Make prediction when the "Predict" button is clicked
if st.button("Predict"):
# Replace with the actual URL of your deployed backend Hugging Face Space
backend_url = "https://Garg06-superkart.hf.space" # Example URL format
predict_url = f"{backend_url}/v1/product"
try:
response = requests.post(predict_url, json=input_data)
if response.status_code == 200:
prediction = response.json()['Predicted_Product_Store_Sales_Total']
st.success(f"Predicted Product Store Sales Total: {prediction:.2f}")
else:
st.error(f"Error making prediction. Status code: {response.status_code}")
st.error(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to the backend: {e}")
# Section for batch prediction
st.subheader("Batch Prediction")
# Allow users to upload a CSV file for batch prediction
uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
# Make batch prediction when the "Predict Batch" button is clicked
if uploaded_file is not None:
if st.button("Predict Batch"):
# Replace with the actual URL of your deployed backend Hugging Face Space
backend_url = "https://Garg06-superkart.hf.space" # Example URL format
predict_batch_url = f"{backend_url}/v1/productbatch"
try:
response = requests.post(predict_batch_url, files={"file": uploaded_file}) # Send file to Flask API
if response.status_code == 200:
predictions = response.json()
st.success("Batch predictions completed!")
st.write(predictions) # Display the predictions
else:
st.error(f"Error making batch prediction. Status code: {response.status_code}")
st.error(f"Response: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Error connecting to the backend: {e}")