ThomasSimonini HF Staff commited on
Commit
5bb2ded
Β·
verified Β·
1 Parent(s): 9fc56a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -2,49 +2,57 @@ import gradio as gr
2
  from datasets import load_dataset, Dataset
3
  from datetime import datetime
4
  import io
 
5
  import os
6
 
7
- # Load the datasets
8
  SCORES_DATASET = "agents-course/unit4-students-scores"
9
  CERTIFICATES_DATASET = "agents-course/course-certificates-of-excellence"
10
- THRESOLD_SCORE = 45
11
 
12
- # Check the score based on username
13
  def check_user_score(username):
14
  score_data = load_dataset(SCORES_DATASET, split="train", download_mode="force_redownload")
15
  matches = [row for row in score_data if row["username"] == username]
16
  return matches[0] if matches else None
17
 
18
-
19
- # Check if this user already generated a certificate
20
  def has_certificate_entry(username):
21
  cert_data = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload")
22
  return any(row["username"] == username for row in cert_data)
23
 
24
-
25
  def add_certificate_entry(username, name):
26
- # Create a new entry
27
  new_entry = {
28
  "username": username,
29
  "name": name,
30
  "date": datetime.now().strftime("%Y-%m-%d"),
31
  }
32
-
33
- # Download the dataset, append and push
34
  ds = load_dataset(CERTIFICATES_DATASET, split="train")
35
- updated = ds.add_item(new_entry)
36
- updated.push_to_hub(CERTIFICATES_DATASET)
37
 
 
38
  def generate_certificate(name, score):
39
- pass
40
-
41
-
42
- def handle_certificate(name, request: gr.Request):
43
- username = request.username
44
-
45
- if not username:
 
 
 
 
 
 
 
 
 
46
  return "You must be logged in with your Hugging Face account.", None
47
 
 
48
  user_score = check_user_score(username)
49
 
50
  if not user_score:
@@ -52,18 +60,19 @@ def handle_certificate(name, request: gr.Request):
52
 
53
  score = user_score["score"]
54
 
55
- if score < THRESOLD_SCORE:
56
- return f"Your score is {score}. You need at least {THRESOLD_SCORE} to pass.", None
57
 
58
- # Passed: check if already in certificate dataset
59
  if not has_certificate_entry(username):
60
  add_certificate_entry(username, name)
61
 
62
  certificate = generate_certificate(name, score)
63
  return "Congratulations! Here's your certificate:", certificate
64
 
65
- with gr.Blocks(auth=True) as demo:
 
66
  gr.Markdown("# πŸŽ“ Unit 4 Certificate Generator")
 
67
  with gr.Row():
68
  name_input = gr.Text(label="Enter your name")
69
  generate_btn = gr.Button("Get my certificate")
@@ -72,8 +81,8 @@ with gr.Blocks(auth=True) as demo:
72
 
73
  generate_btn.click(
74
  fn=handle_certificate,
75
- inputs=[name_input],
76
  outputs=[output_text, cert_file]
77
  )
78
 
79
- demo.launch()
 
2
  from datasets import load_dataset, Dataset
3
  from datetime import datetime
4
  import io
5
+ from fpdf import FPDF
6
  import os
7
 
8
+ # Constants
9
  SCORES_DATASET = "agents-course/unit4-students-scores"
10
  CERTIFICATES_DATASET = "agents-course/course-certificates-of-excellence"
11
+ THRESHOLD_SCORE = 45
12
 
13
+ # Function to check user score
14
  def check_user_score(username):
15
  score_data = load_dataset(SCORES_DATASET, split="train", download_mode="force_redownload")
16
  matches = [row for row in score_data if row["username"] == username]
17
  return matches[0] if matches else None
18
 
19
+ # Function to check if certificate entry exists
 
20
  def has_certificate_entry(username):
21
  cert_data = load_dataset(CERTIFICATES_DATASET, split="train", download_mode="force_redownload")
22
  return any(row["username"] == username for row in cert_data)
23
 
24
+ # Function to add certificate entry
25
  def add_certificate_entry(username, name):
 
26
  new_entry = {
27
  "username": username,
28
  "name": name,
29
  "date": datetime.now().strftime("%Y-%m-%d"),
30
  }
 
 
31
  ds = load_dataset(CERTIFICATES_DATASET, split="train")
32
+ ds = ds.add_item(new_entry)
33
+ ds.push_to_hub(CERTIFICATES_DATASET)
34
 
35
+ # Function to generate certificate PDF
36
  def generate_certificate(name, score):
37
+ pdf = FPDF()
38
+ pdf.add_page()
39
+ pdf.set_font("Arial", size=24)
40
+ pdf.cell(200, 20, txt="Certificate of Excellence", ln=True, align="C")
41
+ pdf.set_font("Arial", size=16)
42
+ pdf.cell(200, 10, txt=f"Presented to {name}", ln=True, align="C")
43
+ pdf.cell(200, 10, txt=f"For passing Unit 4 with a score of {score}", ln=True, align="C")
44
+ pdf.cell(200, 10, txt=f"Date: {datetime.now().strftime('%Y-%m-%d')}", ln=True, align="C")
45
+ pdf_output = io.BytesIO()
46
+ pdf.output(pdf_output)
47
+ pdf_output.seek(0)
48
+ return pdf_output
49
+
50
+ # Main function to handle certificate generation
51
+ def handle_certificate(name, profile: gr.OAuthProfile | None):
52
+ if profile is None:
53
  return "You must be logged in with your Hugging Face account.", None
54
 
55
+ username = profile.username
56
  user_score = check_user_score(username)
57
 
58
  if not user_score:
 
60
 
61
  score = user_score["score"]
62
 
63
+ if score < THRESHOLD_SCORE:
64
+ return f"Your score is {score}. You need at least {THRESHOLD_SCORE} to pass.", None
65
 
 
66
  if not has_certificate_entry(username):
67
  add_certificate_entry(username, name)
68
 
69
  certificate = generate_certificate(name, score)
70
  return "Congratulations! Here's your certificate:", certificate
71
 
72
+ # Gradio interface
73
+ with gr.Blocks() as demo:
74
  gr.Markdown("# πŸŽ“ Unit 4 Certificate Generator")
75
+ gr.LoginButton()
76
  with gr.Row():
77
  name_input = gr.Text(label="Enter your name")
78
  generate_btn = gr.Button("Get my certificate")
 
81
 
82
  generate_btn.click(
83
  fn=handle_certificate,
84
+ inputs=[name_input, gr.OAuthProfile()],
85
  outputs=[output_text, cert_file]
86
  )
87
 
88
+ demo.launch()