Merge branch 'main' of https://huggingface.co/spaces/agents-course/unit_1_quiz
Browse files
app.py
CHANGED
@@ -2,6 +2,10 @@ import os
|
|
2 |
from datetime import datetime
|
3 |
import random
|
4 |
|
|
|
|
|
|
|
|
|
5 |
import gradio as gr
|
6 |
from datasets import load_dataset, Dataset
|
7 |
from huggingface_hub import whoami
|
@@ -12,6 +16,13 @@ EXAM_PASSING_SCORE = os.getenv("EXAM_PASSING_SCORE") or 0.7
|
|
12 |
|
13 |
ds = load_dataset(EXAM_DATASET_ID, split="train")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Convert dataset to a list of dicts and randomly sort
|
16 |
quiz_data = ds.to_pandas().to_dict("records")
|
17 |
random.shuffle(quiz_data)
|
@@ -55,6 +66,24 @@ def on_user_logged_in(token: gr.OAuthToken | None):
|
|
55 |
None, # no token
|
56 |
]
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
60 |
"""
|
@@ -92,9 +121,18 @@ def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
|
92 |
)
|
93 |
sanitized_name = user_info["name"].replace("-", "000")
|
94 |
new_ds.push_to_hub(repo_id=repo_id, split=sanitized_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}"
|
96 |
|
97 |
|
|
|
|
|
98 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
99 |
"""
|
100 |
Handle quiz state transitions and store answers
|
@@ -126,7 +164,7 @@ def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
|
126 |
return [
|
127 |
"", # question_text
|
128 |
gr.update(choices=[], visible=False), # hide radio choices
|
129 |
-
f"{'
|
130 |
question_idx,
|
131 |
user_answers,
|
132 |
gr.update(visible=False), # start button visibility
|
|
|
2 |
from datetime import datetime
|
3 |
import random
|
4 |
|
5 |
+
import pandas as pd
|
6 |
+
from huggingface_hub import HfApi, hf_hub_download, Repository
|
7 |
+
from huggingface_hub.repocard import metadata_load
|
8 |
+
|
9 |
import gradio as gr
|
10 |
from datasets import load_dataset, Dataset
|
11 |
from huggingface_hub import whoami
|
|
|
16 |
|
17 |
ds = load_dataset(EXAM_DATASET_ID, split="train")
|
18 |
|
19 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/agents-course/certificates"
|
20 |
+
CERTIFIED_USERS_FILENAME = "certified_students.csv"
|
21 |
+
CERTIFIED_USERS_DIR = "certificates"
|
22 |
+
repo = Repository(
|
23 |
+
local_dir=CERTIFIED_USERS_DIR, clone_from=DATASET_REPO_URL, use_auth_token=os.getenv("HF_TOKEN")
|
24 |
+
)
|
25 |
+
|
26 |
# Convert dataset to a list of dicts and randomly sort
|
27 |
quiz_data = ds.to_pandas().to_dict("records")
|
28 |
random.shuffle(quiz_data)
|
|
|
66 |
None, # no token
|
67 |
]
|
68 |
|
69 |
+
def add_certified_user(hf_username, pass_percentage, submission_time):
|
70 |
+
"""
|
71 |
+
Add the certified user to the database
|
72 |
+
"""
|
73 |
+
print("ADD CERTIFIED USER")
|
74 |
+
repo.git_pull()
|
75 |
+
history = pd.read_csv(os.path.join(CERTIFIED_USERS_DIR, CERTIFIED_USERS_FILENAME))
|
76 |
+
|
77 |
+
# Check if this hf_username is already in our dataset:
|
78 |
+
check = history.loc[history['hf_username'] == hf_username]
|
79 |
+
if not check.empty:
|
80 |
+
history = history.drop(labels=check.index[0], axis=0)
|
81 |
+
|
82 |
+
new_row = pd.DataFrame({'hf_username': hf_username, 'pass_percentage': pass_percentage, 'datetime': submission_time}, index=[0])
|
83 |
+
history = pd.concat([new_row, history[:]]).reset_index(drop=True)
|
84 |
+
|
85 |
+
history.to_csv(os.path.join(CERTIFIED_USERS_DIR, CERTIFIED_USERS_FILENAME), index=False)
|
86 |
+
repo.push_to_hub(commit_message="Update certified users list")
|
87 |
|
88 |
def push_results_to_hub(user_answers, token: gr.OAuthToken | None):
|
89 |
"""
|
|
|
121 |
)
|
122 |
sanitized_name = user_info["name"].replace("-", "000")
|
123 |
new_ds.push_to_hub(repo_id=repo_id, split=sanitized_name)
|
124 |
+
|
125 |
+
# I'm adding a csv version
|
126 |
+
# The idea, if the user passed, we create a simple row in a csv
|
127 |
+
print("ADD CERTIFIED USER")
|
128 |
+
# Add this user to our database
|
129 |
+
add_certified_user(sanitized_name, grade, submission_time)
|
130 |
+
|
131 |
return f"Your responses have been submitted to the Hub! Final grade: {grade:.1%}"
|
132 |
|
133 |
|
134 |
+
|
135 |
+
|
136 |
def handle_quiz(question_idx, user_answers, selected_answer, is_start):
|
137 |
"""
|
138 |
Handle quiz state transitions and store answers
|
|
|
164 |
return [
|
165 |
"", # question_text
|
166 |
gr.update(choices=[], visible=False), # hide radio choices
|
167 |
+
f"{'🎉 Passed! Click now on ✅ Submit to save your exam score!' if grade >= float(EXAM_PASSING_SCORE) else '❌ Did not pass'}",
|
168 |
question_idx,
|
169 |
user_answers,
|
170 |
gr.update(visible=False), # start button visibility
|