burtenshaw
commited on
Commit
·
1258179
1
Parent(s):
1cb2d7e
make specific to shared course use
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
from datasets import load_dataset, Dataset
|
| 5 |
from huggingface_hub import whoami
|
| 6 |
|
| 7 |
|
| 8 |
-
EXAM_DATASET_ID = os.getenv("EXAM_DATASET_ID") or "
|
| 9 |
|
| 10 |
ds = load_dataset(EXAM_DATASET_ID, split="train")
|
| 11 |
|
|
@@ -36,10 +37,15 @@ def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
|
| 36 |
return
|
| 37 |
else:
|
| 38 |
gr.Info("Submitting answers to the Hub. Please wait...", duration=2)
|
|
|
|
| 39 |
user_info = whoami(token=token.token)
|
| 40 |
-
repo_id = f"{
|
|
|
|
| 41 |
|
| 42 |
new_ds = Dataset.from_list(user_answers)
|
|
|
|
|
|
|
|
|
|
| 43 |
new_ds.push_to_hub(repo_id)
|
| 44 |
gr.Success("Your responses have been submitted to the Hub!")
|
| 45 |
|
|
@@ -61,8 +67,17 @@ def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
|
| 61 |
# If not the very first question, store the user's last selection
|
| 62 |
if question_idx < len(quiz_data):
|
| 63 |
current_q = quiz_data[question_idx]
|
|
|
|
|
|
|
|
|
|
| 64 |
user_answers.append(
|
| 65 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
question_idx += 1
|
| 68 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from datasets import load_dataset, Dataset
|
| 6 |
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 |
|
|
|
|
| 37 |
return
|
| 38 |
else:
|
| 39 |
gr.Info("Submitting answers to the Hub. Please wait...", duration=2)
|
| 40 |
+
|
| 41 |
user_info = whoami(token=token.token)
|
| 42 |
+
repo_id = f"{EXAM_DATASET_ID}_student_responses" # e.g. 'myUsername/quiz-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: {"username": user_info["name"], "datetime": submission_time}
|
| 48 |
+
)
|
| 49 |
new_ds.push_to_hub(repo_id)
|
| 50 |
gr.Success("Your responses have been submitted to the Hub!")
|
| 51 |
|
|
|
|
| 67 |
# If not the very first question, store the user's last selection
|
| 68 |
if question_idx < len(quiz_data):
|
| 69 |
current_q = quiz_data[question_idx]
|
| 70 |
+
correct_reference = current_q["correct_answer"]
|
| 71 |
+
correct_reference = f"answer_{correct_reference}".lower()
|
| 72 |
+
is_correct = selected_answer == current_q[correct_reference]
|
| 73 |
user_answers.append(
|
| 74 |
+
{
|
| 75 |
+
"question": current_q["question"],
|
| 76 |
+
"selected_answer": selected_answer,
|
| 77 |
+
"correct_answer": current_q[correct_reference],
|
| 78 |
+
"is_correct": is_correct,
|
| 79 |
+
"correct_reference": correct_reference,
|
| 80 |
+
}
|
| 81 |
)
|
| 82 |
question_idx += 1
|
| 83 |
|