Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,21 +9,41 @@ def load_model():
|
|
| 9 |
raise FileNotFoundError("student_model.pkl not found. Upload or run train.py first.")
|
| 10 |
with open(MODEL_PATH, "rb") as f:
|
| 11 |
bundle = pickle.load(f)
|
| 12 |
-
return bundle["model"], bundle["features"], bundle["targets"]
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def predict_fn(attendance, study_hours, parent_support, sleep_hours, reading_hours, behavior_score, pretest_score, homework_completion, participation):
|
| 17 |
row = pd.DataFrame([{
|
| 18 |
-
"Attendance": attendance,
|
| 19 |
-
"StudyHours": study_hours,
|
| 20 |
-
"ParentalSupport": parent_support,
|
| 21 |
-
"SleepHours": sleep_hours,
|
| 22 |
-
"ReadingHours": reading_hours,
|
| 23 |
-
"BehaviorScore": behavior_score,
|
| 24 |
-
"PretestScore": pretest_score,
|
| 25 |
-
"HomeworkCompletion": homework_completion,
|
| 26 |
-
"Participation": participation
|
| 27 |
}])
|
| 28 |
y_pred = model.predict(row)[0]
|
| 29 |
return {TARGET_COLS[0]: float(y_pred[0]), TARGET_COLS[1]: float(y_pred[1])}
|
|
@@ -43,7 +63,6 @@ with gr.Blocks() as iface:
|
|
| 43 |
homework_completion = gr.Slider(0, 100, value=85, step=1, label="Homework Completion (%)")
|
| 44 |
participation = gr.Slider(1, 10, value=6, step=1, label="Participation (1-10)")
|
| 45 |
|
| 46 |
-
|
| 47 |
out = gr.JSON(label="Predicted Scores")
|
| 48 |
gr.Button("Predict").click(
|
| 49 |
predict_fn,
|
|
|
|
| 9 |
raise FileNotFoundError("student_model.pkl not found. Upload or run train.py first.")
|
| 10 |
with open(MODEL_PATH, "rb") as f:
|
| 11 |
bundle = pickle.load(f)
|
|
|
|
| 12 |
|
| 13 |
+
# Backward-compatible defaults if old pickle didn't include bounds
|
| 14 |
+
feature_mins = bundle.get("feature_mins", {
|
| 15 |
+
"Attendance": 70, "StudyHours": 1, "ParentalSupport": 1, "SleepHours": 5,
|
| 16 |
+
"ReadingHours": 0, "BehaviorScore": 1, "PretestScore": 0,
|
| 17 |
+
"HomeworkCompletion": 0, "Participation": 1
|
| 18 |
+
})
|
| 19 |
+
feature_maxs = bundle.get("feature_maxs", {
|
| 20 |
+
"Attendance": 100, "StudyHours": 20, "ParentalSupport": 5, "SleepHours": 12,
|
| 21 |
+
"ReadingHours": 20, "BehaviorScore": 10, "PretestScore": 100,
|
| 22 |
+
"HomeworkCompletion": 100, "Participation": 10
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
return bundle["model"], bundle["features"], bundle["targets"], feature_mins, feature_maxs
|
| 26 |
+
|
| 27 |
+
model, FEATURE_COLS, TARGET_COLS, FEATURE_MINS, FEATURE_MAXS = load_model()
|
| 28 |
+
|
| 29 |
+
def _clip(name, val):
|
| 30 |
+
lo = FEATURE_MINS.get(name, None)
|
| 31 |
+
hi = FEATURE_MAXS.get(name, None)
|
| 32 |
+
if lo is not None and val < lo: val = lo
|
| 33 |
+
if hi is not None and val > hi: val = hi
|
| 34 |
+
return val
|
| 35 |
|
| 36 |
def predict_fn(attendance, study_hours, parent_support, sleep_hours, reading_hours, behavior_score, pretest_score, homework_completion, participation):
|
| 37 |
row = pd.DataFrame([{
|
| 38 |
+
"Attendance": _clip("Attendance", attendance),
|
| 39 |
+
"StudyHours": _clip("StudyHours", study_hours),
|
| 40 |
+
"ParentalSupport": _clip("ParentalSupport", parent_support),
|
| 41 |
+
"SleepHours": _clip("SleepHours", sleep_hours),
|
| 42 |
+
"ReadingHours": _clip("ReadingHours", reading_hours),
|
| 43 |
+
"BehaviorScore": _clip("BehaviorScore", behavior_score),
|
| 44 |
+
"PretestScore": _clip("PretestScore", pretest_score),
|
| 45 |
+
"HomeworkCompletion": _clip("HomeworkCompletion", homework_completion),
|
| 46 |
+
"Participation": _clip("Participation", participation)
|
| 47 |
}])
|
| 48 |
y_pred = model.predict(row)[0]
|
| 49 |
return {TARGET_COLS[0]: float(y_pred[0]), TARGET_COLS[1]: float(y_pred[1])}
|
|
|
|
| 63 |
homework_completion = gr.Slider(0, 100, value=85, step=1, label="Homework Completion (%)")
|
| 64 |
participation = gr.Slider(1, 10, value=6, step=1, label="Participation (1-10)")
|
| 65 |
|
|
|
|
| 66 |
out = gr.JSON(label="Predicted Scores")
|
| 67 |
gr.Button("Predict").click(
|
| 68 |
predict_fn,
|