MBG0903's picture
Upload folder using huggingface_hub
b016999 verified
import streamlit as st
import pandas as pd
import requests
from transformers import pipeline
import joblib
# Set the title of the Streamlit app
st.title("SuperKart Sales Prediction")
# Section for online prediction
st.subheader("Online Prediction")
# Collect business input for features
Product_Weight = st.number_input("Product Weight", min_value=0.0, value=12.66)
Product_Sugar_Content = st.selectbox("Product Sugar Content", ["Sugar_Low Sugar", "Sugar_Regular", "Sugar_No Sugar", "Sugar_regular"])
Product_Allocated_Area = st.selectbox("Product Allocated Area", ["Area_Small", "Area_Medium", "Area_Large"])
Product_MRP = st.selectbox("Product MRP", ["Size_Low", "Size_Medium", "Size_High"])
Store_Size = st.selectbox("Store Size", ["Size_Small", "Size_Medium", "Size_Large"])
Store_Age = st.number_input("Store Age", min_value=1987, max_value=2009)
Store_Location_City_Type = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
Store_Type = st.selectbox("Store Type", ["Type_Supermarket Type1", "Type_Supermarket Type2", "Type_Departmental Store", "Type_Grocery Store"])
Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1987, max_value=2009)
Product_Type = st.selectbox("Product Type", ["Baking Goods", "Frozen Foods", "Dairy", "Canned", "Health and Hygiene", "Snack Foods", "Meat", "Household", "Hard Drinks",
"Fruits and Vegetables", "Breads", "Soft Drinks", "Breakfast", "Others", "Starchy Foods", "Seafood"])
# Convert user input into a DataFrame
input_data = pd.DataFrame({
"Product_Weight": Product_Weight,
"Product_Sugar_Content": Product_Sugar_Content,
"Product_Allocated_Area": Product_Allocated_Area,
"Product_MRP": Product_MRP,
"Store_Size": Store_Size,
"Store_Age": Store_Age,
"Store_Location_City_Type": Store_Location_City_Type,
"Store_Type": Store_Type,
"Store_Establishment_Year": Store_Establishment_Year,
"Product_Type": Product_Type
}, index=[0])
def predict_sales(input_data):
backend_url = "https://MBG0903-SuperKartSalesPredictionBackend.hf.space/v1/predict" # THIS LINE IS CRUCIAL
headers = {'Content-Type': 'application/json'}
try:
response = requests.post(backend_url, json=input_data, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
prediction = response.json()['Predicted sales']
return prediction
except requests.exceptions.RequestException as e:
st.error(f"Error communicating with backend: {e}")
return None
# Make prediction when the "Predict" button is clicked
if st.button("Predict"):
response = requests.post("https://MBG0903-SuperKartSalesPredictionBackend.hf.space/v1/predict", json=input_data.to_dict(orient="records")[0])
if response.status_code == 200:
prediction = response.json()['Predicted Sales (in dollars)']
st.success(f"Predicted Sales (in dollars): {prediction}")
else:
st.error("Error making prediction.")
# 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"):
response = requests.post("https://MBG0903-SuperKartSalesPredictionBackend.hf.space/v1/predict/batch", files={"file": uploaded_file})
if response.status_code == 200:
predictions = response.json()
st.success("Batch predictions completed!")
st.write(predictions) # Display the predictions
else:
st.error("Error making batch prediction.")