Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| import json | |
| # Configure page | |
| st.set_page_config( | |
| page_title="SuperKart Sales Forecasting", | |
| page_icon="π", | |
| layout="wide" | |
| ) | |
| # Backend API URL | |
| BACKEND_URL = "https://deepakdm411-shoppingcartbackend.hf.space" | |
| st.title("π SuperKart Sales Forecasting System") | |
| st.write("Predict sales revenue for SuperKart products using our advanced ML model") | |
| # API connection status | |
| def check_backend_connection(): | |
| try: | |
| response = requests.get(f"{BACKEND_URL}/", timeout=10) | |
| return response.status_code == 200 | |
| except Exception as e: | |
| st.error(f"Connection error: {str(e)}") | |
| return False | |
| # Check backend status | |
| with st.spinner("Checking API connection..."): | |
| backend_online = check_backend_connection() | |
| if backend_online: | |
| st.success("β Connected to backend API") | |
| else: | |
| st.error("β Backend API not available. Please check the backend URL.") | |
| st.info(f"Current backend URL: {BACKEND_URL}") | |
| st.subheader("Enter Product and Store Details:") | |
| # Create input form | |
| with st.form("prediction_form"): | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.markdown("**πͺ Store Information**") | |
| store_type = st.selectbox( | |
| "Store Type", | |
| ["Supermarket Type1", "Supermarket Type2", "Supermarket Type3", "Grocery Store"] | |
| ) | |
| store_size = st.selectbox("Store Size", ["Small", "Medium", "High"]) | |
| store_location = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"]) | |
| store_year = st.number_input("Store Establishment Year", min_value=1980, max_value=2024, value=2010) | |
| with col2: | |
| st.markdown("**π¦ Product Information**") | |
| product_type = st.selectbox( | |
| "Product Type", | |
| ["Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables", "Household", | |
| "Baking Goods", "Snack Foods", "Frozen Foods", "Breakfast", | |
| "Health and Hygiene", "Hard Drinks", "Canned", "Bread", | |
| "Starchy Foods", "Others", "Seafood"] | |
| ) | |
| product_sugar = st.selectbox("Product Sugar Content", ["Low Fat", "Regular"]) | |
| product_weight = st.number_input("Product Weight", min_value=0.01, value=1.0) | |
| product_mrp = st.number_input("Product MRP", min_value=1.0, value=100.0) | |
| product_area = st.number_input("Product Allocated Area", min_value=0.001, value=0.1) | |
| # Submit button | |
| submitted = st.form_submit_button("π― Predict Sales Revenue", type="primary") | |
| # Handle form submission | |
| if submitted and backend_online: | |
| # Prepare data for API | |
| prediction_data = { | |
| "Product_Weight": product_weight, | |
| "Product_Sugar_Content": product_sugar, | |
| "Product_Allocated_Area": product_area, | |
| "Product_Type": product_type, | |
| "Product_MRP": product_mrp, | |
| "Store_Establishment_Year": store_year, | |
| "Store_Size": store_size, | |
| "Store_Location_City_Type": store_location, | |
| "Store_Type": store_type | |
| } | |
| # Make API call | |
| with st.spinner("Making prediction..."): | |
| try: | |
| response = requests.post( | |
| f"{BACKEND_URL}/predict", | |
| json=prediction_data, | |
| headers={"Content-Type": "application/json"}, | |
| timeout=30 | |
| ) | |
| if response.status_code == 200: | |
| result = response.json() | |
| # Display results | |
| st.success("β Prediction Complete!") | |
| # Main prediction display | |
| col1, col2, col3 = st.columns([1, 2, 1]) | |
| with col2: | |
| st.metric( | |
| label="Predicted Sales Revenue", | |
| value=result["formatted_prediction"] | |
| ) | |
| # Additional details | |
| st.markdown("---") | |
| st.markdown("### π Prediction Details") | |
| detail_col1, detail_col2 = st.columns(2) | |
| with detail_col1: | |
| st.info(f""" | |
| **Store Profile:** | |
| - Type: {store_type} | |
| - Size: {store_size} | |
| - Location: {store_location} | |
| - Established: {store_year} | |
| """) | |
| with detail_col2: | |
| st.info(f""" | |
| **Product Profile:** | |
| - Category: {product_type} | |
| - Weight: {product_weight} kg | |
| - MRP: βΉ{product_mrp} | |
| - Sugar Content: {product_sugar} | |
| """) | |
| # Business insights | |
| prediction_value = result["prediction"] | |
| if prediction_value > product_mrp * 10: | |
| st.success("π Excellent Revenue Potential!") | |
| elif prediction_value > product_mrp * 5: | |
| st.info("π Good Revenue Potential") | |
| else: | |
| st.warning("π Moderate Revenue Potential") | |
| else: | |
| error_data = response.json() | |
| st.error(f"Prediction failed: {error_data.get('error', 'Unknown error')}") | |
| except requests.exceptions.Timeout: | |
| st.error("β° Request timed out. Please try again.") | |
| except requests.exceptions.ConnectionError: | |
| st.error("π Connection error. Please check if backend is running.") | |
| except Exception as e: | |
| st.error(f"An error occurred: {str(e)}") | |
| elif submitted and not backend_online: | |
| st.error("Cannot make prediction - backend API is not available.") | |
| # Sidebar with API information | |
| st.sidebar.markdown("### π§ API Information") | |
| if backend_online: | |
| try: | |
| model_info = requests.get(f"{BACKEND_URL}/model-info", timeout=10).json() | |
| st.sidebar.success("β API Online") | |
| st.sidebar.json(model_info) | |
| except: | |
| st.sidebar.warning("β οΈ Could not fetch model info") | |
| else: | |
| st.sidebar.error("β API Offline") | |
| st.sidebar.markdown(f"**Backend URL:** {BACKEND_URL}") | |
| # Footer | |
| st.markdown("---") | |
| st.markdown(""" | |
| <div style='text-align: center; color: #666;'> | |
| <p>SuperKart Sales Forecasting System | Built with Streamlit & Flask</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |