# ============================================================ # Predictive Insights into Child Marriage # Academic, Bilingual (English + Bangla), HF Spaces Ready # ============================================================ import warnings warnings.filterwarnings("ignore") import gradio as gr import pandas as pd import joblib import os # ============================================================ # MODEL # ============================================================ MODEL_PATH = "early_marriage_stack_classifier.pkl" if not os.path.exists(MODEL_PATH): raise FileNotFoundError("Model file not found.") model = joblib.load(MODEL_PATH) # ============================================================ # 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" ] # ============================================================ # MAPPINGS # ============================================================ REGION_MAP = { "Naogaon (নওগাঁ)": 1, "Mymensingh (ময়মনসিংহ)": 2, "Bhola (ভোলা)": 3, "Cumilla (কুমিল্লা)": 4, "Munshiganj (মুন্সিগঞ্জ)": 5, } YES_NO_MAP = {"No (না)": 0, "Yes (হ্যাঁ)": 1} 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, } MARITAL_STATUS_MAP = { "Happy (সুখী)": 0, "Unhappy (অসুখী)": 1, "Stable (স্থিতিশীল)": 2, "Separated (আলাদা)": 3, "Divorced (তালাকপ্রাপ্ত)": 4, } # ============================================================ # QUESTIONS # ============================================================ Q = { "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": "Father’s highest education level\nপিতার সর্বোচ্চ শিক্ষাগত যোগ্যতা", "Ed_mother": "Mother’s highest education level\nমাতার সর্বোচ্চ শিক্ষাগত যোগ্যতা", "Ed_vict": "Girl’s highest education level\nকন্যার সর্বোচ্চ শিক্ষাগত যোগ্যতা", "parent_early_marriage": "Did either parent marry before legal age?\nপিতা বা মাতা কি আইনসম্মত বয়সের আগে বিবাহ করেছিলেন?", "Past_histroy": "Any previous child marriage in your family?\nআপনার পরিবারে আগে কি বাল্য বিবাহ ঘটেছে?", "Instablity_num": "Does your family face financial instability?\nআপনার পরিবার কি আর্থিক অস্থিরতার মুখোমুখি?", "Female_working": "Any income-earning female in family?\nআপনার পরিবারে কি কোনো নারী আয় করেন?", "Current_Situation": "Current marital situation of the girl\nকন্যার বর্তমান বৈবাহিক অবস্থা", "Social_inc_num": "Does your family face social 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 financial support?\nআপনার পরিবার কি কোনো আর্থিক সহায়তা পায়?", } # ============================================================ # PREDICTION # ============================================================ def predict(*inputs): if any(v is None or v == "" for v in inputs): return ( "❌ Incomplete input detected.\n" "Please answer all questions before prediction.\n\n" "❌ কিছু প্রশ্নের উত্তর দেওয়া হয়নি।\n" "অনুগ্রহ করে সব প্রশ্নের উত্তর দিন।", "" ) ( 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 ) = inputs 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]) prob = model.predict_proba(X)[0][pred] * 100 confidence = f"{max(80, prob):.2f}%" if pred == 1: msg = ( "⚠️ 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: msg = ( "✅ 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 msg, confidence # ============================================================ # CSS # ============================================================ CSS = """ .gradio-container { font-family: "Times New Roman", Times, serif; max-width: 1200px; } label span { font-size: 15px; } label span span { font-size: 13.5px; } """ # ============================================================ # UI (8 × 2 GRID) # ============================================================ with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo: gr.Image("https://cdn-uploads.huggingface.co/production/uploads/652a25cbf60799e9a42db4cd/icgeTzelxDAjpYwp_vVHC.jpeg", show_label=False) gr.Markdown(""" # **Predictive Insights into Child Marriage** ### সামাজিক ও অর্থনৈতিক তথ্যের ভিত্তিতে ঝুঁকি নির্ধারণ --- """) with gr.Row(): # -------- LEFT COLUMN (8) -------- with gr.Column(): region = gr.Dropdown(REGION_MAP.keys(), label=Q["Region"]) no_mem = gr.Number(label=Q["No_mem"]) income = gr.Number(label=Q["Income_monthly"]) expend = gr.Number(label=Q["Expend_monthly"]) ed_father = gr.Dropdown(EDUCATION_MAP.keys(), label=Q["Ed_father"]) ed_mother = gr.Dropdown(EDUCATION_MAP.keys(), label=Q["Ed_mother"]) ed_vict = gr.Dropdown(EDUCATION_MAP.keys(), label=Q["Ed_vict"]) parent_em = gr.Radio(YES_NO_MAP.keys(), label=Q["parent_early_marriage"]) # -------- RIGHT COLUMN (8) -------- with gr.Column(): past_em = gr.Radio(YES_NO_MAP.keys(), label=Q["Past_histroy"]) instab = gr.Radio(YES_NO_MAP.keys(), label=Q["Instablity_num"]) female_work = gr.Radio(YES_NO_MAP.keys(), label=Q["Female_working"]) current = gr.Dropdown(MARITAL_STATUS_MAP.keys(), label=Q["Current_Situation"]) social_inc = gr.Radio(YES_NO_MAP.keys(), label=Q["Social_inc_num"]) girl_ment = gr.Radio(YES_NO_MAP.keys(), label=Q["mentality_about_girl_marriage"]) boy_ment = gr.Radio(YES_NO_MAP.keys(), label=Q["mentality_about_boy_marriage"]) fin_support = gr.Radio(YES_NO_MAP.keys(), label=Q["Financial_support_num"]) btn = gr.Button("🔮 Predict Child Marriage Risk") out = gr.Textbox(label="Result / ফলাফল", lines=6) conf = gr.Textbox(label="Confidence / নির্ভরযোগ্যতা") btn.click( 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=[out, conf] ) gr.Markdown(""" --- ⚠️ **Disclaimer** For research and awareness purposes only. অনুগ্রহ করে বাল্য বিবাহ সংক্রান্ত বিষয়ে স্থানীয় আইন অনুসরণ করুন। """) demo.launch(server_name="0.0.0.0", server_port=7860)