File size: 5,320 Bytes
69302a0
 
 
 
 
 
 
 
460b7e2
 
 
 
 
 
7cfef98
69302a0
627ce6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69302a0
 
 
 
627ce6d
69302a0
 
25a4fd3
627ce6d
 
 
 
 
 
 
 
 
 
25a4fd3
627ce6d
69302a0
627ce6d
69302a0
627ce6d
 
 
 
 
69302a0
 
627ce6d
 
 
 
69302a0
 
 
 
 
627ce6d
69302a0
 
 
25a4fd3
627ce6d
 
 
 
 
69302a0
 
 
 
 
 
 
 
25a4fd3
69302a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cfef98
69302a0
 
25a4fd3
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
import gradio as gr
import joblib
import pandas as pd
import numpy as np
import os

# Load all trained models
models = {
    "Logistic Regression": joblib.load('logistic_regression_model.joblib'),
    "Random Forest": joblib.load('random_forest_model.joblib'),
    "Support Vector Machine (SVM)": joblib.load('svm_model.joblib'),
    "Decision Tree": joblib.load('decision_tree_model.joblib'),
    "XGBoost": joblib.load('xgboost_model.joblib'),
    "AdaBoost": joblib.load('adaboost_model.joblib')
}  # ← fixed: closing brace present

# --- BACKGROUND MAPPINGS ---
mappings = {
    "Gender": {"Male": 1, "Female": 0}, 
    "YesNo": {"Yes": 1, "No": 0},
    "Income": {
        "Below ₹3,00,000": 0,
        "₹3,00,001 - ₹5,00,000": 1,
        "₹5,00,001 - ₹8,00,000": 2,
        "₹8,00,001 - ₹12,00,000": 3,
        "Above ₹12,00,000": 4
    },
    "Grades": {
        "Below 50%": 0,
        "50% - 59.9%": 1,
        "60% - 69.9%": 2,
        "70% - 79.9%": 3,
        "80% - 89.9%": 4,
        "90% and above": 5
    },
    "Social_Category": {"General": 0, "OBC": 1, "SC": 2, "ST": 3, "EWS": 4},
    "Domicile_Type": {"Urban": 0, "Semi-Urban": 1, "Rural": 2}
}

def predict_risk(model_name, gender, age, social_category, domicile_type, first_gen, 
                 disability_status, income_bracket, family_members, earning_members,
                 scholarship, loan_status, class10, class12, cgpa, backlogs, year_backs):
    
    # 1. Select the model
    model = models[model_name]
    
    # 2. Convert text to numbers
    encoded_gender = mappings["Gender"].get(gender, 0)
    encoded_first_gen = mappings["YesNo"].get(first_gen, 0)
    encoded_disability = mappings["YesNo"].get(disability_status, 0)
    encoded_scholarship = mappings["YesNo"].get(scholarship, 0)
    encoded_loan = mappings["YesNo"].get(loan_status, 0)
    encoded_income = mappings["Income"].get(income_bracket, 0)
    encoded_class10 = mappings["Grades"].get(class10, 0)
    encoded_class12 = mappings["Grades"].get(class12, 0)
    encoded_social = mappings["Social_Category"].get(social_category, 0)
    encoded_domicile = mappings["Domicile_Type"].get(domicile_type, 0)
    
    # 3. Create the numeric DataFrame
    input_data = pd.DataFrame({
        'Gender_bin': [encoded_gender],
        'Age': [age],
        'Social_Category': [encoded_social], 
        'Domicile_Type': [encoded_domicile],
        'First_Gen_bin': [encoded_first_gen],
        'Disability_Status_bin': [encoded_disability],
        'Income_ord': [encoded_income],
        'Total_Family_Members': [family_members],
        'Earning_Members': [earning_members],
        'Scholarship_Status_bin': [encoded_scholarship],
        'Education_Loan_Status_bin': [encoded_loan],
        'Class10_ord': [encoded_class10],
        'Class12_ord': [encoded_class12],
        'Current_CGPA': [cgpa],
        'Current_Backlogs': [backlogs],
        'Year_Backs': [year_backs]
    })
    
    # 4. Predict
    prediction = model.predict(input_data)[0]
    result = "At Risk" if prediction >= 2 else "Not At Risk"
    
    # 5. Calculate confidence
    try:
        probabilities = model.predict_proba(input_data)
        confidence = float(np.max(probabilities)) * 100
    except:
        confidence = 85.0 
    
    return {
        "model_used": model_name,
        "prediction": result,
        "confidence": round(confidence, 2),
        "risk_score": float(prediction)
    }

# Create Gradio interface
iface = gr.Interface(
    fn=predict_risk,
    inputs=[
        gr.Dropdown(list(models.keys()), label="Select Model", value="XGBoost"),
        gr.Dropdown(["Male", "Female"], label="Gender"),
        gr.Number(label="Age", minimum=17, maximum=30),
        gr.Dropdown(["General", "OBC", "SC", "ST", "EWS"], label="Social Category"),
        gr.Dropdown(["Urban", "Semi-Urban", "Rural"], label="Domicile Type"),
        gr.Dropdown(["Yes", "No"], label="First Generation"),
        gr.Dropdown(["Yes", "No"], label="Disability Status"),
        gr.Dropdown(["Below ₹3,00,000", "₹3,00,001 - ₹5,00,000", "₹5,00,001 - ₹8,00,000", 
                     "₹8,00,001 - ₹12,00,000", "Above ₹12,00,000"], label="Annual Family Income"),
        gr.Number(label="Total Family Members", minimum=1, maximum=15),
        gr.Number(label="Earning Members", minimum=0, maximum=10),
        gr.Dropdown(["Yes", "No"], label="Scholarship Status"),
        gr.Dropdown(["Yes", "No"], label="Education Loan Status"),
        gr.Dropdown(["Below 50%", "50% - 59.9%", "60% - 69.9%", "70% - 79.9%", 
                     "80% - 89.9%", "90% and above"], label="Class 10 Percentage"),
        gr.Dropdown(["Below 50%", "50% - 59.9%", "60% - 69.9%", "70% - 79.9%", 
                     "80% - 89.9%", "90% and above"], label="Class 12 Percentage"),
        gr.Number(label="Current CGPA", minimum=0, maximum=10, step=0.01),
        gr.Number(label="Current Backlogs", minimum=0, maximum=10),
        gr.Number(label="Year Backs", minimum=0, maximum=5)
    ],
    outputs=gr.JSON(label="Prediction Result"),
    title="SETU Student At-Risk Prediction",
    description="AI-powered student dropout risk assessment system with multiple ML models",
    api_name="predict"
)  # ← fixed: closing parenthesis present

if __name__ == "__main__":
    iface.launch()