u2jyothibhat's picture
Upload folder using huggingface_hub
e0afc1d verified
import streamlit as st
import requests
import datetime
import joblib # For loading the serialized model
import pandas as pd # For data manipulation
from flask import Flask, request, jsonify # For creating the Flask API
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import PowerTransformer, OrdinalEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
# Set the title of the Streamlit app
st.title("SuperKart sales Prediction")
# Section for online prediction
st.subheader("Online Prediction")
# cyear = datetime.now().year
cyear=2025
#--------------------------------------------TESTING (Model without REST)-----------------------------------------------
#print( " Trying to load XGBoost model using joblib")
#model = joblib.load("XGBoost_best_model.joblib")
#print("Model loaded successfully!")
#--------------------------------------------TESTING (Model without REST)-----------------------------------------------------
# Collect user input for property features
pwt = st.number_input("Product Weight", min_value=0.1, value=100.0)
paa = st.number_input("Product_Allocated_Area (Ratio of product area to Total Area)", min_value=0.001, max_value=0.999, value=0.2)
psc = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
ptyp = st.selectbox("Product Type", ['Fruits and Vegetables', 'Snack Foods', 'Frozen Foods', 'Dairy',
'Household', 'Baking Goods', 'Canned', 'Health and Hygiene',
'Meat', 'Soft Drinks', 'Breads', 'Hard Drinks', 'Others',
'Starchy Foods', 'Breakfast', 'Seafood'])
ssize = st.selectbox("Store Size", ['Small', 'Medium', 'High'])
sloctype = st.selectbox("Store Location City Type", ['Tier 1', 'Tier 2', 'Tier 3'])
styp = st.selectbox("Store Type", ['Supermarket Type2', 'Supermarket Type1', 'Departmental Store', 'Food Mart'])
# pid_c2 = st.selectbox("pid_c2", ['FD', 'DR', 'NC'])
pmrp = st.number_input("Product MRP", min_value=0.1, value=100.0)
seyr_i = st.number_input("Store Establishment Year", min_value=1987, step=1, max_value=cyear, value=2025)
seyr = str(seyr_i)
# Convert user input into a DataFrame
input_data = pd.DataFrame([{
'Product_Weight': pwt,
'Product_Allocated_Area': paa,
'Product_MRP': pmrp,
'Product_Sugar_Content': psc,
'Product_Type': ptyp,
'Store_Establishment_Year': seyr,
'Store_Size': ssize,
# 'pid_c2':pid_c2,
'Store_Location_City_Type': sloctype,
'Store_Type': styp
}])
# Make prediction when the "Predict" button is clicked
if st.button("Predict"):
print("payload = ", input_data.to_dict(orient='records')[0])
response = requests.post("https://u2jyothibhat-SuperKart-Backend.hf.space/v1/sales", json=input_data.to_dict(orient='records')[0])
if response.status_code == 200:
prediction = response.json()['Predicted Sales']
st.success(f"Predicted Sales: {prediction}")
else:
st.error("Error making prediction.")