Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,9 @@ models = {
|
|
| 12 |
"Decision Tree": joblib.load('decision_tree_model.joblib'),
|
| 13 |
"XGBoost": joblib.load('xgboost_model.joblib'),
|
| 14 |
"AdaBoost": joblib.load('adaboost_model.joblib')
|
| 15 |
-
}
|
| 16 |
|
| 17 |
# --- BACKGROUND MAPPINGS ---
|
| 18 |
-
# These dictionaries map the "Friendly Text" -> "Model Number"
|
| 19 |
-
# You MUST ensure these numbers match exactly what your LabelEncoder/OneHotEncoder used during training.
|
| 20 |
mappings = {
|
| 21 |
"Gender": {"Male": 1, "Female": 0},
|
| 22 |
"YesNo": {"Yes": 1, "No": 0},
|
|
@@ -35,7 +33,6 @@ mappings = {
|
|
| 35 |
"80% - 89.9%": 4,
|
| 36 |
"90% and above": 5
|
| 37 |
},
|
| 38 |
-
# Add mappings for Social Category and Domicile if your model needs numbers
|
| 39 |
"Social_Category": {"General": 0, "OBC": 1, "SC": 2, "ST": 3, "EWS": 4},
|
| 40 |
"Domicile_Type": {"Urban": 0, "Semi-Urban": 1, "Rural": 2}
|
| 41 |
}
|
|
@@ -47,8 +44,7 @@ def predict_risk(model_name, gender, age, social_category, domicile_type, first_
|
|
| 47 |
# 1. Select the model
|
| 48 |
model = models[model_name]
|
| 49 |
|
| 50 |
-
# 2.
|
| 51 |
-
# We use .get() to safely grab the number. Default to 0 if something goes wrong.
|
| 52 |
encoded_gender = mappings["Gender"].get(gender, 0)
|
| 53 |
encoded_first_gen = mappings["YesNo"].get(first_gen, 0)
|
| 54 |
encoded_disability = mappings["YesNo"].get(disability_status, 0)
|
|
@@ -57,13 +53,9 @@ def predict_risk(model_name, gender, age, social_category, domicile_type, first_
|
|
| 57 |
encoded_income = mappings["Income"].get(income_bracket, 0)
|
| 58 |
encoded_class10 = mappings["Grades"].get(class10, 0)
|
| 59 |
encoded_class12 = mappings["Grades"].get(class12, 0)
|
| 60 |
-
|
| 61 |
-
# Handle the ones that might have caused the "could not convert string" error
|
| 62 |
-
# If your model used OneHotEncoder for these, you might need a different approach,
|
| 63 |
-
# but if it used LabelEncoder, this is perfect.
|
| 64 |
encoded_social = mappings["Social_Category"].get(social_category, 0)
|
| 65 |
encoded_domicile = mappings["Domicile_Type"].get(domicile_type, 0)
|
| 66 |
-
|
| 67 |
# 3. Create the numeric DataFrame
|
| 68 |
input_data = pd.DataFrame({
|
| 69 |
'Gender_bin': [encoded_gender],
|
|
@@ -88,7 +80,7 @@ def predict_risk(model_name, gender, age, social_category, domicile_type, first_
|
|
| 88 |
prediction = model.predict(input_data)[0]
|
| 89 |
result = "At Risk" if prediction >= 2 else "Not At Risk"
|
| 90 |
|
| 91 |
-
# 5.
|
| 92 |
try:
|
| 93 |
probabilities = model.predict_proba(input_data)
|
| 94 |
confidence = float(np.max(probabilities)) * 100
|
|
@@ -102,7 +94,7 @@ def predict_risk(model_name, gender, age, social_category, domicile_type, first_
|
|
| 102 |
"risk_score": float(prediction)
|
| 103 |
}
|
| 104 |
|
| 105 |
-
# Create Gradio interface
|
| 106 |
iface = gr.Interface(
|
| 107 |
fn=predict_risk,
|
| 108 |
inputs=[
|
|
@@ -131,7 +123,7 @@ iface = gr.Interface(
|
|
| 131 |
title="SETU Student At-Risk Prediction",
|
| 132 |
description="AI-powered student dropout risk assessment system with multiple ML models",
|
| 133 |
api_name="predict"
|
| 134 |
-
)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
-
iface.launch()
|
|
|
|
| 12 |
"Decision Tree": joblib.load('decision_tree_model.joblib'),
|
| 13 |
"XGBoost": joblib.load('xgboost_model.joblib'),
|
| 14 |
"AdaBoost": joblib.load('adaboost_model.joblib')
|
| 15 |
+
} # ← This closing brace was MISSING!
|
| 16 |
|
| 17 |
# --- BACKGROUND MAPPINGS ---
|
|
|
|
|
|
|
| 18 |
mappings = {
|
| 19 |
"Gender": {"Male": 1, "Female": 0},
|
| 20 |
"YesNo": {"Yes": 1, "No": 0},
|
|
|
|
| 33 |
"80% - 89.9%": 4,
|
| 34 |
"90% and above": 5
|
| 35 |
},
|
|
|
|
| 36 |
"Social_Category": {"General": 0, "OBC": 1, "SC": 2, "ST": 3, "EWS": 4},
|
| 37 |
"Domicile_Type": {"Urban": 0, "Semi-Urban": 1, "Rural": 2}
|
| 38 |
}
|
|
|
|
| 44 |
# 1. Select the model
|
| 45 |
model = models[model_name]
|
| 46 |
|
| 47 |
+
# 2. Convert text to numbers
|
|
|
|
| 48 |
encoded_gender = mappings["Gender"].get(gender, 0)
|
| 49 |
encoded_first_gen = mappings["YesNo"].get(first_gen, 0)
|
| 50 |
encoded_disability = mappings["YesNo"].get(disability_status, 0)
|
|
|
|
| 53 |
encoded_income = mappings["Income"].get(income_bracket, 0)
|
| 54 |
encoded_class10 = mappings["Grades"].get(class10, 0)
|
| 55 |
encoded_class12 = mappings["Grades"].get(class12, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
encoded_social = mappings["Social_Category"].get(social_category, 0)
|
| 57 |
encoded_domicile = mappings["Domicile_Type"].get(domicile_type, 0)
|
| 58 |
+
|
| 59 |
# 3. Create the numeric DataFrame
|
| 60 |
input_data = pd.DataFrame({
|
| 61 |
'Gender_bin': [encoded_gender],
|
|
|
|
| 80 |
prediction = model.predict(input_data)[0]
|
| 81 |
result = "At Risk" if prediction >= 2 else "Not At Risk"
|
| 82 |
|
| 83 |
+
# 5. Calculate confidence
|
| 84 |
try:
|
| 85 |
probabilities = model.predict_proba(input_data)
|
| 86 |
confidence = float(np.max(probabilities)) * 100
|
|
|
|
| 94 |
"risk_score": float(prediction)
|
| 95 |
}
|
| 96 |
|
| 97 |
+
# Create Gradio interface
|
| 98 |
iface = gr.Interface(
|
| 99 |
fn=predict_risk,
|
| 100 |
inputs=[
|
|
|
|
| 123 |
title="SETU Student At-Risk Prediction",
|
| 124 |
description="AI-powered student dropout risk assessment system with multiple ML models",
|
| 125 |
api_name="predict"
|
| 126 |
+
) # ← This closing parenthesis was MISSING!
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
+
iface.launch()
|