File size: 1,657 Bytes
200a607
 
 
 
 
 
 
 
a3bbcf6
 
 
 
 
200a607
 
 
 
a3bbcf6
200a607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc5586
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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"}