File size: 5,388 Bytes
87a0817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import joblib
import pandas as pd
import numpy as np

# 1. Load preprocessing tools
preprocessor = joblib.load('preprocessor.joblib')
label_encoder = joblib.load('label_encoder.joblib')

# 2. 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')
}

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,
    stress, burnout, depression
):
    """
    Takes raw input values, applies the same preprocessing as the notebook,
    and returns the predicted risk label together with the model's confidence.
    """
    # 3. Build a DataFrame with exactly the column names used during training.
    #    'Sl. no' is irrelevant, set to a dummy value.
    input_data = pd.DataFrame({
        'Sl. no': [0],
        'Gender': [gender],
        'Age in years': [age],
        'Social Category': [social_category],
        'Domicile Type': [domicile_type],
        'Are you a First-Generation College Student': [first_gen],
        'Disability Status': [disability_status],
        'Annual Family Income Bracket': [income_bracket],
        'Total Family Members': [family_members],
        'Earning Members in Family Count': [earning_members],
        'Scholarship / Fee-Waiver Status': [scholarship],
        'Do you have an Education Loan?': [loan_status],
        '10th Board Percentage/Grade Equivalent': [class10],
        '12th Board / Diploma Percentage / Grade Equivalent': [class12],
        'Current Cumulative Grade Point Average (CGPA)': [cgpa],
        'Current Backlog Count': [backlogs],
        'Stress and Anxiety Levels': [stress],
        'Burnout (Mental Exhaustion)': [burnout],
        'Depression/ Emotional Well being': [depression]
    })

    # 4. Preprocess (scale, encode) exactly as in training
    X_preprocessed = preprocessor.transform(input_data)

    # 5. Predict
    model = models[model_name]
    y_encoded = model.predict(X_preprocessed)[0]          # 0 or 1
    label = label_encoder.inverse_transform([y_encoded])[0]  # 'at-risk' or 'not at-risk'

    result = "At Risk" if label == "at-risk" else "Not At Risk"

    # 6. Confidence (probability of the predicted class)
    try:
        proba = model.predict_proba(X_preprocessed)[0]
        # index of the predicted class
        confidence = float(proba[y_encoded]) * 100
    except AttributeError:
        confidence = 85.0   # fallback if model doesn't support predict_proba

    return {
        "model_used": model_name,
        "prediction": result,
        "confidence": round(confidence, 2)
    }

# 7. Build the Gradio interface with correct dropdown choices
#    (make sure these match the actual values in your training dataset)
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 ₹1,00,000",
                "₹1,00,000 - ₹3,00,000",
                "₹3,00,001 - ₹5,00,000",
                "₹5,00,001 - ₹8,00,000",
                "₹8,00,001 - ₹12,00,000",
                "₹12,00,001 - ₹15,00,000",
                "Above ₹15,00,000"
            ],
            label="Annual Family Income Bracket"
        ),
        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="Stress and Anxiety Levels", minimum=1, maximum=5),
        gr.Number(label="Burnout (Mental Exhaustion)", minimum=1, maximum=5),
        gr.Number(label="Depression/ Emotional Well being", minimum=1, 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"
)

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