File size: 3,476 Bytes
23af5ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import json

# Page configuration
st.set_page_config(
    page_title="SuperKart Sales Predictor",
    page_icon="πŸ›’",
    layout="centered"
)

# Debug: Print to logs
print("Streamlit app starting...")

# Title and description
st.title("πŸ›’ SuperKart Sales Predictor")
st.markdown("Predict product sales using your tuned Random Forest model. Enter details below!")

# Input fields matching SuperKart dataset
col1, col2 = st.columns(2)

with col1:
    st.subheader("Product Information")
    product_weight = st.number_input("Product Weight", min_value=0.0, max_value=50.0, value=12.0, step=0.1)
    product_mrp = st.number_input("Product MRP ($)", min_value=0.0, max_value=10000.0, value=150.0, step=0.01)
    product_sugar = st.selectbox("Product Sugar Content", ['Low Fat', 'Regular', 'Low Sugar', 'LF'])
    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', 'Breads', 
                                'Starchy Foods', 'Others'])

with col2:
    st.subheader("Store Information")
    store_size = st.selectbox("Store Size", ['Small', 'Medium', 'High'])
    store_location = st.selectbox("Store Location Type", ['Tier 1', 'Tier 2', 'Tier 3'])
    store_type = st.selectbox("Store Type", 
                             ['Grocery Store', 'Supermarket Type1', 'Supermarket Type2', 'Supermarket Type3'])

# Prediction button
if st.button("Predict Sales"):
    # Prepare data for your backend API
    data = {
        "Product_Weight": product_weight,
        "Product_MRP": product_mrp,
        "Product_Sugar_Content": product_sugar,
        "Product_Type": product_type,
        "Store_Size": store_size,
        "Store_Location_City_Type": store_location,
        "Store_Type": store_type
    }

    # Debug: Print data being sent
    print(f"Sending data: {data}")

    # Call your deployed backend API
    # REPLACE YOUR_USERNAME with your actual Hugging Face username
    api_url = "https://toddmattingly-superkart-backend.hf.space/predict"
    
    try:
        response = requests.post(api_url, json=data, timeout=10)
        print(f"API response status: {response.status_code}")
        
        if response.status_code == 200:
            # API returns a list directly (based on your testing)
            predictions = response.json()
            prediction = predictions[0] if isinstance(predictions, list) and len(predictions) > 0 else 0
            
            st.success(f"🎯 Predicted Sales Total: ${prediction:,.2f}")
            st.info(f"πŸ“Š Based on: {product_type} at ${product_mrp:,.2f} MRP in a {store_type}")
        else:
            st.error(f"API Error: {response.status_code} - {response.text}")
            print(f"API Error: {response.status_code} - {response.text}")
            
    except requests.exceptions.RequestException as e:
        st.error(f"Connection Error: {str(e)}")
        print(f"Connection Error: {str(e)}")
    except Exception as e:
        st.error(f"Unexpected Error: {str(e)}")
        print(f"Unexpected Error: {str(e)}")

# Footer
st.markdown("---")
st.markdown("*Powered by Streamlit & Hugging Face Spaces*")
st.markdown("*Using your tuned Random Forest model*")

print("Streamlit app loaded successfully.")