Abhilashu's picture
Upload folder using huggingface_hub
01c36bc verified
import streamlit as st
import pandas as pd
import numpy as np
import requests
# Streamlit UI for Sales Prediction
st.title("Superkart Total Product Sales Prediction App")
st.write("This tool predicts the sales of SuperKart store's product based on the property details.")
st.subheader("Enter the details:")
# Collect user input for each feature
product_id = st.selectbox("Product ID", ["FD", "NC", "DR"])
product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0, step=0.1)
product_sugar_content = st.selectbox("Product Sugar Content", ["Low Sugar", "Regular", "No Sugar"])
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05, step=0.01)
product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0, step=0.1)
store_id = st.selectbox("Store ID", ["OUT001", "OUT002", "OUT003", "OUT004"])
store_establishment_year = st.number_input("Store Establishment Year", min_value=1985, max_value=2025, value=2000, step=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"])
grouped_product_type = st.selectbox("Grouped Product Type", ["Food Items", "Household and Hygiene", "Beverages", "Miscellaneous"])
# Convert user input into a DataFrame
input_data = pd.DataFrame([{
'Product_Weight': product_weight,
'Product_Allocated_Area': product_allocated_area,
'Product_MRP': product_mrp,
'Product_Id_FD': 1 if product_id == 'FD' else 0,
'Product_Id_NC': 1 if product_id == 'NC' else 0,
'Product_Sugar_Content_No Sugar': 1 if product_sugar_content == 'No Sugar' else 0,
'Product_Sugar_Content_Regular': 1 if product_sugar_content == 'Regular' else 0,
'Store_Id_OUT002': 1 if store_id == 'OUT002' else 0,
'Store_Id_OUT003': 1 if store_id == 'OUT003' else 0,
'Store_Id_OUT004': 1 if store_id == 'OUT004' else 0,
'Store_Size_Medium': 1 if store_size == 'Medium' else 0,
'Store_Size_Small': 1 if store_size == 'Small' else 0,
'Store_Location_City_Type_Tier 2': 1 if store_location_city_type == 'Tier 2' else 0,
'Store_Location_City_Type_Tier 3': 1 if store_location_city_type == 'Tier 3' else 0,
'Store_Type_Food Mart': 1 if store_type == 'Food Mart' else 0,
'Store_Type_Supermarket Type1': 1 if store_type == 'Supermarket Type1' else 0,
'Store_Type_Supermarket Type2': 1 if store_type == 'Supermarket Type2' else 0,
'Grouped_Product_Type_Food Items': 1 if grouped_product_type == 'Food Items' else 0,
'Grouped_Product_Type_Household and Hygiene': 1 if grouped_product_type == 'Household and Hygiene' else 0,
'Grouped_Product_Type_Miscellaneous': 1 if grouped_product_type == 'Miscellaneous' else 0,
'Store_Age': 2025 - store_establishment_year # Calculate Store_Age
}])
# Ensure all columns used during training are present in the input_data DataFrame
# Add dummy columns for any missing one-hot encoded features
train_cols = ['Product_Weight', 'Product_Allocated_Area', 'Product_MRP', 'Store_Age',
'Product_Id_FD', 'Product_Id_NC', 'Product_Sugar_Content_No Sugar',
'Product_Sugar_Content_Regular', 'Store_Id_OUT002', 'Store_Id_OUT003',
'Store_Id_OUT004', 'Store_Size_Medium', 'Store_Size_Small',
'Store_Location_City_Type_Tier 2', 'Store_Location_City_Type_Tier 3',
'Store_Type_Food Mart', 'Store_Type_Supermarket Type1',
'Store_Type_Supermarket Type2', 'Grouped_Product_Type_Food Items',
'Grouped_Product_Type_Household and Hygiene',
'Grouped_Product_Type_Miscellaneous']
for col in train_cols:
if col not in input_data.columns:
input_data[col] = 0
# Reorder columns to match the training data
input_data = input_data[train_cols]
# Predict button
if st.button("Predict"):
# Send data to Flask API
response = requests.post("https://abhilashu-superkart-total-sales-prediction-backend.hf.space/v1/sales", json=input_data.to_dict(orient='records')[0])
if response.status_code == 200:
# Get the prediction value directly from the JSON response
prediction_value = response.json()['Predicted total sales (in dollars)']
st.success(f"The predicted total sales for this product in this store is: {prediction_value:.2f}")
else:
st.error("Error making prediction.")