Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
burtenshaw
commited on
Commit
·
0a4e0d7
1
Parent(s):
2ec763a
add custom name functionality
Browse files
app.py
CHANGED
@@ -46,32 +46,32 @@ def on_user_logged_in(token: gr.OAuthToken | None):
|
|
46 |
if token is not None:
|
47 |
return [
|
48 |
gr.update(visible=False), # login_btn
|
49 |
-
gr.update(visible=True),
|
50 |
gr.update(visible=False), # next_btn
|
51 |
gr.update(visible=False), # submit_btn
|
52 |
-
"",
|
53 |
gr.update(choices=[], visible=False), # radio_choices
|
54 |
"Click 'Start' to begin the quiz", # status_text
|
55 |
-
0,
|
56 |
-
[],
|
57 |
gr.update(visible=False), # certificate_img
|
58 |
gr.update(visible=False), # linkedin_btn
|
59 |
-
token,
|
60 |
]
|
61 |
else:
|
62 |
return [
|
63 |
-
gr.update(visible=True),
|
64 |
gr.update(visible=False), # start_btn
|
65 |
gr.update(visible=False), # next_btn
|
66 |
gr.update(visible=False), # submit_btn
|
67 |
-
"",
|
68 |
gr.update(choices=[], visible=False), # radio_choices
|
69 |
-
"",
|
70 |
-
0,
|
71 |
-
[],
|
72 |
gr.update(visible=False), # certificate_img
|
73 |
gr.update(visible=False), # linkedin_btn
|
74 |
-
None,
|
75 |
]
|
76 |
|
77 |
|
@@ -168,7 +168,10 @@ async def upload_certificate_to_hub(username: str, certificate_img) -> str:
|
|
168 |
|
169 |
|
170 |
async def push_results_to_hub(
|
171 |
-
user_answers,
|
|
|
|
|
|
|
172 |
):
|
173 |
"""Handle quiz completion and certificate generation."""
|
174 |
if token is None or profile is None:
|
@@ -177,6 +180,7 @@ async def push_results_to_hub(
|
|
177 |
gr.update(visible=True, value="Please login first"),
|
178 |
gr.update(visible=False),
|
179 |
gr.update(visible=False),
|
|
|
180 |
)
|
181 |
|
182 |
# Calculate grade
|
@@ -186,19 +190,21 @@ async def push_results_to_hub(
|
|
186 |
|
187 |
if grade < float(EXAM_PASSING_SCORE):
|
188 |
return (
|
189 |
-
gr.update(
|
190 |
-
visible=True,
|
191 |
-
value=f"You scored {grade:.1%}. Please try again to achieve at least "
|
192 |
-
f"{float(EXAM_PASSING_SCORE):.1%}",
|
193 |
-
),
|
194 |
gr.update(visible=False),
|
195 |
gr.update(visible=False),
|
|
|
196 |
)
|
197 |
|
198 |
try:
|
|
|
|
|
|
|
|
|
|
|
199 |
# Generate certificate
|
200 |
certificate_img, _ = generate_certificate(
|
201 |
-
name=
|
202 |
)
|
203 |
|
204 |
# Start certificate upload asynchronously
|
@@ -222,16 +228,16 @@ async def push_results_to_hub(
|
|
222 |
gr.update(visible=True, value=result_message),
|
223 |
gr.update(visible=True, value=certificate_img),
|
224 |
gr.update(visible=True),
|
|
|
225 |
)
|
226 |
|
227 |
except Exception as e:
|
228 |
print(f"Error generating certificate: {e}")
|
229 |
return (
|
230 |
-
gr.update(
|
231 |
-
visible=True, value=f"🎉 Congratulations! You passed with {grade:.1%}!"
|
232 |
-
),
|
233 |
gr.update(visible=False),
|
234 |
gr.update(visible=False),
|
|
|
235 |
)
|
236 |
|
237 |
|
@@ -359,6 +365,15 @@ with gr.Blocks() as demo:
|
|
359 |
next_btn = gr.Button("Next ⏭️", visible=False)
|
360 |
submit_btn = gr.Button("🎓 Get your certificate", visible=False)
|
361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
# Wire up the event handlers
|
363 |
login_btn.click(
|
364 |
fn=on_user_logged_in,
|
@@ -415,8 +430,22 @@ with gr.Blocks() as demo:
|
|
415 |
|
416 |
submit_btn.click(
|
417 |
fn=push_results_to_hub,
|
418 |
-
inputs=[
|
419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
)
|
421 |
|
422 |
if __name__ == "__main__":
|
|
|
46 |
if token is not None:
|
47 |
return [
|
48 |
gr.update(visible=False), # login_btn
|
49 |
+
gr.update(visible=True), # start_btn
|
50 |
gr.update(visible=False), # next_btn
|
51 |
gr.update(visible=False), # submit_btn
|
52 |
+
"", # question_text
|
53 |
gr.update(choices=[], visible=False), # radio_choices
|
54 |
"Click 'Start' to begin the quiz", # status_text
|
55 |
+
0, # question_idx
|
56 |
+
[], # user_answers
|
57 |
gr.update(visible=False), # certificate_img
|
58 |
gr.update(visible=False), # linkedin_btn
|
59 |
+
token, # user_token
|
60 |
]
|
61 |
else:
|
62 |
return [
|
63 |
+
gr.update(visible=True), # login_btn
|
64 |
gr.update(visible=False), # start_btn
|
65 |
gr.update(visible=False), # next_btn
|
66 |
gr.update(visible=False), # submit_btn
|
67 |
+
"", # question_text
|
68 |
gr.update(choices=[], visible=False), # radio_choices
|
69 |
+
"", # status_text
|
70 |
+
0, # question_idx
|
71 |
+
[], # user_answers
|
72 |
gr.update(visible=False), # certificate_img
|
73 |
gr.update(visible=False), # linkedin_btn
|
74 |
+
None, # user_token
|
75 |
]
|
76 |
|
77 |
|
|
|
168 |
|
169 |
|
170 |
async def push_results_to_hub(
|
171 |
+
user_answers,
|
172 |
+
custom_name: str | None,
|
173 |
+
token: gr.OAuthToken | None,
|
174 |
+
profile: gr.OAuthProfile | None,
|
175 |
):
|
176 |
"""Handle quiz completion and certificate generation."""
|
177 |
if token is None or profile is None:
|
|
|
180 |
gr.update(visible=True, value="Please login first"),
|
181 |
gr.update(visible=False),
|
182 |
gr.update(visible=False),
|
183 |
+
gr.update(visible=False), # hide custom name input
|
184 |
)
|
185 |
|
186 |
# Calculate grade
|
|
|
190 |
|
191 |
if grade < float(EXAM_PASSING_SCORE):
|
192 |
return (
|
193 |
+
gr.update(visible=True, value=f"You scored {grade:.1%}..."),
|
|
|
|
|
|
|
|
|
194 |
gr.update(visible=False),
|
195 |
gr.update(visible=False),
|
196 |
+
gr.update(visible=False), # hide custom name input
|
197 |
)
|
198 |
|
199 |
try:
|
200 |
+
# Use custom name if provided, otherwise use profile name
|
201 |
+
name = (
|
202 |
+
custom_name.strip() if custom_name and custom_name.strip() else profile.name
|
203 |
+
)
|
204 |
+
|
205 |
# Generate certificate
|
206 |
certificate_img, _ = generate_certificate(
|
207 |
+
name=name, profile_url=profile.picture
|
208 |
)
|
209 |
|
210 |
# Start certificate upload asynchronously
|
|
|
228 |
gr.update(visible=True, value=result_message),
|
229 |
gr.update(visible=True, value=certificate_img),
|
230 |
gr.update(visible=True),
|
231 |
+
gr.update(visible=True), # show custom name input
|
232 |
)
|
233 |
|
234 |
except Exception as e:
|
235 |
print(f"Error generating certificate: {e}")
|
236 |
return (
|
237 |
+
gr.update(visible=True, value=f"🎉 You passed with {grade:.1%}!"),
|
|
|
|
|
238 |
gr.update(visible=False),
|
239 |
gr.update(visible=False),
|
240 |
+
gr.update(visible=False), # hide custom name input
|
241 |
)
|
242 |
|
243 |
|
|
|
365 |
next_btn = gr.Button("Next ⏭️", visible=False)
|
366 |
submit_btn = gr.Button("🎓 Get your certificate", visible=False)
|
367 |
|
368 |
+
with gr.Row(variant="panel"):
|
369 |
+
custom_name_input = gr.Textbox(
|
370 |
+
label="Custom Name for Certificate",
|
371 |
+
placeholder="Enter name as you want it to appear on the certificate",
|
372 |
+
info="Leave empty to use your Hugging Face profile name",
|
373 |
+
visible=False,
|
374 |
+
value=None,
|
375 |
+
)
|
376 |
+
|
377 |
# Wire up the event handlers
|
378 |
login_btn.click(
|
379 |
fn=on_user_logged_in,
|
|
|
430 |
|
431 |
submit_btn.click(
|
432 |
fn=push_results_to_hub,
|
433 |
+
inputs=[
|
434 |
+
user_answers,
|
435 |
+
custom_name_input,
|
436 |
+
],
|
437 |
+
outputs=[
|
438 |
+
status_text,
|
439 |
+
certificate_img,
|
440 |
+
linkedin_btn,
|
441 |
+
custom_name_input,
|
442 |
+
],
|
443 |
+
)
|
444 |
+
|
445 |
+
custom_name_input.submit(
|
446 |
+
fn=push_results_to_hub,
|
447 |
+
inputs=[user_answers, custom_name_input],
|
448 |
+
outputs=[status_text, certificate_img, linkedin_btn, custom_name_input],
|
449 |
)
|
450 |
|
451 |
if __name__ == "__main__":
|