Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| import requests | |
| import json | |
| st.set_page_config(page_title="SuperKart Sales Prediction", page_icon="π") | |
| st.title("SuperKart Sales Prediction") | |
| st.markdown( | |
| "Provide the product and store details to predict the expected " | |
| "**Product Total Sales** for that combination." | |
| ) | |
| # Backend URL from environment / secrets | |
| BACKEND_URL = os.environ.get("BACKEND_URL") | |
| if BACKEND_URL is None: | |
| # Try Streamlit secrets as a second option | |
| try: | |
| BACKEND_URL = st.secrets["BACKEND_URL"] | |
| except Exception: | |
| st.warning( | |
| "No BACKEND_URL found in secrets or environment. " | |
| "If you are running locally, provide it below." | |
| ) | |
| BACKEND_URL = st.text_input( | |
| "Backend URL", | |
| value="http://127.0.0.1:7860/v1/predict" | |
| ) | |
| st.subheader("Product information") | |
| Product_Weight = st.number_input( | |
| "Product Weight", | |
| min_value=0.0, | |
| value=12.5, | |
| step=0.1 | |
| ) | |
| Product_Sugar_Content = st.selectbox( | |
| "Product Sugar Content", | |
| ["Low Sugar", "Regular", "No Sugar"] | |
| ) | |
| Product_Allocated_Area = st.number_input( | |
| "Product Allocated Area (ratio)", | |
| min_value=0.0, | |
| max_value=0.4, | |
| value=0.07, | |
| step=0.01 | |
| ) | |
| Product_MRP = st.number_input( | |
| "Product MRP (Maximum Retail Price)", | |
| min_value=0.0, | |
| value=150.0, | |
| step=1.0 | |
| ) | |
| Product_Type_Category = st.selectbox( | |
| "Product Type Category", | |
| ["Perishables", "Non Perishables"] | |
| ) | |
| Product_Id_char = st.text_input( | |
| "Product ID Prefix(FD(Food), NC(No Consumable), DR(Drinks))", | |
| value="FD", | |
| max_chars=2 | |
| ) | |
| st.subheader("Store information") | |
| 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", "Food Mart"] | |
| ) | |
| Store_Age_Years = st.number_input( | |
| "Store Age (years)", | |
| min_value=0, | |
| value=10, | |
| step=1 | |
| ) | |
| # Build payload matching backend expectations | |
| product_data = { | |
| "Product_Weight": float(Product_Weight), | |
| "Product_Sugar_Content": Product_Sugar_Content, | |
| "Product_Allocated_Area": float(Product_Allocated_Area), | |
| "Product_MRP": float(Product_MRP), | |
| "Store_Size": Store_Size, | |
| "Store_Location_City_Type": Store_Location_City_Type, | |
| "Store_Type": Store_Type, | |
| "Product_Id_char": Product_Id_char, | |
| "Store_Age_Years": int(Store_Age_Years), | |
| "Product_Type_Category": Product_Type_Category, | |
| } | |
| st.markdown("----") | |
| if st.button("Predict Sales", type="primary"): | |
| with st.spinner("Calling SuperKart backend..."): | |
| try: | |
| response = requests.post( | |
| BACKEND_URL, | |
| json=product_data, | |
| timeout=20 | |
| ) | |
| if response.status_code == 200: | |
| result = response.json() | |
| predicted_sales = result.get("Sales", None) | |
| if predicted_sales is not None: | |
| st.success(f"β Predicted Total Store Sales: **{predicted_sales:,.2f}**") | |
| st.caption("Prediction returned by the tuned Bagging model deployed by Sergio Riveros.") | |
| else: | |
| st.error("The backend did not return a 'Sales' field.") | |
| st.code(json.dumps(result, indent=2)) | |
| else: | |
| st.error(f"Backend error. Status code: {response.status_code}") | |
| try: | |
| st.code(json.dumps(response.json(), indent=2)) | |
| except Exception: | |
| st.write(response.text) | |
| except Exception as e: | |
| st.error(f"Request failed: {e}") | |