DeepthiJ28's picture
Update app.py
74bea32 verified
import requests
import streamlit as st
import pandas as pd
import re
st.title("SuperKart Sales Prediction")
# Batch Prediction
st.subheader("Online Prediction")
# Input fields for customer data
Product_Id = st.text_input("Enter Product ID (e.g., AB123)")
# Validation using regex
pattern = r"^[A-Za-z]{2}\d+$"
if Product_Id:
if not re.match(pattern, Product_Id):
st.success("Valid Product ID!")
Product_Weight = st.number_input("Weight of each product", min_value=0, max_value=100, value=10)
Product_Sugar_Content = st.selectbox("Sugar Content of each product", ["Low Sugar", "No Sugar", "Regular"])
Product_Allocated_Area = st.number_input("Allocated area ratio of each product", min_value=0.000, max_value=1.000, value=0.000,format="%.3f")
Product_Type = st.selectbox("Product Type", ["Frozen Foods", "Dairy", "Canned","Baking Goods","Health and Hygiene"])
Product_MRP = st.number_input("MRP of the product", min_value=0, max_value=500, value=0)
Store_Id = st.text_input("Enter Store ID (e.g., OUT004)")
# Validation using regex
pattern = r"^[A-Za-z]{3}\d+$"
if Store_Id:
if not re.match(pattern, Store_Id):
st.error("Invalid Store ID!")
Store_Establishment_Year = st.number_input("Store Establishment Year",min_value=1900,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", ["Supermarket Type1","Supermarket Type2","Departmental Store","Grocery Store","Food Mart"])
product_data = {
'Product_Weight':Product_Weight,
'Product_Sugar_Content':Product_Sugar_Content,
'Product_Allocated_Area':Product_Allocated_Area,
'Product_Type':Product_Type,
'Product_MRP':Product_MRP,
'Store_Id':Store_Id,
'Store_Establishment_Year':Store_Establishment_Year,
'Store_Size':Store_Size,
'Store_Location_City_Type':Store_Location_City_Type,
'Store_Type':Store_Type
}
if st.button("Predict", type='primary'):
response = requests.post("https://DeepthiJ28-SuperKartSalesBackend2.hf.space/v1/Product", json=product_data) # enter user name and space name before running the cell
if response.status_code == 200:
result = response.json()
sales = result["Prediction"] # Extract only the value
st.write(f"Based on the information provided, the product with ID {Product_Id} is likely to have sales of {sales}.")
else:
st.error("Error in API request")
# Batch Prediction
st.subheader("Batch Prediction")
file = st.file_uploader("Upload CSV file", type=["csv"])
if file is not None:
if st.button("Predict for Batch", type='primary'):
response = requests.post("https://DeepthiJ28-SuperKartSalesBackend2.hf.space/v1/Productbatch", files={"file": file}) # enter user name and space name before running the cell
if response.status_code == 200:
result = response.json()
st.header("Batch Prediction Results")
st.write(result)
else:
st.error("Error in API request")
print(response.status_code)
print(response.text)