Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
REPO_ID = "DetectiveShadow/Grade_predictor"
|
| 8 |
+
|
| 9 |
+
# Load pipeline and optional schema at startup
|
| 10 |
+
def load_model():
|
| 11 |
+
pipe = joblib.load(hf_hub_download(REPO_ID, "grade_predictor.pkl"))
|
| 12 |
+
schema_path = None
|
| 13 |
+
try:
|
| 14 |
+
schema_path = hf_hub_download(REPO_ID, "feature_schema.json")
|
| 15 |
+
except Exception:
|
| 16 |
+
schema_path = None
|
| 17 |
+
|
| 18 |
+
schema = None
|
| 19 |
+
if schema_path is not None:
|
| 20 |
+
try:
|
| 21 |
+
import json
|
| 22 |
+
with open(schema_path, "r") as f:
|
| 23 |
+
schema = json.load(f)
|
| 24 |
+
except Exception:
|
| 25 |
+
schema = None
|
| 26 |
+
return pipe, schema
|
| 27 |
+
|
| 28 |
+
PIPE, SCHEMA = load_model()
|
| 29 |
+
|
| 30 |
+
# Defaults & choices (adjust if your training data uses different labels)
|
| 31 |
+
SUBJECTS = ["Math","Science","English","History","Tech","Drama"]
|
| 32 |
+
DIFFICULTY = ["Regular","Honors","AP"]
|
| 33 |
+
GROUPING = ["Solo","Group"]
|
| 34 |
+
|
| 35 |
+
def predict(attendance, hours, quizzes, confidence, grade_level, subject, difficulty, group_or_solo):
|
| 36 |
+
# Prepare a single-row dataframe matching training feature names
|
| 37 |
+
row = pd.DataFrame([{
|
| 38 |
+
"attendance": float(attendance),
|
| 39 |
+
"hours_studied": int(hours),
|
| 40 |
+
"quizzes_avg": int(quizzes),
|
| 41 |
+
"confidence": int(confidence),
|
| 42 |
+
"grade_level": int(grade_level),
|
| 43 |
+
"subject": subject,
|
| 44 |
+
"course_difficulty": difficulty,
|
| 45 |
+
"group_or_solo": group_or_solo
|
| 46 |
+
}])
|
| 47 |
+
pred = float(PIPE.predict(row)[0])
|
| 48 |
+
|
| 49 |
+
# Letter grade helper
|
| 50 |
+
def to_letter(x):
|
| 51 |
+
if x >= 90: return "A"
|
| 52 |
+
if x >= 80: return "B"
|
| 53 |
+
if x >= 70: return "C"
|
| 54 |
+
if x >= 60: return "D"
|
| 55 |
+
return "F"
|
| 56 |
+
|
| 57 |
+
letter = to_letter(pred)
|
| 58 |
+
return {"Predicted Final Grade": round(pred, 1), "Letter": letter}
|
| 59 |
+
|
| 60 |
+
with gr.Blocks(title="Grade Predictor") as demo:
|
| 61 |
+
gr.Markdown("# 🎯 Grade Predictor")
|
| 62 |
+
gr.Markdown("Enter your study & course details to estimate your final grade.")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
attendance = gr.Slider(0.5, 1.0, value=0.95, step=0.01, label="Attendance (0–1)")
|
| 66 |
+
hours = gr.Slider(0, 30, value=12, step=1, label="Hours studied / week")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
quizzes = gr.Slider(0, 100, value=85, step=1, label="Quizzes average")
|
| 69 |
+
confidence = gr.Slider(0, 10, value=7, step=1, label="Confidence (0–10)")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
grade_lvl = gr.Slider(9, 12, value=11, step=1, label="Grade level")
|
| 72 |
+
subject = gr.Dropdown(SUBJECTS, value="Math", label="Subject", allow_custom_value=True)
|
| 73 |
+
with gr.Row():
|
| 74 |
+
difficulty = gr.Dropdown(DIFFICULTY, value="Regular", label="Course difficulty", allow_custom_value=True)
|
| 75 |
+
grouping = gr.Dropdown(GROUPING, value="Solo", label="Group or Solo", allow_custom_value=True)
|
| 76 |
+
|
| 77 |
+
go = gr.Button("Predict")
|
| 78 |
+
out = gr.JSON(label="Prediction")
|
| 79 |
+
|
| 80 |
+
go.click(
|
| 81 |
+
fn=predict,
|
| 82 |
+
inputs=[attendance, hours, quizzes, confidence, grade_lvl, subject, difficulty, grouping],
|
| 83 |
+
outputs=out
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|