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()