from fastapi import FastAPI from pydantic import BaseModel import pickle import pandas as pd from sklearn.preprocessing import StandardScaler app = FastAPI() # Root route for Hugging Face health check @app.get("/") def read_root(): return {"message": "Dropout prediction API is running!"} # Load the model with open('random_forest_model.pkl', 'rb') as model_file: model = pickle.load(model_file) # Dummy scaler initialization (adjust with real stats if available) scaler = StandardScaler() scaler.mean_ = [0.5, 0.5, 2.5, 2.5, 20, 50] # Example means scaler.scale_ = [0.5, 0.5, 1, 1, 10, 20] # Example stds # Pydantic model for request body class StudentInfo(BaseModel): tuition: float scholarship: float gpa1: float gpa2: float age: float attendance: float @app.post("/predict") def predict_dropout_reason(student_info: StudentInfo): input_data = [[ student_info.tuition, student_info.scholarship, student_info.gpa1, student_info.gpa2, student_info.age, student_info.attendance ]] input_scaled = scaler.transform(input_data) predicted_class = model.predict(input_scaled)[0] reasons = [] if student_info.attendance < 80: reasons.append("Attendance is below 80%") if student_info.tuition == 0: reasons.append("Tuition fees not paid") if student_info.gpa1 < 2 and student_info.gpa2 < 2: reasons.append("GPA in both semesters is below 2.0") if reasons: return {"dropout_risk": True, "reasons": reasons} else: return {"dropout_risk": False, "message": "Student is likely to continue"}