FrEd / app.py
Lokiiparihar's picture
Update app.py
65e2502 verified
import streamlit as st
import pandas as pd
import requests # Import requests for API calls
# --- Configuration for Backend API --- #
# This URL should point to your deployed backend Hugging Face Space
BACKEND_API_URL = "https://lokiiparihar-superkart-api-t.hf.space" # Replace with your actual backend space URL
PREDICT_ENDPOINT = f"{BACKEND_API_URL}/v1/sales"
# Streamlit UI for Sales Prediction
st.title("SuperKart Sales Prediction App")
st.write("This tool predicts the sales revenue for a specific product in a SuperKart store.")
st.subheader("Enter the product and store details:")
# Collect user input for SuperKart sales prediction
product_id = st.selectbox("Product ID Prefix", ['FD', 'NC', 'DR'])
product_weight = st.number_input("Product Weight", min_value=0.0, value=12.0)
product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
product_allocated_area = st.number_input("Product Allocated Area", min_value=0.0, value=0.05)
product_type = st.selectbox("Product Type", ['Perishables', 'Non Perishables'])
product_mrp = st.number_input("Product MRP", min_value=0.0, value=150.0)
store_id = st.selectbox("Store ID", ['OUT004', 'OUT003', 'OUT001', 'OUT002'])
store_type = st.selectbox("Store Type", ["Grocery Store","Supermarket Type1","Supermarket Type2","Supermarket Type3"])
store_size = st.selectbox("Store Size", ['Medium', 'High', 'Small'])
store_location_city_type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3'])
store_current_age = st.number_input("Store Current Age (Years)", min_value=0, value=15)
# Convert user input into a dictionary for JSON payload
input_data = {
'Product_Id': product_id,
'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_Type': store_type,
'Store_Size': store_size,
'Store_Location_City_Type': store_location_city_type,
'Store_Current_Age': store_current_age
}
# Predict button
if st.button("Predict Sales"):
try:
# Make a POST request to the backend API
response = requests.post(PREDICT_ENDPOINT, json=input_data)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
predicted_sales = response.json().get('Predicted Sales')
if predicted_sales is not None:
st.write(f"The predicted sales revenue is ${predicted_sales:.2f}.")
else:
st.error("Prediction failed: Unexpected response from backend.")
st.json(response.json()) # Display full response for debugging
except requests.exceptions.ConnectionError:
st.error("Could not connect to the backend API. Please ensure the backend Space is running and accessible.")
except requests.exceptions.RequestException as e:
st.error(f"An error occurred during the API request: {e}")
st.text(response.text) # Display raw response text for debugging
except Exception as e:
st.error(f"An unexpected error occurred: {e}")