Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
burtenshaw
commited on
Commit
·
d5f098f
1
Parent(s):
1258179
add max questions and passing grade functionality
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
from datetime import datetime
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
from datasets import load_dataset, Dataset
|
@@ -7,11 +8,18 @@ from huggingface_hub import whoami
|
|
7 |
|
8 |
|
9 |
EXAM_DATASET_ID = os.getenv("EXAM_DATASET_ID") or "agents-course/unit_1_quiz"
|
|
|
|
|
10 |
|
11 |
ds = load_dataset(EXAM_DATASET_ID, split="train")
|
12 |
|
13 |
-
# Convert dataset to a list of dicts
|
14 |
-
quiz_data = ds.to_pandas().to_dict("records")
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
def on_user_logged_in(token: gr.OAuthToken | None):
|
@@ -29,25 +37,39 @@ def on_user_logged_in(token: gr.OAuthToken | None):
|
|
29 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
30 |
"""
|
31 |
Create a new dataset from user_answers and push it to the Hub.
|
32 |
-
|
33 |
-
If no one is logged in, we'll return an error message.
|
34 |
"""
|
35 |
if token is None:
|
36 |
gr.Warning("Please log in to Hugging Face before pushing!")
|
37 |
return
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
user_info = whoami(token=token.token)
|
42 |
-
repo_id = f"{EXAM_DATASET_ID}_student_responses"
|
43 |
submission_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
44 |
|
45 |
new_ds = Dataset.from_list(user_answers)
|
46 |
new_ds = new_ds.map(
|
47 |
-
lambda x: {
|
|
|
|
|
|
|
|
|
48 |
)
|
49 |
new_ds.push_to_hub(repo_id)
|
50 |
-
|
51 |
|
52 |
|
53 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
@@ -83,15 +105,22 @@ def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
|
83 |
|
84 |
# If we've reached the end, show final results
|
85 |
if question_idx >= len(quiz_data):
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
return (
|
88 |
"", # question_text becomes blank
|
89 |
gr.update(choices=[], visible=False),
|
90 |
-
"
|
91 |
question_idx,
|
92 |
user_answers,
|
93 |
start_btn_update,
|
94 |
-
gr.update(value=
|
95 |
)
|
96 |
else:
|
97 |
# Otherwise, show the next question
|
|
|
1 |
import os
|
2 |
from datetime import datetime
|
3 |
+
import random
|
4 |
|
5 |
import gradio as gr
|
6 |
from datasets import load_dataset, Dataset
|
|
|
8 |
|
9 |
|
10 |
EXAM_DATASET_ID = os.getenv("EXAM_DATASET_ID") or "agents-course/unit_1_quiz"
|
11 |
+
EXAM_MAX_QUESTIONS = os.getenv("EXAM_MAX_QUESTIONS") or 10
|
12 |
+
EXAM_PASSING_SCORE = os.getenv("EXAM_PASSING_SCORE") or 0.7
|
13 |
|
14 |
ds = load_dataset(EXAM_DATASET_ID, split="train")
|
15 |
|
16 |
+
# Convert dataset to a list of dicts and randomly sort
|
17 |
+
quiz_data = ds.to_pandas().to_dict("records")
|
18 |
+
random.shuffle(quiz_data)
|
19 |
+
|
20 |
+
# Limit to max questions if specified
|
21 |
+
if EXAM_MAX_QUESTIONS:
|
22 |
+
quiz_data = quiz_data[: int(EXAM_MAX_QUESTIONS)]
|
23 |
|
24 |
|
25 |
def on_user_logged_in(token: gr.OAuthToken | None):
|
|
|
37 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
38 |
"""
|
39 |
Create a new dataset from user_answers and push it to the Hub.
|
40 |
+
Calculates grade and checks against passing threshold.
|
|
|
41 |
"""
|
42 |
if token is None:
|
43 |
gr.Warning("Please log in to Hugging Face before pushing!")
|
44 |
return
|
45 |
+
|
46 |
+
# Calculate grade
|
47 |
+
correct_count = sum(1 for answer in user_answers if answer["is_correct"])
|
48 |
+
total_questions = len(user_answers)
|
49 |
+
grade = correct_count / total_questions if total_questions > 0 else 0
|
50 |
+
|
51 |
+
if grade < float(EXAM_PASSING_SCORE):
|
52 |
+
gr.Warning(
|
53 |
+
f"Score {grade:.1%} below passing threshold of {float(EXAM_PASSING_SCORE):.1%}"
|
54 |
+
)
|
55 |
+
return f"You scored {grade:.1%}. Please try again to achieve at least {float(EXAM_PASSING_SCORE):.1%}"
|
56 |
+
|
57 |
+
gr.Info("Submitting answers to the Hub. Please wait...", duration=2)
|
58 |
|
59 |
user_info = whoami(token=token.token)
|
60 |
+
repo_id = f"{EXAM_DATASET_ID}_student_responses"
|
61 |
submission_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
62 |
|
63 |
new_ds = Dataset.from_list(user_answers)
|
64 |
new_ds = new_ds.map(
|
65 |
+
lambda x: {
|
66 |
+
"username": user_info["name"],
|
67 |
+
"datetime": submission_time,
|
68 |
+
"grade": grade,
|
69 |
+
}
|
70 |
)
|
71 |
new_ds.push_to_hub(repo_id)
|
72 |
+
return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}"
|
73 |
|
74 |
|
75 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
|
|
105 |
|
106 |
# If we've reached the end, show final results
|
107 |
if question_idx >= len(quiz_data):
|
108 |
+
correct_count = sum(1 for answer in user_answers if answer["is_correct"])
|
109 |
+
grade = correct_count / len(user_answers)
|
110 |
+
results_text = (
|
111 |
+
f"**Quiz Complete!**\n\n"
|
112 |
+
f"Your score: {grade:.1%}\n"
|
113 |
+
f"Passing score: {float(EXAM_PASSING_SCORE):.1%}\n\n"
|
114 |
+
f"Your answers:\n\n{user_answers}"
|
115 |
+
)
|
116 |
return (
|
117 |
"", # question_text becomes blank
|
118 |
gr.update(choices=[], visible=False),
|
119 |
+
f"{'✅ Passed!' if grade >= float(EXAM_PASSING_SCORE) else '❌ Did not pass'}",
|
120 |
question_idx,
|
121 |
user_answers,
|
122 |
start_btn_update,
|
123 |
+
gr.update(value=results_text, visible=True), # show final_markdown
|
124 |
)
|
125 |
else:
|
126 |
# Otherwise, show the next question
|