File size: 5,521 Bytes
9b3d57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests, os

st.set_page_config(
    page_title="ChildCare Revenue Predictor",
    page_icon="๐Ÿซ",
    layout="wide",
)

st.title("๐Ÿซ ChildCare Facility Revenue Predictor")
st.markdown("""
Predict the **monthly revenue** a child will generate for a day care facility,
based on child profile, program enrolment, and facility characteristics.

Fill in the details below and click **Predict Revenue**.
""")

BACKEND_URL = os.environ.get(
    "BACKEND_URL",
    "https://ramzai9-childcareprediction.hf.space"
)

with st.sidebar:
    st.header("โ„น๏ธ How to Use")
    st.markdown("""
1. Enter the child's details in the **Child Profile** section.
2. Enter the facility details in the **Facility Details** section.
3. Click **Predict Revenue** to get the model's estimate.

**Field Guide:**
- **Child Age** โ€“ child's current age in months (6 = 6 months, 60 = 5 years)
- **Care Program** โ€“ Full Day covers 8+ hours; Half Day covers 4โ€“5 hours; After School is 3โ€“4 hours after school
- **Attendance Rate** โ€“ proportion of days the child attends (1.0 = never absent)
- **Monthly Fee** โ€“ the fee charged per month in USD
- **Child ID Prefix** โ€“ two-letter code derived from program (FD, HD, AS)
- **Activity Category** โ€“ primary activity type the child is enrolled in
""")
    st.divider()
    st.markdown("**Backend API:**")
    st.code(BACKEND_URL, language=None)

col1, col2 = st.columns(2)

with col1:
    st.subheader("๐Ÿ‘ถ Child Profile")
    child_age = st.number_input(
        "Child Age (months)",
        min_value=0.0, max_value=120.0, value=36.0, step=1.0,
        help="Enter the child's age in months. E.g. 24 = 2 years old.",
    )
    care_program = st.selectbox(
        "Care Program",
        options=["Full Day", "Half Day", "After School"],
        help="Full Day (8+ hrs), Half Day (4โ€“5 hrs), After School (3โ€“4 hrs after school).",
    )
    attendance_rate = st.slider(
        "Attendance Rate",
        min_value=0.50, max_value=1.00, value=0.85, step=0.01,
        help="Proportion of scheduled days the child attends. 1.0 = 100% attendance.",
    )
    monthly_fee = st.number_input(
        "Monthly Fee ($)",
        min_value=0.0, max_value=10000.0, value=2000.0, step=50.0,
        help="The monthly fee charged for the child's enrolment, in USD.",
    )
    child_id_char = st.selectbox(
        "Child ID Prefix",
        options=["FD", "HD", "AS"],
        help="FD = Full Day, HD = Half Day, AS = After School.",
    )
    activity_category = st.selectbox(
        "Activity Type Category",
        options=["Academic", "Creative", "Wellness"],
        help="Academic: Reading, Math, STEM. Creative: Arts, Music, Dance. Wellness: PE, Yoga, Outdoor Play.",
    )

with col2:
    st.subheader("๐Ÿข Facility Details")
    facility_size = st.selectbox(
        "Facility Size",
        options=["Small", "Medium", "Large"],
        help="Small (<30 children), Medium (30โ€“80 children), Large (80+ children).",
    )
    city_type = st.selectbox(
        "City Type",
        options=["Tier 1", "Tier 2", "Tier 3"],
        help="Tier 1 = major metropolitan area. Tier 2 = mid-sized city. Tier 3 = small town or rural.",
    )
    facility_type = st.selectbox(
        "Facility Type",
        options=["Full-Service Center", "Montessori School", "Home Daycare", "Corporate Daycare"],
        help="Type of facility offering child care services.",
    )
    facility_year = st.number_input(
        "Facility Establishment Year",
        min_value=1900, max_value=2100, value=2005, step=1,
        help="The year the facility was established. Older facilities may have more established reputations.",
    )

st.divider()
predict_btn = st.button("๐Ÿ”ฎ Predict Revenue", type="primary", use_container_width=True)

if predict_btn:
    payload = {
        "Child_Age_Months":            child_age,
        "Child_Care_Program":          care_program,
        "Child_Attendance_Rate":       attendance_rate,
        "Child_Monthly_Fee":           monthly_fee,
        "Facility_Size":               facility_size,
        "Facility_Location_City_Type": city_type,
        "Facility_Type":               facility_type,
        "Child_Id_char":               child_id_char,
        "Facility_Establishment_Year": facility_year,
        "Activity_Type_Category":      activity_category,
    }

    with st.spinner("Contacting the prediction API..."):
        try:
            resp = requests.post(
                f"{BACKEND_URL}/v1/predict",
                json=payload,
                timeout=30,
            )
            if resp.status_code == 200:
                revenue = resp.json().get("Revenue", "N/A")
                st.success(f"### ๐Ÿ’ฐ Predicted Monthly Revenue: **${revenue:,.2f}**")
                st.balloons()

                with st.expander("๐Ÿ“‹ Input Summary"):
                    st.json(payload)
            else:
                st.error(f"API Error ({resp.status_code}): {resp.json().get('error', resp.text)}")
        except requests.exceptions.Timeout:
            st.error("โฑ๏ธ Request timed out. The backend may be starting up โ€” please try again in 30 seconds.")
        except requests.exceptions.ConnectionError:
            st.error("๐Ÿ”Œ Could not connect to the backend. Please verify the BACKEND_URL environment variable.")
        except Exception as e:
            st.error(f"Unexpected error: {e}")

st.divider()
st.caption("Built with XGBoost + Flask + Streamlit ยท Deployed on Hugging Face Spaces")