File size: 3,392 Bytes
e0948f6
 
 
 
 
 
 
 
 
 
 
 
c497c0a
 
b7719a5
 
 
c497c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0948f6
 
 
c497c0a
 
 
 
 
 
 
 
 
e0948f6
 
 
 
 
 
 
b7719a5
 
 
e0948f6
b7719a5
f75cbfb
b7719a5
e0948f6
 
f75cbfb
b7719a5
db5c2d0
e0948f6
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()