Spaces:
Running
Running
burtenshaw
commited on
Commit
·
18fb6c3
1
Parent(s):
6d5550e
update logic flow of app to include start button
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import gradio as gr
|
|
| 6 |
from datasets import load_dataset, Dataset
|
| 7 |
from huggingface_hub import whoami
|
| 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
|
|
@@ -24,14 +23,37 @@ if EXAM_MAX_QUESTIONS:
|
|
| 24 |
|
| 25 |
def on_user_logged_in(token: gr.OAuthToken | None):
|
| 26 |
"""
|
| 27 |
-
If the user has a valid token,
|
| 28 |
-
Otherwise, keep the login button visible
|
| 29 |
"""
|
| 30 |
if token is not None:
|
| 31 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
|
@@ -74,36 +96,24 @@ def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
|
| 74 |
|
| 75 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
| 76 |
"""
|
| 77 |
-
|
| 78 |
-
- If is_start=True, skip storing an answer and show the first question.
|
| 79 |
-
- Otherwise, store the last answer and move on.
|
| 80 |
-
- If we've reached the end, display results.
|
| 81 |
"""
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
{
|
| 97 |
-
"question": current_q["question"],
|
| 98 |
-
"selected_answer": selected_answer,
|
| 99 |
-
"correct_answer": current_q[correct_reference],
|
| 100 |
-
"is_correct": is_correct,
|
| 101 |
-
"correct_reference": correct_reference,
|
| 102 |
-
}
|
| 103 |
-
)
|
| 104 |
question_idx += 1
|
| 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)
|
|
@@ -112,37 +122,35 @@ def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
|
| 112 |
f"Your score: {grade:.1%}\n"
|
| 113 |
f"Passing score: {float(EXAM_PASSING_SCORE):.1%}\n\n"
|
| 114 |
)
|
| 115 |
-
return
|
| 116 |
-
"", # question_text
|
| 117 |
-
gr.update(choices=[], visible=False),
|
| 118 |
f"{'✅ Passed!' if grade >= float(EXAM_PASSING_SCORE) else '❌ Did not pass'}",
|
| 119 |
question_idx,
|
| 120 |
user_answers,
|
| 121 |
-
|
| 122 |
-
gr.update(
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
gr.update(visible=False), # Hide final_markdown for now
|
| 145 |
-
)
|
| 146 |
|
| 147 |
|
| 148 |
def success_message(response):
|
|
@@ -152,47 +160,58 @@ def success_message(response):
|
|
| 152 |
|
| 153 |
with gr.Blocks() as demo:
|
| 154 |
demo.title = f"Dataset Quiz for {EXAM_DATASET_ID}"
|
|
|
|
| 155 |
# State variables
|
| 156 |
question_idx = gr.State(value=0)
|
| 157 |
user_answers = gr.State(value=[])
|
|
|
|
| 158 |
|
| 159 |
with gr.Row(variant="compact"):
|
| 160 |
gr.Markdown(f"## Welcome to the {EXAM_DATASET_ID} Quiz")
|
|
|
|
| 161 |
with gr.Row(variant="compact"):
|
| 162 |
gr.Markdown(
|
| 163 |
"Log in first, then click 'Start' to begin. Answer each question, click 'Next', and finally click 'Submit' to publish your results to the Hugging Face Hub."
|
| 164 |
)
|
| 165 |
-
|
| 166 |
-
with gr.Row(
|
| 167 |
-
variant="panel",
|
| 168 |
-
):
|
| 169 |
question_text = gr.Markdown("")
|
| 170 |
radio_choices = gr.Radio(
|
| 171 |
-
choices=[],
|
| 172 |
)
|
| 173 |
|
| 174 |
with gr.Row(variant="compact"):
|
| 175 |
status_text = gr.Markdown("")
|
|
|
|
| 176 |
|
| 177 |
with gr.Row(variant="compact"):
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
# Click "Start" => show first question, hide Start button
|
| 193 |
start_btn.click(
|
| 194 |
fn=handle_quiz,
|
| 195 |
-
inputs=[question_idx, user_answers,
|
| 196 |
outputs=[
|
| 197 |
question_text,
|
| 198 |
radio_choices,
|
|
@@ -200,11 +219,12 @@ with gr.Blocks() as demo:
|
|
| 200 |
question_idx,
|
| 201 |
user_answers,
|
| 202 |
start_btn,
|
|
|
|
|
|
|
| 203 |
final_markdown,
|
| 204 |
],
|
| 205 |
)
|
| 206 |
|
| 207 |
-
# Click "Next" => store selection, move on
|
| 208 |
next_btn.click(
|
| 209 |
fn=handle_quiz,
|
| 210 |
inputs=[question_idx, user_answers, radio_choices, gr.State(False)],
|
|
@@ -215,13 +235,14 @@ with gr.Blocks() as demo:
|
|
| 215 |
question_idx,
|
| 216 |
user_answers,
|
| 217 |
start_btn,
|
|
|
|
|
|
|
| 218 |
final_markdown,
|
| 219 |
],
|
| 220 |
)
|
| 221 |
|
| 222 |
submit_btn.click(fn=push_results_to_hub, inputs=[user_answers])
|
| 223 |
|
| 224 |
-
|
| 225 |
if __name__ == "__main__":
|
| 226 |
# Note: If testing locally, you'll need to run `huggingface-cli login` or set HF_TOKEN
|
| 227 |
# environment variable for the login to work locally.
|
|
|
|
| 6 |
from datasets import load_dataset, Dataset
|
| 7 |
from huggingface_hub import whoami
|
| 8 |
|
|
|
|
| 9 |
EXAM_DATASET_ID = os.getenv("EXAM_DATASET_ID") or "agents-course/unit_1_quiz"
|
| 10 |
EXAM_MAX_QUESTIONS = os.getenv("EXAM_MAX_QUESTIONS") or 10
|
| 11 |
EXAM_PASSING_SCORE = os.getenv("EXAM_PASSING_SCORE") or 0.7
|
|
|
|
| 23 |
|
| 24 |
def on_user_logged_in(token: gr.OAuthToken | None):
|
| 25 |
"""
|
| 26 |
+
If the user has a valid token, show Start button.
|
| 27 |
+
Otherwise, keep the login button visible.
|
| 28 |
"""
|
| 29 |
if token is not None:
|
| 30 |
+
return [
|
| 31 |
+
gr.update(visible=False), # login button visibility
|
| 32 |
+
gr.update(visible=True), # start button visibility
|
| 33 |
+
gr.update(visible=False), # next button visibility
|
| 34 |
+
gr.update(visible=False), # submit button visibility
|
| 35 |
+
"", # question text
|
| 36 |
+
[], # radio choices (empty list = no choices)
|
| 37 |
+
"Click 'Start' to begin the quiz", # status message
|
| 38 |
+
0, # question_idx
|
| 39 |
+
[], # user_answers
|
| 40 |
+
"", # final_markdown content
|
| 41 |
+
token, # user token
|
| 42 |
+
]
|
| 43 |
else:
|
| 44 |
+
return [
|
| 45 |
+
gr.update(visible=True), # login button visibility
|
| 46 |
+
gr.update(visible=False), # start button visibility
|
| 47 |
+
gr.update(visible=False), # next button visibility
|
| 48 |
+
gr.update(visible=False), # submit button visibility
|
| 49 |
+
"", # question text
|
| 50 |
+
[], # radio choices
|
| 51 |
+
"", # status message
|
| 52 |
+
0, # question_idx
|
| 53 |
+
[], # user_answers
|
| 54 |
+
"", # final_markdown content
|
| 55 |
+
None, # no token
|
| 56 |
+
]
|
| 57 |
|
| 58 |
|
| 59 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
|
|
|
| 96 |
|
| 97 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
| 98 |
"""
|
| 99 |
+
Handle quiz state transitions and store answers
|
|
|
|
|
|
|
|
|
|
| 100 |
"""
|
| 101 |
+
if not is_start and question_idx < len(quiz_data):
|
| 102 |
+
current_q = quiz_data[question_idx]
|
| 103 |
+
correct_reference = current_q["correct_answer"]
|
| 104 |
+
correct_reference = f"answer_{correct_reference}".lower()
|
| 105 |
+
is_correct = selected_answer == current_q[correct_reference]
|
| 106 |
+
user_answers.append(
|
| 107 |
+
{
|
| 108 |
+
"question": current_q["question"],
|
| 109 |
+
"selected_answer": selected_answer,
|
| 110 |
+
"correct_answer": current_q[correct_reference],
|
| 111 |
+
"is_correct": is_correct,
|
| 112 |
+
"correct_reference": correct_reference,
|
| 113 |
+
}
|
| 114 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
question_idx += 1
|
| 116 |
|
|
|
|
| 117 |
if question_idx >= len(quiz_data):
|
| 118 |
correct_count = sum(1 for answer in user_answers if answer["is_correct"])
|
| 119 |
grade = correct_count / len(user_answers)
|
|
|
|
| 122 |
f"Your score: {grade:.1%}\n"
|
| 123 |
f"Passing score: {float(EXAM_PASSING_SCORE):.1%}\n\n"
|
| 124 |
)
|
| 125 |
+
return [
|
| 126 |
+
"", # question_text
|
| 127 |
+
gr.update(choices=[], visible=False), # hide radio choices
|
| 128 |
f"{'✅ Passed!' if grade >= float(EXAM_PASSING_SCORE) else '❌ Did not pass'}",
|
| 129 |
question_idx,
|
| 130 |
user_answers,
|
| 131 |
+
gr.update(visible=False), # start button visibility
|
| 132 |
+
gr.update(visible=False), # next button visibility
|
| 133 |
+
gr.update(visible=True), # submit button visibility
|
| 134 |
+
results_text, # final results text
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
# Show next question
|
| 138 |
+
q = quiz_data[question_idx]
|
| 139 |
+
return [
|
| 140 |
+
f"## Question {question_idx + 1} \n### {q['question']}", # question text
|
| 141 |
+
gr.update( # properly update radio choices
|
| 142 |
+
choices=[q["answer_a"], q["answer_b"], q["answer_c"], q["answer_d"]],
|
| 143 |
+
value=None,
|
| 144 |
+
visible=True,
|
| 145 |
+
),
|
| 146 |
+
"Select an answer and click 'Next' to continue.",
|
| 147 |
+
question_idx,
|
| 148 |
+
user_answers,
|
| 149 |
+
gr.update(visible=False), # start button visibility
|
| 150 |
+
gr.update(visible=True), # next button visibility
|
| 151 |
+
gr.update(visible=False), # submit button visibility
|
| 152 |
+
"", # clear final markdown
|
| 153 |
+
]
|
|
|
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
def success_message(response):
|
|
|
|
| 160 |
|
| 161 |
with gr.Blocks() as demo:
|
| 162 |
demo.title = f"Dataset Quiz for {EXAM_DATASET_ID}"
|
| 163 |
+
|
| 164 |
# State variables
|
| 165 |
question_idx = gr.State(value=0)
|
| 166 |
user_answers = gr.State(value=[])
|
| 167 |
+
user_token = gr.State(value=None)
|
| 168 |
|
| 169 |
with gr.Row(variant="compact"):
|
| 170 |
gr.Markdown(f"## Welcome to the {EXAM_DATASET_ID} Quiz")
|
| 171 |
+
|
| 172 |
with gr.Row(variant="compact"):
|
| 173 |
gr.Markdown(
|
| 174 |
"Log in first, then click 'Start' to begin. Answer each question, click 'Next', and finally click 'Submit' to publish your results to the Hugging Face Hub."
|
| 175 |
)
|
| 176 |
+
|
| 177 |
+
with gr.Row(variant="panel"):
|
|
|
|
|
|
|
| 178 |
question_text = gr.Markdown("")
|
| 179 |
radio_choices = gr.Radio(
|
| 180 |
+
choices=[], label="Your Answer", scale=1.5, visible=False
|
| 181 |
)
|
| 182 |
|
| 183 |
with gr.Row(variant="compact"):
|
| 184 |
status_text = gr.Markdown("")
|
| 185 |
+
final_markdown = gr.Markdown("")
|
| 186 |
|
| 187 |
with gr.Row(variant="compact"):
|
| 188 |
+
login_btn = gr.LoginButton(visible=True)
|
| 189 |
+
start_btn = gr.Button("Start ⏭️", visible=True)
|
| 190 |
+
next_btn = gr.Button("Next ⏭️", visible=False)
|
| 191 |
+
submit_btn = gr.Button("Submit ✅", visible=False)
|
| 192 |
+
|
| 193 |
+
# Wire up the event handlers
|
| 194 |
+
login_btn.click(
|
| 195 |
+
fn=on_user_logged_in,
|
| 196 |
+
inputs=None,
|
| 197 |
+
outputs=[
|
| 198 |
+
login_btn,
|
| 199 |
+
start_btn,
|
| 200 |
+
next_btn,
|
| 201 |
+
submit_btn,
|
| 202 |
+
question_text,
|
| 203 |
+
radio_choices,
|
| 204 |
+
status_text,
|
| 205 |
+
question_idx,
|
| 206 |
+
user_answers,
|
| 207 |
+
final_markdown,
|
| 208 |
+
user_token,
|
| 209 |
+
],
|
| 210 |
+
)
|
| 211 |
|
|
|
|
| 212 |
start_btn.click(
|
| 213 |
fn=handle_quiz,
|
| 214 |
+
inputs=[question_idx, user_answers, gr.State(""), gr.State(True)],
|
| 215 |
outputs=[
|
| 216 |
question_text,
|
| 217 |
radio_choices,
|
|
|
|
| 219 |
question_idx,
|
| 220 |
user_answers,
|
| 221 |
start_btn,
|
| 222 |
+
next_btn,
|
| 223 |
+
submit_btn,
|
| 224 |
final_markdown,
|
| 225 |
],
|
| 226 |
)
|
| 227 |
|
|
|
|
| 228 |
next_btn.click(
|
| 229 |
fn=handle_quiz,
|
| 230 |
inputs=[question_idx, user_answers, radio_choices, gr.State(False)],
|
|
|
|
| 235 |
question_idx,
|
| 236 |
user_answers,
|
| 237 |
start_btn,
|
| 238 |
+
next_btn,
|
| 239 |
+
submit_btn,
|
| 240 |
final_markdown,
|
| 241 |
],
|
| 242 |
)
|
| 243 |
|
| 244 |
submit_btn.click(fn=push_results_to_hub, inputs=[user_answers])
|
| 245 |
|
|
|
|
| 246 |
if __name__ == "__main__":
|
| 247 |
# Note: If testing locally, you'll need to run `huggingface-cli login` or set HF_TOKEN
|
| 248 |
# environment variable for the login to work locally.
|