Spaces:
Sleeping
Sleeping
import os | |
from datetime import datetime | |
import random | |
import gradio as gr | |
from datasets import load_dataset, Dataset | |
from huggingface_hub import whoami | |
# Dataset corretto | |
EXAM_DATASET_ID = "huggingface-course/chapter_1_exam" | |
EXAM_MAX_QUESTIONS = 10 | |
EXAM_PASSING_SCORE = 0.7 | |
# Caricamento e mescolamento delle domande | |
ds = load_dataset(EXAM_DATASET_ID, split="train") | |
quiz_data = ds.to_pandas().to_dict("records") | |
random.shuffle(quiz_data) | |
quiz_data = quiz_data[:EXAM_MAX_QUESTIONS] | |
def on_user_logged_in(token): | |
if token is not None: | |
return [ | |
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), | |
gr.update(visible=False), "", [], | |
"Click 'Start' to begin the quiz", 0, [], "", token | |
] | |
else: | |
return [ | |
gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), | |
gr.update(visible=False), "", [], "", 0, [], "", None | |
] | |
def push_results_to_hub(user_answers, token): | |
if token is None: | |
gr.Warning("Please log in to Hugging Face before pushing!") | |
return | |
correct_count = sum(1 for a in user_answers if a["is_correct"]) | |
total_questions = len(user_answers) | |
grade = correct_count / total_questions if total_questions > 0 else 0 | |
if grade < EXAM_PASSING_SCORE: | |
gr.Warning(f"Score {grade:.1%} below passing threshold of {EXAM_PASSING_SCORE:.1%}") | |
return f"You scored {grade:.1%}. Please try again to achieve at least {EXAM_PASSING_SCORE:.1%}" | |
gr.Info("Submitting answers to the Hub. Please wait...", duration=2) | |
user_info = whoami(token=token.token) | |
repo_id = f"{EXAM_DATASET_ID}_student_responses" | |
submission_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
new_ds = Dataset.from_list(user_answers) | |
new_ds = new_ds.map(lambda x: { | |
"username": user_info["name"], | |
"datetime": submission_time, | |
"grade": grade | |
}) | |
new_ds.push_to_hub(repo_id) | |
return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}" | |
def handle_quiz(question_idx, user_answers, selected_answer, is_start): | |
if not is_start and question_idx < len(quiz_data): | |
current_q = quiz_data[question_idx] | |
correct_key = f"answer_{current_q['correct_answer']}".lower() | |
is_correct = selected_answer == current_q[correct_key] | |
user_answers.append({ | |
"question": current_q["question"], | |
"selected_answer": selected_answer, | |
"correct_answer": current_q[correct_key], | |
"is_correct": is_correct, | |
"correct_reference": correct_key | |
}) | |
question_idx += 1 | |
if question_idx >= len(quiz_data): | |
correct_count = sum(1 for a in user_answers if a["is_correct"]) | |
grade = correct_count / len(user_answers) | |
results_text = f"**Quiz Complete!**\n\nYour score: {grade:.1%}\nPassing score: {EXAM_PASSING_SCORE:.1%}\n" | |
return [ | |
"", gr.update(choices=[], visible=False), | |
f"{'✅ Passed!' if grade >= EXAM_PASSING_SCORE else '❌ Did not pass'}", | |
question_idx, user_answers, | |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), | |
results_text | |
] | |
q = quiz_data[question_idx] | |
return [ | |
f"## Question {question_idx + 1}\n### {q['question']}", | |
gr.update(choices=[q["answer_a"], q["answer_b"], q["answer_c"], q["answer_d"]], value=None, visible=True), | |
"Select an answer and click 'Next' to continue.", | |
question_idx, user_answers, | |
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), "" | |
] | |
def success_message(response): | |
return f"{response}\n\n**Success!**" | |
with gr.Blocks() as demo: | |
demo.title = f"Dataset Quiz for {EXAM_DATASET_ID}" | |
question_idx = gr.State(value=0) | |
user_answers = gr.State(value=[]) | |
user_token = gr.State(value=None) | |
gr.Markdown(f"## Welcome to the {EXAM_DATASET_ID} Quiz") | |
gr.Markdown("Log in first, then click 'Start' to begin. Answer each question, click 'Next', and finally click 'Submit'.") | |
question_text = gr.Markdown("") | |
radio_choices = gr.Radio(choices=[], label="Your Answer", scale=1.5, visible=False) | |
status_text = gr.Markdown("") | |
final_markdown = gr.Markdown("") | |
login_btn = gr.LoginButton(visible=True) | |
start_btn = gr.Button("Start ⏭️", visible=True) | |
next_btn = gr.Button("Next ⏭️", visible=False) | |
submit_btn = gr.Button("Submit ✅", visible=False) | |
login_btn.click( | |
fn=on_user_logged_in, | |
inputs=None, | |
outputs=[login_btn, start_btn, next_btn, submit_btn, question_text, radio_choices, | |
status_text, question_idx, user_answers, final_markdown, user_token] | |
) | |
start_btn.click( | |
fn=handle_quiz, | |
inputs=[question_idx, user_answers, gr.State(""), gr.State(True)], | |
outputs=[question_text, radio_choices, status_text, question_idx, user_answers, | |
start_btn, next_btn, submit_btn, final_markdown] | |
) | |
next_btn.click( | |
fn=handle_quiz, | |
inputs=[question_idx, user_answers, radio_choices, gr.State(False)], | |
outputs=[question_text, radio_choices, status_text, question_idx, user_answers, | |
start_btn, next_btn, submit_btn, final_markdown] | |
) | |
submit_btn.click( | |
fn=push_results_to_hub, | |
inputs=[user_answers, user_token], | |
outputs=[final_markdown] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |