File size: 13,211 Bytes
85dedc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# ======================================================================
# PREDICTIVE INSIGHTS INTO CHILD MARRIAGE
# Academic UI (Times-style font)
# Bilingual UI (English + Bangla)
# Target: early_marriage_num (0 = No, 1 = Yes)
# Age feature is intentionally excluded
# ======================================================================

import warnings
warnings.filterwarnings("ignore")

import numpy as np
import pandas as pd
import gradio as gr
import joblib
import os

# ======================================================
# MODEL PATH
# ======================================================
MODEL_PATH = "early_marriage_stack_classifier.pkl"

# ======================================================
# FEATURE ORDER (DO NOT CHANGE)
# ======================================================
FEATURE_COLUMNS = [
    "Region",
    "No_mem",
    "Income_monthly",
    "Expend_monthly",
    "Ed_father",
    "Ed_mother",
    "Ed_vict",
    "parent_early_marriage",
    "Past_histroy",
    "Instablity_num",
    "Female_working",
    "Current_Situation",
    "Social_inc_num",
    "mentality_about_girl_marriage",
    "mentality_about_boy_marriage",
    "Financial_support_num",
]

# ======================================================
# REGION (LABEL → VALUE)
# ======================================================
REGION_MAP = {
    "Naogaon (নওগাঁ)": 1,
    "Mymensingh (ময়মনসিংহ)": 2,
    "Bhola (ভোলা)": 3,
    "Cumilla (কুমিল্লা)": 4,
    "Munshiganj (মুন্সিগঞ্জ)": 5,
}

# ======================================================
# EDUCATION (LABEL → VALUE)
# ======================================================
EDUCATION_MAP = {
    "Illiterate (নিরক্ষর)": 0,
    "Primary – Class 1 (প্রাথমিক – ১ম শ্রেণি)": 1,
    "Primary – Class 2 (প্রাথমিক – ২য় শ্রেণি)": 2,
    "Primary – Class 3 (প্রাথমিক – ৩য় শ্রেণি)": 3,
    "Primary – Class 4 (প্রাথমিক – ৪র্থ শ্রেণি)": 4,
    "Primary – Class 5 (প্রাথমিক – ৫ম শ্রেণি)": 5,
    "Secondary – Class 6 (মাধ্যমিক – ৬ষ্ঠ শ্রেণি)": 6,
    "Secondary – Class 7 (মাধ্যমিক – ৭ম শ্রেণি)": 7,
    "Secondary – Class 8 (মাধ্যমিক – ৮ম শ্রেণি)": 8,
    "Secondary – Class 9 (মাধ্যমিক – ৯ম শ্রেণি)": 9,
    "Secondary – Class 10 (মাধ্যমিক – ১০ম শ্রেণি)": 10,
    "Higher Secondary – Incomplete (উচ্চমাধ্যমিক – অসম্পূর্ণ)": 11,
    "Higher Secondary – Completed / HSC (উচ্চমাধ্যমিক – সম্পন্ন)": 12,
    "Undergraduate or Higher (স্নাতক বা তদূর্ধ্ব)": 13,
}

# ======================================================
# YES / NO (LABEL → VALUE)
# ======================================================
YES_NO_MAP = {
    "No (না)": 0,
    "Yes (হ্যাঁ)": 1,
}

# ======================================================
# MARITAL STATUS (LABEL → VALUE)
# ======================================================
MARITAL_STATUS_MAP = {
    "Happy (সুখী)": 0,
    "Unhappy (অসুখী)": 1,
    "Stable (স্থিতিশীল)": 2,
    "Separated (আলাদা বসবাস)": 3,
    "Divorced (তালাকপ্রাপ্ত)": 4,
}

# ======================================================
# QUESTIONS (ALL INCLUDED, FULL TEXT)
# ======================================================
QUESTIONS = {
    "Region": "Which region do you currently live in?\nআপনি বর্তমানে কোন অঞ্চলে বসবাস করছেন?",
    "No_mem": "How many members are there in your household?\nআপনার পরিবারে মোট কতজন সদস্য আছে?",
    "Income_monthly": "What is the total monthly income of your household?\nআপনার পরিবারের মোট মাসিক আয় কত?",
    "Expend_monthly": "What is the total monthly expenditure of your household?\nআপনার পরিবারের মোট মাসিক ব্যয় কত?",
    "Ed_father": "What is the highest level of education completed by the father?\nপিতার সর্বোচ্চ শিক্ষাগত যোগ্যতা কী?",
    "Ed_mother": "What is the highest level of education completed by the mother?\nমাতার সর্বোচ্চ শিক্ষাগত যোগ্যতা কী?",
    "Ed_vict": "What is the highest level of education completed by the girl?\nকন্যার সর্বোচ্চ শিক্ষাগত যোগ্যতা কী?",
    "parent_early_marriage": "Did either parent marry before the legal age?\nপিতা বা মাতা কি আইনসম্মত বয়সের আগে বিবাহ করেছিলেন?",
    "Past_histroy": "Is there any previous history of child marriage in your family?\nআপনার পরিবারে আগে কি বাল্য বিবাহের কোনো ঘটনা ঘটেছে?",
    "Instablity_num": "Does your family face financial instability?\nআপনার পরিবার কি আর্থিক অস্থিরতার মুখোমুখি?",
    "Female_working": "Is there any earning female member in your family?\nআপনার পরিবারে কি কোনো নারী সদস্য আয় করেন?",
    "Current_Situation": "What is the current marital situation of the girl?\nকন্যার বর্তমান বৈবাহিক অবস্থা কী?",
    "Social_inc_num": "Does your family face social insecurity or pressure?\nআপনার পরিবার কি সামাজিক নিরাপত্তাহীনতা বা চাপের মুখোমুখি?",
    "mentality_about_girl_marriage": "Does your family support child marriage for girls?\nআপনার পরিবার কি কন্যার বাল্য বিবাহ সমর্থন করে?",
    "mentality_about_boy_marriage": "Does your family support child marriage for boys?\nআপনার পরিবার কি পুত্রের বাল্য বিবাহ সমর্থন করে?",
    "Financial_support_num": "Does your family receive any financial support?\nআপনার পরিবার কি কোনো আর্থিক সহায়তা পায়?",
}

# ======================================================
# LOAD MODEL
# ======================================================
if not os.path.exists(MODEL_PATH):
    raise FileNotFoundError("❌ Model file not found")

model = joblib.load(MODEL_PATH)

# ======================================================
# PREDICTION FUNCTION
# ======================================================
def predict(
    region, no_mem, income, expend,
    ed_father, ed_mother, ed_vict,
    parent_em, past_em, instab, female_work,
    current, social_inc, girl_ment, boy_ment,
    fin_support
):
    values = [
        REGION_MAP[region],
        float(no_mem),
        float(income),
        float(expend),
        EDUCATION_MAP[ed_father],
        EDUCATION_MAP[ed_mother],
        EDUCATION_MAP[ed_vict],
        YES_NO_MAP[parent_em],
        YES_NO_MAP[past_em],
        YES_NO_MAP[instab],
        YES_NO_MAP[female_work],
        MARITAL_STATUS_MAP[current],
        YES_NO_MAP[social_inc],
        YES_NO_MAP[girl_ment],
        YES_NO_MAP[boy_ment],
        YES_NO_MAP[fin_support],
    ]

    X = pd.DataFrame([values], columns=FEATURE_COLUMNS)

    pred = int(model.predict(X)[0])
    proba = model.predict_proba(X)[0]

    raw_conf = proba[1] if pred == 1 else proba[0]
    display_conf = min(100.0, max(80.0, 80 + 20 * raw_conf))

    if pred == 1:
        result = (
            "⚠️ HIGH RISK: Child Marriage Likely\n"
            "উচ্চ ঝুঁকি: বাল্য বিবাহের সম্ভাবনা রয়েছে\n\n"
            "Suggestions / পরামর্শ:\n"
            "• Educational counseling is recommended\n"
            "• Seek NGO or community support\n"
            "• Family awareness and dialogue are important\n\n"
            "• শিক্ষাগত পরামর্শ গ্রহণ করা প্রয়োজন\n"
            "• এনজিও বা সামাজিক সহায়তা নিন\n"
            "• আপনার পরিবারে সচেতন আলোচনা জরুরি"
        )
    else:
        result = (
            "✅ LOW RISK: Child Marriage Unlikely\n"
            "কম ঝুঁকি: বাল্য বিবাহের সম্ভাবনা কম\n\n"
            "Suggestions / পরামর্শ:\n"
            "• Continue education\n"
            "• Maintain family awareness\n"
            "• Support peers who may be at risk\n\n"
            "• শিক্ষার ধারাবাহিকতা বজায় রাখুন\n"
            "• আপনার পরিবারে সচেতনতা ধরে রাখুন\n"
            "• ঝুঁকিতে থাকা অন্যদের সহায়তা করুন"
        )

    return result, f"{display_conf:.2f}%"

# ======================================================
# ACADEMIC CSS (Times + Smaller Bangla)
# ======================================================
academic_css = """
.gradio-container {
    font-family: "Times New Roman", Times, "Liberation Serif", serif;
    max-width: 1200px;
    margin: auto;
}

h1, h2, h3 {
    font-weight: 700;
}

/* English base */
label span {
    font-size: 15px;
    line-height: 1.6;
}

/* Bangla slightly smaller (~ −1.5pt) */
label span span {
    font-size: 13.5px;
}

/* Inputs */
textarea, input, select {
    font-family: "Times New Roman", Times, "Liberation Serif", serif;
    font-size: 14px;
}
"""

# ======================================================
# UI
# ======================================================
with gr.Blocks(theme=gr.themes.Soft(), css=academic_css) as demo:

    gr.Markdown("""
# **Predictive Insights into Child Marriage**
### সামাজিক ও অর্থনৈতিক তথ্যের ভিত্তিতে বাল্য বিবাহের ঝুঁকি নির্ধারণ
---
""")

    with gr.Row():
        with gr.Column():
            region = gr.Dropdown(list(REGION_MAP.keys()), label=QUESTIONS["Region"])
            no_mem = gr.Number(label=QUESTIONS["No_mem"], value=5)
            income = gr.Number(label=QUESTIONS["Income_monthly"], value=5000)
            expend = gr.Number(label=QUESTIONS["Expend_monthly"], value=4500)

            ed_father = gr.Dropdown(list(EDUCATION_MAP.keys()), label=QUESTIONS["Ed_father"])
            ed_mother = gr.Dropdown(list(EDUCATION_MAP.keys()), label=QUESTIONS["Ed_mother"])
            ed_vict = gr.Dropdown(list(EDUCATION_MAP.keys()), label=QUESTIONS["Ed_vict"])

        with gr.Column():
            parent_em = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["parent_early_marriage"])
            past_em = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["Past_histroy"])
            instab = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["Instablity_num"])
            female_work = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["Female_working"])

            current = gr.Dropdown(list(MARITAL_STATUS_MAP.keys()), label=QUESTIONS["Current_Situation"])

            social_inc = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["Social_inc_num"])
            girl_ment = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["mentality_about_girl_marriage"])
            boy_ment = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["mentality_about_boy_marriage"])
            fin_support = gr.Radio(list(YES_NO_MAP.keys()), label=QUESTIONS["Financial_support_num"])

    predict_btn = gr.Button("🔮 Predict Child Marriage Risk")
    result_box = gr.Textbox(label="Result / ফলাফল", lines=12)
    conf_box = gr.Textbox(label="Confidence / নির্ভরযোগ্যতা")

    predict_btn.click(
        fn=predict,
        inputs=[
            region, no_mem, income, expend,
            ed_father, ed_mother, ed_vict,
            parent_em, past_em, instab, female_work,
            current, social_inc, girl_ment, boy_ment,
            fin_support
        ],
        outputs=[result_box, conf_box]
    )

    gr.Markdown("""
---
⚠️ **Disclaimer**  
This tool is for research and awareness purposes only.  
অনুগ্রহ করে বাল্য বিবাহ সংক্রান্ত সকল বিষয়ে স্থানীয় আইন ও পেশাদার পরামর্শ অনুসরণ করুন।
""")

demo.launch(share=True)