MainiSandeep1987's picture
Upload folder using huggingface_hub
3bc9275 verified
import streamlit as st
import pandas as pd
import requests
import os
# Set the title of the Streamlit app
st.title("Product Store Sales Revenue Predictor")
# Section for online prediction
st.subheader("Online Prediction.! ")
# Collect user input
Id=st.number_input("Enter Id for the intended Product", min_value=1, step=1, format="%d")
ProductCategory = st.selectbox("Product_Category", ["Food(FD)","Non Consumables(NC)","Drinks(DR)"])
ProductWeight = st.number_input("Product_Weight", min_value=1.00, value=1.00)
ProductAllocatedArea = st.number_input("Product_Allocated_Area", min_value=0.0001, value=0.0001)
ProductMRP = st.number_input("Product_MRP", min_value=1.00,value=1.00)
StoreEstablishmentYear = st.number_input("Store_Establishment_Year", min_value=1980,value=1990)
ProductSugarContent = st.selectbox("Product_Sugar_Content", ["Low Sugar", "Regular", "No Sugar"])
ProductType = 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"])
StoreId = st.selectbox("Store_Id", ["OUT001", "OUT002", "OUT003","OUT004"])
StoreSize = st.selectbox("Store_Size", ["Small", "Medium", "High"])
StoreLocationCityType = st.selectbox("Store_Location_City_Type", ["Tier 1", "Tier 2", "Tier 3"])
StoreType = st.selectbox("Store_Type", ["Departmental Store", "Food Mart", "Supermarket Type1","Supermarket Type2"])
# Convert user input into a DataFrame
input_data = pd.DataFrame([{
'Product_Weight': ProductWeight,
'Product_Allocated_Area': ProductAllocatedArea,
'Product_MRP': ProductMRP,
'Store_Establishment_Year': StoreEstablishmentYear,
'Product_Category' : ProductCategory.split("(")[-1].strip(")"),
'Product_Sugar_Content': ProductSugarContent,
'Product_Type': ProductType,
'Store_Id': StoreId,
'Store_Size': StoreSize,
'Store_Location_City_Type': StoreLocationCityType,
'Store_Type': StoreType
}])
#header
api_token = os.environ["API_TOKEN"]
# api_token = " " # For running Locally ,Fallback value
headers = {
'Authorization': f"Bearer {api_token}", # Include token in headers
'Content-Type': 'application/json'
}
headers_batch = {
'Authorization': f"Bearer {api_token}", # Include token in headers
}
# Single Prediction
if st.button("Predict"):
product_Id = input_data["Product_Category"].iloc[0] + str(Id)
try:
response = requests.post(
"https://MainiSandeep1987-SuperKartBackEnd.hf.space/v1/salesTotal",headers=headers,
json=input_data.to_dict(orient='records')[0]
) # Send data to Flask API
if response.status_code == 200:
# Convert predicted_price to Python float
prediction = response.json().get('Predicted Sales Revenue Price', "Prediction not found")
st.success(f"Predicted Sales Revenue for Product_Id {product_Id} :: {prediction}")
else:
st.error(f"Error {response.status_code}: {response.text}")
except requests.exceptions.RequestException as e:
st.error(f"Request failed : {str(e)}")
# Section for batch prediction
st.subheader("Batch Prediction")
# Batch Prediction Section
# 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("https://MainiSandeep1987-SuperKartBackEnd.hf.space/v1/salesTotalBatch",headers=headers_batch, files={"file": uploaded_file}) # Send file to Flask API
if response.status_code == 200:
response.raise_for_status() # Raise error if request fails
predictions = response.json()
st.success("Batch predictions completed!")
st.write(predictions) # Display the predictions
else:
st.error("Error making batch prediction.")
except requests.exceptions.RequestException as e:
st.error(f"Request failed: {str(e)}")
except Exception as e:
st.error(f"Unexpected error: {e}")