Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from fastapi import FastAPI, Request, Form
|
| 5 |
+
from fastapi.responses import HTMLResponse
|
| 6 |
+
from fastapi.templating import Jinja2Templates
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
# Templates folder
|
| 10 |
+
templates = Jinja2Templates(directory="templates")
|
| 11 |
+
|
| 12 |
+
# Load trained model (ensure student_performance_model.pkl is in /app)
|
| 13 |
+
MODEL_PATH = os.getenv('MODEL_PATH', 'student_performance_model.pkl')
|
| 14 |
+
with open(MODEL_PATH, 'rb') as f:
|
| 15 |
+
model = pickle.load(f)
|
| 16 |
+
|
| 17 |
+
@app.get('/', response_class=HTMLResponse)
|
| 18 |
+
async def read_form(request: Request):
|
| 19 |
+
return templates.TemplateResponse('index.html', {
|
| 20 |
+
'request': request,
|
| 21 |
+
'predicted': None
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
@app.post('/predict', response_class=HTMLResponse)
|
| 25 |
+
async def predict(request: Request,
|
| 26 |
+
Age: int = Form(...),
|
| 27 |
+
Gender: str = Form(...),
|
| 28 |
+
HoursOfStudyPerDay: float = Form(...),
|
| 29 |
+
SchoolAttendanceRate: float = Form(...),
|
| 30 |
+
TuitionAccess: str = Form(...),
|
| 31 |
+
AveragePreviousScores: float = Form(...),
|
| 32 |
+
HoursOfSleep: float = Form(...),
|
| 33 |
+
BreakfastDaily: str = Form(...),
|
| 34 |
+
ScreenTimeHours: float = Form(...),
|
| 35 |
+
PhysicalActivityHours: float = Form(...),
|
| 36 |
+
PlaysSport: str = Form(...),
|
| 37 |
+
MentalHealthScore: float = Form(...),
|
| 38 |
+
ParentalEducationLevel: str = Form(...),
|
| 39 |
+
HouseholdIncomeLevel: str = Form(...),
|
| 40 |
+
StudyEnvironmentRating: float = Form(...),
|
| 41 |
+
FriendSupportScore: float = Form(...),
|
| 42 |
+
ParticipatesInClubs: str = Form(...),
|
| 43 |
+
PartTimeWork: str = Form(...)):
|
| 44 |
+
# Organize input into DataFrame
|
| 45 |
+
data = pd.DataFrame([{
|
| 46 |
+
'Age': Age,
|
| 47 |
+
'Gender': Gender,
|
| 48 |
+
'HoursOfStudyPerDay': HoursOfStudyPerDay,
|
| 49 |
+
'SchoolAttendanceRate': SchoolAttendanceRate,
|
| 50 |
+
'TuitionAccess': TuitionAccess,
|
| 51 |
+
'AveragePreviousScores': AveragePreviousScores,
|
| 52 |
+
'HoursOfSleep': HoursOfSleep,
|
| 53 |
+
'BreakfastDaily': BreakfastDaily,
|
| 54 |
+
'ScreenTimeHours': ScreenTimeHours,
|
| 55 |
+
'PhysicalActivityHours': PhysicalActivityHours,
|
| 56 |
+
'PlaysSport': PlaysSport,
|
| 57 |
+
'MentalHealthScore': MentalHealthScore,
|
| 58 |
+
'ParentalEducationLevel': ParentalEducationLevel,
|
| 59 |
+
'HouseholdIncomeLevel': HouseholdIncomeLevel,
|
| 60 |
+
'StudyEnvironmentRating': StudyEnvironmentRating,
|
| 61 |
+
'FriendSupportScore': FriendSupportScore,
|
| 62 |
+
'ParticipatesInClubs': ParticipatesInClubs,
|
| 63 |
+
'PartTimeWork': PartTimeWork
|
| 64 |
+
}])
|
| 65 |
+
# Predict
|
| 66 |
+
pred = model.predict(data)[0]
|
| 67 |
+
return templates.TemplateResponse('index.html', {
|
| 68 |
+
'request': request,
|
| 69 |
+
'predicted': round(pred, 2)
|
| 70 |
+
})
|