ThomasSimonini HF Staff commited on
Commit
35b9d7e
·
verified ·
1 Parent(s): f9b68ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from datasets import load_dataset
3
  from datetime import datetime
4
  import io
5
-
6
 
7
  # Constants
8
  SCORES_DATASET = "agents-course/unit4-students-scores"
@@ -33,8 +33,25 @@ def add_certificate_entry(username, name):
33
 
34
  # Function to generate certificate PDF
35
  def generate_certificate(name, score):
36
- pdf_output = ""
37
- return pdf_output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Main function to handle certificate generation
40
  def handle_certificate(name, profile: gr.OAuthProfile):
@@ -66,12 +83,13 @@ with gr.Blocks() as demo:
66
  name_input = gr.Text(label="Enter your name")
67
  generate_btn = gr.Button("Get my certificate")
68
  output_text = gr.Textbox(label="Result")
69
- cert_file = gr.File(label="Your Certificate", file_types=[".pdf"])
 
70
 
71
  generate_btn.click(
72
  fn=handle_certificate,
73
  inputs=[name_input],
74
- outputs=[output_text, cert_file]
75
  )
76
 
77
  demo.launch()
 
2
  from datasets import load_dataset
3
  from datetime import datetime
4
  import io
5
+ from PIL import Image, ImageDraw, ImageFont
6
 
7
  # Constants
8
  SCORES_DATASET = "agents-course/unit4-students-scores"
 
33
 
34
  # Function to generate certificate PDF
35
  def generate_certificate(name, score):
36
+ """Generate certificate image and PDF."""
37
+ certificate_path = os.path.join(
38
+ os.path.dirname(__file__), "templates", "certificate.png"
39
+ )
40
+ im = Image.open(certificate_path)
41
+ d = ImageDraw.Draw(im)
42
+
43
+ name_font = ImageFont.truetype("Quattrocento-Regular.ttf", 100)
44
+ date_font = ImageFont.truetype("Quattrocento-Regular.ttf", 48)
45
+
46
+ name = name.title()
47
+ d.text((1000, 740), name, fill="black", anchor="mm", font=name_font)
48
+
49
+ d.text((1480, 1170), str(date.today()), fill="black", anchor="mm", font=date_font)
50
+
51
+ pdf = im.convert("RGB")
52
+ pdf.save("certificate.pdf")
53
+
54
+ return im, "certificate.pdf"
55
 
56
  # Main function to handle certificate generation
57
  def handle_certificate(name, profile: gr.OAuthProfile):
 
83
  name_input = gr.Text(label="Enter your name")
84
  generate_btn = gr.Button("Get my certificate")
85
  output_text = gr.Textbox(label="Result")
86
+ cert_image = gr.Image(label="Your Certificate")
87
+ cert_file = gr.File(label="Download Certificate (PDF)", file_types=[".pdf"])
88
 
89
  generate_btn.click(
90
  fn=handle_certificate,
91
  inputs=[name_input],
92
+ outputs=[output_text, cert_image, cert_file]
93
  )
94
 
95
  demo.launch()