setu / app.py
rtnjt's picture
updated again
7cfef98 verified
Raw History Blame Contribute Delete
5.32 kB
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()