superkart_ui / src /streamlit_app.py
righthook75's picture
Upload folder using huggingface_hub
bc832c0 verified
Raw
History Blame Contribute Delete
4.88 kB
import streamlit as st
import pandas as pd
import requests
# Set the title of the Streamlit app
st.title("Sales Prediction for SuperKart")
# Section for online prediction
st.subheader("Online Prediction")
# As we declare components and configure them Streamlit will lay them out in the page and assign the values to the variables here
product_type = st.selectbox("Product Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods', 'Health and Hygiene',
'Snack Foods', 'Meat', 'Household', 'Hard Drinks',
'Fruits and Vegetables', 'Breads', 'Soft Drinks', 'Breakfast', 'Others',
'Starchy Foods', 'Seafood'])
product_sugar_content = st.selectbox("Product Sugar Content", ["No Sugar", "Low Sugar","Regular"])
product_weight = st.number_input("Product Weight", min_value=0.0, value=10.0)
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
product_mrp = st.number_input("Product MRP", min_value=0.0, value=100.0)
store_establishment_year = st.number_input("Store Establishment Year", min_value=1900, max_value=2024, value=2010)
store_id = st.selectbox("Store ID", ["OUT002", "OUT003", "OUT004"])
store_size = st.selectbox("Store Size", ["Medium", "Small", "High"])
store_location_city_type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
store_type = st.selectbox("Store Type", ["Food Mart", "Supermarket Type1", "Supermarket Type2", "Department Store"])
# Convert user input into a DataFrame
input_data = pd.DataFrame([{
"Product_Weight": product_weight,
"Product_Allocated_Area": product_allocated_area,
"Product_MRP": product_mrp,
"Store_Establishment_Year": store_establishment_year,
"Product_Sugar_Content_No Sugar": product_sugar_content == "No Sugar",
"Product_Sugar_Content_Regular": product_sugar_content == "Regular",
"Product_Type_Breads": product_type == "Breads",
"Product_Type_Breakfast": product_type == "Breakfast",
"Product_Type_Canned": product_type == "Canned",
"Product_Type_Dairy": product_type == "Dairy",
"Product_Type_Frozen Foods": product_type == "Frozen Foods",
"Product_Type_Fruits and Vegetables": product_type == "Fruits and Vegetables",
"Product_Type_Hard Drinks": product_type == "Hard Drinks",
"Product_Type_Health and Hygiene": product_type == "Health and Hygiene",
"Product_Type_Household": product_type == "Household",
"Product_Type_Meat": product_type == "Meat",
"Product_Type_Others": product_type == "Others",
"Product_Type_Seafood": product_type == "Seafood",
"Product_Type_Snack Foods": product_type == "Snack Foods",
"Product_Type_Soft Drinks": product_type == "Soft Drinks",
"Product_Type_Starchy Foods": product_type == "Starchy Foods",
"Store_Id_OUT002": store_id == "OUT002",
"Store_Id_OUT003": store_id == "OUT003",
"Store_Id_OUT004": store_id == "OUT004",
"Store_Size_Medium": store_size == "Medium",
"Store_Size_Small": store_size == "Small",
"Store_Location_City_Type_Tier 2": store_location_city_type == "Tier 2",
"Store_Location_City_Type_Tier 3": store_location_city_type == "Tier 3",
"Store_Type_Food Mart": store_type == "Food Mart",
"Store_Type_Supermarket Type1": store_type == "Supermarket Type1",
"Store_Type_Supermarket Type2": store_type == "Supermarket Type2"
}])
# This is the API we're going to use to send the user input to for prediction
root = "https://righthook75-superkart-api.hf.space/"
url=root+'/predict'
# Make prediction when the "Predict" button is clicked
if st.button("Predict"):
try:
response = requests.post(url, json=input_data.to_dict(orient='records')[0])
if response.status_code == 200:
prediction = response.json().get("predicted_sales")
st.success(f"Predicted Rental Price (in dollars): {prediction}")
else:
st.error(f"{response.status_code} Error making prediction.")
except Exception as e:
st.error(f"Error making prediction: {e}")
st.subheader("Batch Prediction")
url = root + '/predict/batch'
# 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"):
try:
response = requests.post(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"{response.status_code} Error making batch prediction.")
except Exception as e:
st.error(f"Error making batch prediction: {e}")