Spaces:
Sleeping
Sleeping
updated app.py again
Browse files
app.py
CHANGED
|
@@ -14,41 +14,86 @@ models = {
|
|
| 14 |
"AdaBoost": joblib.load('adaboost_model.joblib')
|
| 15 |
}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def predict_risk(model_name, gender, age, social_category, domicile_type, first_gen,
|
| 18 |
disability_status, income_bracket, family_members, earning_members,
|
| 19 |
scholarship, loan_status, class10, class12, cgpa, backlogs, year_backs):
|
| 20 |
|
| 21 |
-
# Select the model
|
| 22 |
model = models[model_name]
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
input_data = pd.DataFrame({
|
| 27 |
-
'Gender_bin': [
|
| 28 |
'Age': [age],
|
| 29 |
-
'Social_Category': [
|
| 30 |
-
'Domicile_Type': [
|
| 31 |
-
'First_Gen_bin': [
|
| 32 |
-
'Disability_Status_bin': [
|
| 33 |
-
'Income_ord': [
|
| 34 |
'Total_Family_Members': [family_members],
|
| 35 |
'Earning_Members': [earning_members],
|
| 36 |
-
'Scholarship_Status_bin': [
|
| 37 |
-
'Education_Loan_Status_bin': [
|
| 38 |
-
'Class10_ord': [
|
| 39 |
-
'Class12_ord': [
|
| 40 |
'Current_CGPA': [cgpa],
|
| 41 |
'Current_Backlogs': [backlogs],
|
| 42 |
'Year_Backs': [year_backs]
|
| 43 |
})
|
| 44 |
|
| 45 |
-
#
|
| 46 |
prediction = model.predict(input_data)[0]
|
| 47 |
result = "At Risk" if prediction >= 2 else "Not At Risk"
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
return {
|
| 54 |
"model_used": model_name,
|
|
@@ -57,7 +102,7 @@ def predict_risk(model_name, gender, age, social_category, domicile_type, first_
|
|
| 57 |
"risk_score": float(prediction)
|
| 58 |
}
|
| 59 |
|
| 60 |
-
# Create Gradio interface
|
| 61 |
iface = gr.Interface(
|
| 62 |
fn=predict_risk,
|
| 63 |
inputs=[
|
|
@@ -89,4 +134,4 @@ iface = gr.Interface(
|
|
| 89 |
)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
-
iface.launch()
|
|
|
|
| 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},
|
| 23 |
+
"Income": {
|
| 24 |
+
"Below ₹3,00,000": 0,
|
| 25 |
+
"₹3,00,001 - ₹5,00,000": 1,
|
| 26 |
+
"₹5,00,001 - ₹8,00,000": 2,
|
| 27 |
+
"₹8,00,001 - ₹12,00,000": 3,
|
| 28 |
+
"Above ₹12,00,000": 4
|
| 29 |
+
},
|
| 30 |
+
"Grades": {
|
| 31 |
+
"Below 50%": 0,
|
| 32 |
+
"50% - 59.9%": 1,
|
| 33 |
+
"60% - 69.9%": 2,
|
| 34 |
+
"70% - 79.9%": 3,
|
| 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 |
+
}
|
| 42 |
+
|
| 43 |
def predict_risk(model_name, gender, age, social_category, domicile_type, first_gen,
|
| 44 |
disability_status, income_bracket, family_members, earning_members,
|
| 45 |
scholarship, loan_status, class10, class12, cgpa, backlogs, year_backs):
|
| 46 |
|
| 47 |
+
# 1. Select the model
|
| 48 |
model = models[model_name]
|
| 49 |
|
| 50 |
+
# 2. SILENT CONVERSION (Text -> Number)
|
| 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)
|
| 55 |
+
encoded_scholarship = mappings["YesNo"].get(scholarship, 0)
|
| 56 |
+
encoded_loan = mappings["YesNo"].get(loan_status, 0)
|
| 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],
|
| 70 |
'Age': [age],
|
| 71 |
+
'Social_Category': [encoded_social],
|
| 72 |
+
'Domicile_Type': [encoded_domicile],
|
| 73 |
+
'First_Gen_bin': [encoded_first_gen],
|
| 74 |
+
'Disability_Status_bin': [encoded_disability],
|
| 75 |
+
'Income_ord': [encoded_income],
|
| 76 |
'Total_Family_Members': [family_members],
|
| 77 |
'Earning_Members': [earning_members],
|
| 78 |
+
'Scholarship_Status_bin': [encoded_scholarship],
|
| 79 |
+
'Education_Loan_Status_bin': [encoded_loan],
|
| 80 |
+
'Class10_ord': [encoded_class10],
|
| 81 |
+
'Class12_ord': [encoded_class12],
|
| 82 |
'Current_CGPA': [cgpa],
|
| 83 |
'Current_Backlogs': [backlogs],
|
| 84 |
'Year_Backs': [year_backs]
|
| 85 |
})
|
| 86 |
|
| 87 |
+
# 4. Predict
|
| 88 |
prediction = model.predict(input_data)[0]
|
| 89 |
result = "At Risk" if prediction >= 2 else "Not At Risk"
|
| 90 |
|
| 91 |
+
# 5. Confidence
|
| 92 |
+
try:
|
| 93 |
+
probabilities = model.predict_proba(input_data)
|
| 94 |
+
confidence = float(np.max(probabilities)) * 100
|
| 95 |
+
except:
|
| 96 |
+
confidence = 85.0
|
| 97 |
|
| 98 |
return {
|
| 99 |
"model_used": model_name,
|
|
|
|
| 102 |
"risk_score": float(prediction)
|
| 103 |
}
|
| 104 |
|
| 105 |
+
# Create Gradio interface (Users see Text, Code sees Numbers)
|
| 106 |
iface = gr.Interface(
|
| 107 |
fn=predict_risk,
|
| 108 |
inputs=[
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
iface.launch()
|