|
import gradio as gr |
|
import pandas as pd |
|
import joblib |
|
import os |
|
|
|
|
|
model_path = os.path.join(os.getcwd(), "Random Forest_model.pkl") |
|
model = joblib.load(model_path) |
|
|
|
|
|
def predict_status(age, gender, family_income, attendance, grades, paying_school_feeding, feeding_rate, orphans, parental_education, number_of_siblings): |
|
try: |
|
|
|
input_data = { |
|
'age': age, |
|
'gender': gender, |
|
'family_income': family_income, |
|
'attendance': attendance, |
|
'grades': grades, |
|
'paying_school_feeding': paying_school_feeding, |
|
'feeding_rate': feeding_rate, |
|
'orphans': orphans, |
|
'parental_education': parental_education, |
|
'number_of_siblings': number_of_siblings |
|
} |
|
|
|
|
|
new_data = pd.DataFrame([input_data]) |
|
|
|
|
|
encoding_maps = { |
|
'gender': {'Male': 0, 'Female': 1}, |
|
'paying_school_feeding': {'Yes': 1, 'No': 0}, |
|
'orphans': {'Yes': 1, 'No': 0}, |
|
'parental_education': {'Not_Ediucated': 0, 'Primary': 1, 'Secondary': 2, 'University': 3}, |
|
} |
|
|
|
for col, mapping in encoding_maps.items(): |
|
if col in new_data: |
|
new_data[col] = new_data[col].map(mapping) |
|
|
|
|
|
if new_data.isnull().values.any(): |
|
return "Error: Invalid input values. Please check your inputs." |
|
|
|
|
|
prediction = model.predict(new_data) |
|
|
|
|
|
status_map = {0: 'Studying', 1: 'Dropout'} |
|
predicted_status = status_map.get(prediction[0], "Unknown") |
|
|
|
return f"Predicted Status: {predicted_status}" |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
inputs = [ |
|
gr.Number(label="Age"), |
|
gr.Dropdown(label="Gender", choices=["Male", "Female"]), |
|
gr.Number(label="Family Income"), |
|
gr.Number(label="Attendance (0-100)"), |
|
gr.Number(label="Grades (0-100)"), |
|
gr.Dropdown(label="Paying School Feeding", choices=["Yes", "No"]), |
|
gr.Number(label="Feeding Rate (0-4)"), |
|
gr.Dropdown(label="Orphans", choices=["Yes", "No"]), |
|
gr.Dropdown(label="Parental Education", choices=["Not_Ediucated", "Primary", "Secondary", "University"]), |
|
gr.Number(label="Number of Siblings") |
|
] |
|
|
|
outputs = gr.Textbox(label="Prediction Result") |
|
|
|
|
|
app = gr.Interface( |
|
fn=predict_status, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Student Dropout Prediction", |
|
description="Predict whether a student will drop out or continue studying based on various features." |
|
) |
|
|
|
|
|
app.launch() |