Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import pickle, os | |
| MODEL_PATH = "student_model.pkl" | |
| def load_model(): | |
| if not os.path.exists(MODEL_PATH): | |
| raise FileNotFoundError("student_model.pkl not found. Upload or run train.py first.") | |
| with open(MODEL_PATH, "rb") as f: | |
| bundle = pickle.load(f) | |
| # Backward-compatible defaults if old pickle didn't include bounds | |
| feature_mins = bundle.get("feature_mins", { | |
| "Attendance": 0, "StudyHours": 0, "ParentalSupport": 0, "SleepHours": 0, | |
| "ReadingHours": 0, "BehaviorScore": 0, "PretestScore": 0, | |
| "HomeworkCompletion": 0, "Participation": 0 | |
| }) | |
| feature_maxs = bundle.get("feature_maxs", { | |
| "Attendance": 100, "StudyHours": 20, "ParentalSupport": 5, "SleepHours": 12, | |
| "ReadingHours": 20, "BehaviorScore": 10, "PretestScore": 100, | |
| "HomeworkCompletion": 100, "Participation": 10 | |
| }) | |
| return bundle["model"], bundle["features"], bundle["targets"], feature_mins, feature_maxs | |
| model, FEATURE_COLS, TARGET_COLS, FEATURE_MINS, FEATURE_MAXS = load_model() | |
| def _clip(name, val): | |
| lo = FEATURE_MINS.get(name, None) | |
| hi = FEATURE_MAXS.get(name, None) | |
| if lo is not None and val < lo: val = lo | |
| if hi is not None and val > hi: val = hi | |
| return val | |
| def predict_fn(attendance, study_hours, parent_support, sleep_hours, reading_hours, behavior_score, pretest_score, homework_completion, participation): | |
| row = pd.DataFrame([{ | |
| "Attendance": _clip("Attendance", attendance), | |
| "StudyHours": _clip("StudyHours", study_hours), | |
| "ParentalSupport": _clip("ParentalSupport", parent_support), | |
| "SleepHours": _clip("SleepHours", sleep_hours), | |
| "ReadingHours": _clip("ReadingHours", reading_hours), | |
| "BehaviorScore": _clip("BehaviorScore", behavior_score), | |
| "PretestScore": _clip("PretestScore", pretest_score), | |
| "HomeworkCompletion": _clip("HomeworkCompletion", homework_completion), | |
| "Participation": _clip("Participation", participation) | |
| }]) | |
| y_pred = model.predict(row)[0] | |
| return {TARGET_COLS[0]: float(y_pred[0]), TARGET_COLS[1]: float(y_pred[1])} | |
| with gr.Blocks() as iface: | |
| gr.Markdown("# Student Score Predictor (Pickle Model)") | |
| with gr.Row(): | |
| attendance = gr.Slider(0, 100, value=90, step=1, label="Attendance (%)") | |
| study_hours = gr.Slider(0, 20, value=5, step=1, label="Study Hours / week") | |
| parent_support = gr.Slider(0, 5, value=3, step=1, label="Parental Support (1-5)") | |
| with gr.Row(): | |
| sleep_hours = gr.Slider(0, 12, value=8, step=1, label="Sleep Hours / night") | |
| reading_hours = gr.Slider(0, 20, value=2, step=1, label="Reading Hours / week") | |
| behavior_score = gr.Slider(0, 10, value=7, step=1, label="Behavior Score (1-10)") | |
| with gr.Row(): | |
| pretest_score = gr.Slider(0, 100, value=70, step=1, label="Pretest Score") | |
| homework_completion = gr.Slider(0, 100, value=85, step=1, label="Homework Completion (%)") | |
| participation = gr.Slider(0, 10, value=6, step=1, label="Participation (1-10)") | |
| out = gr.JSON(label="Predicted Scores") | |
| gr.Button("Predict").click( | |
| predict_fn, | |
| [attendance, study_hours, parent_support, sleep_hours, reading_hours, behavior_score, pretest_score, homework_completion, participation], | |
| out | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |