ntam0001's picture
Update app.py
35067a8 verified
import gradio as gr
import pandas as pd
import joblib
import os
# Load the trained Naïve Bayes model
model_path = os.path.join(os.getcwd(), "Random Forest_model.pkl")
model = joblib.load(model_path)
# Define the prediction function
def predict_status(age, gender, family_income, attendance, grades, paying_school_feeding, feeding_rate, orphans, parental_education, number_of_siblings):
try:
# Create a dictionary from inputs
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
}
# Convert input to DataFrame
new_data = pd.DataFrame([input_data])
# Encoding categorical variables
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)
# Ensure all values are numeric
if new_data.isnull().values.any():
return "Error: Invalid input values. Please check your inputs."
# Make a prediction
prediction = model.predict(new_data)
# Decode prediction result
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)}"
# Define Gradio interface
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")
# Create Gradio app
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."
)
# Launch the app
app.launch()