Spaces:
Sleeping
Sleeping
| # streamlit_app.py | |
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| # ----------------- CONFIGURATION ----------------- | |
| # CRITICAL FIX: Setting the path to /api/predict. | |
| # The backend Flask app listens on '/predict', but the HF Space infrastructure | |
| # requires the /api/ prefix for external routing to the Docker container. | |
| API_URL = "https://aibtus.ut.aiml.hf.space/api/predict" | |
| # Define static categorical options (MUST match preprocessing categories) | |
| CATEGORIES = { | |
| 'Product_Sugar_Content': ['no sugar', 'low sugar', 'regular'], | |
| 'Store_Size': ['Low', 'Medium', 'High'], | |
| 'Store_Location_City_Type': ['Tier 1', 'Tier 2', 'Tier 3'], | |
| 'Store_Type': ['Supermarket Type 1', 'Supermarket Type 2', 'Supermarket Type 3', 'Food Mart'], | |
| 'Product_Type': ['Snack Foods', 'Dairy', 'Baking Goods', 'Health and Hygiene', 'Frozen Foods', 'Others', | |
| 'Soft Drinks', 'Household', 'Meat', 'Fruits and Vegetables', 'Breads', 'Breakfast', | |
| 'Hard Drinks', 'Starchy Foods', 'Seafood', 'Canned'], | |
| 'Product_Category_Simplified': ['FD', 'DR', 'NC'] | |
| } | |
| FEATURE_COLS = [ | |
| 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area', | |
| 'Product_Type', 'Product_MRP', 'Store_Size', | |
| 'Store_Location_City_Type', 'Store_Type', 'Store_Age', | |
| 'Product_Category_Simplified' | |
| ] | |
| # ----------------- APP INTERFACE ----------------- | |
| st.set_page_config(page_title="SuperKart Sales Forecaster", layout="wide") | |
| st.title(" SuperKart Sales Forecasting Tool") | |
| with st.form("sales_prediction_form"): | |
| st.header("Product Attributes") | |
| col1, col2, col3 = st.columns(3) | |
| product_weight = col1.number_input("Product Weight (kg)", min_value=1.0, max_value=20.0, value=10.0, step=0.1) | |
| product_mrp = col2.number_input("Product MRP (₹)", min_value=10.0, max_value=300.0, value=150.0, step=1.0) | |
| product_allocated_area = col3.number_input("Product Allocated Area", min_value=0.0, max_value=1.0, value=0.07, step=0.01) | |
| product_type = col1.selectbox("Product Type", options=CATEGORIES['Product_Type']) | |
| product_sugar_content = col2.selectbox("Product Sugar Content", options=CATEGORIES['Product_Sugar_Content']) | |
| product_category_simplified = col3.selectbox("Product Category (FD/DR/NC)", options=CATEGORIES['Product_Category_Simplified']) | |
| st.header("Store Attributes") | |
| col4, col5, col6 = st.columns(3) | |
| store_type = col4.selectbox("Store Type", options=CATEGORIES['Store_Type']) | |
| store_location_city_type = col5.selectbox("Store Location City Type", options=CATEGORIES['Store_Location_City_Type']) | |
| store_size = col6.selectbox("Store Size", options=CATEGORIES['Store_Size']) | |
| store_age = st.slider("Store Age (Years)", min_value=5, max_value=35, value=10) | |
| submitted = st.form_submit_button("Forecast Sales") | |
| if submitted: | |
| input_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_Size': store_size, | |
| 'Store_Location_City_Type': store_location_city_type, | |
| 'Store_Type': store_type, | |
| 'Store_Age': float(store_age), | |
| 'Product_Category_Simplified': product_category_simplified | |
| } | |
| payload = [input_data] | |
| try: | |
| st.info(f"Sending request to API at: {API_URL}...") | |
| # FIX: verify=False flag kept to prevent the SSLError from recurring. | |
| response = requests.post(API_URL, json=payload, timeout=15, verify=False) | |
| if response.status_code == 200: | |
| result = response.json() | |
| if result['status'] == 'success': | |
| st.success(f" **Forecast Complete!**") | |
| st.metric(label="Predicted Sales Revenue", value=f"₹{result['predicted_sales_revenue']:,.2f}") | |
| st.balloons() | |
| else: | |
| st.error(f"Prediction API Error: {result.get('error', 'Unknown error')}") | |
| else: | |
| st.error(f"**API Connection Error** (Status {response.status_code}): Check API URL and Docker logs.") | |
| except requests.exceptions.Timeout: | |
| st.error("**Connection Failed:** Request timed out (15s). The Docker API might be slow or unreachable.") | |
| except Exception as e: | |
| st.error(f"An unexpected error occurred: {e}") | |