-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (50 loc) · 1.57 KB
/
main.py
File metadata and controls
60 lines (50 loc) · 1.57 KB
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
# Save this as main.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import pandas as pd
app = FastAPI()
class StudentData(BaseModel):
code_module: str
code_presentation: str
gender: str
region: str
highest_education: str
age_band: str
num_of_prev_attempts: int
Student_sum_click: int
disability: str
# Load the model
try:
model = joblib.load('student_performance_model.joblib')
except FileNotFoundError:
raise HTTPException(status_code=500, detail="Model file not found. Please ensure the model is trained and saved correctly.")
@app.post("/predict")
def predict(data: StudentData):
# Convert input data to DataFrame
input_df = pd.DataFrame([data.dict()])
# Make prediction
try:
prediction = model.predict(input_df)
return {"predicted_performance": prediction[0]}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
@app.get("/model_info")
def model_info():
return {
"model_type": "Random Forest Classifier",
"best_parameters": {
"max_depth": 10,
"min_samples_leaf": 4,
"min_samples_split": 2,
"n_estimators": 300
},
"accuracy": 0.4333,
"supported_classes": ["Distinction", "Fail", "Pass", "Withdrawn"]
}
@app.get("/")
def read_root():
return {"status": "ok", "message": "Student Performance Prediction API is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)