File size: 6,475 Bytes
0285403
 
 
 
 
 
 
 
 
 
 
 
1ca690b
 
0285403
 
 
 
 
 
 
 
 
1ca690b
 
0285403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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)