wseo commited on
Commit
dc59648
β€’
1 Parent(s): 2dba9d3

docs: format success message

Browse files
Files changed (1) hide show
  1. app.py +106 -76
app.py CHANGED
@@ -5,7 +5,7 @@ from gradio_client import Client
5
  from PIL import Image, ImageDraw, ImageFont
6
 
7
  from datetime import date
8
- import time
9
 
10
  import os
11
  import sys
@@ -31,9 +31,9 @@ def has_contributions(repo_type, hf_username, organization):
31
  :param organization: HF Hub organization
32
  """
33
  repo_list = {
34
- 'model': api.list_models,
35
- 'dataset': api.list_datasets,
36
- 'space': api.list_spaces
37
  }
38
 
39
  for repo in repo_list[repo_type](author=organization):
@@ -49,9 +49,9 @@ def get_hub_footprint(hf_username, organization):
49
  :param hf_username: HF Hub username
50
  :param organization: HF Hub organization
51
  """
52
- has_models = has_contributions('model', hf_username, organization)
53
- has_datasets = has_contributions('dataset', hf_username, organization)
54
- has_spaces = has_contributions('space', hf_username, organization)
55
 
56
  return (has_models, has_datasets, has_spaces)
57
 
@@ -61,17 +61,17 @@ def check_if_passed(hf_username):
61
  Check if given user contributed to hackathon
62
  :param hf_username: HF Hub username
63
  """
64
-
65
- passed = False
66
  certificate_type = ""
67
 
68
  # If the user contributed to models, datasets and spaces then assign excellence
69
  if all(get_hub_footprint(hf_username, ORGANIZATION)):
70
- passed = True
71
- certificate_type = "excellence"
72
  elif any(get_hub_footprint(hf_username, ORGANIZATION)):
73
- passed = True
74
- certificate_type = "completion"
75
 
76
  return passed, certificate_type
77
 
@@ -90,25 +90,25 @@ def generate_certificate(certificate_template, first_name, last_name, hf_usernam
90
 
91
  name_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 60)
92
  username_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 18)
93
-
94
  name = str(first_name) + " " + str(last_name)
95
  print("NAME", name)
96
-
97
  # Debug line name
98
- #d.line(((0, 419), (1000, 419)), "gray")
99
- #d.line(((538, 0), (538, 1400)), "gray")
100
-
101
  # Name
102
- d.text((538, 419), name, fill=(87,87,87), anchor="mm", font=name_font)
103
 
104
  # Debug line id
105
- #d.line(((815, 0), (815, 1400)), "gray")
106
 
107
  # Date of certification
108
- d.text((815, 327), f"HKH23-{hf_username}", fill=(117,117,117), font=username_font)
109
 
110
- pdf = im.convert('RGB')
111
- pdf.save('certificate.pdf')
112
 
113
  return im, "./certificate.pdf"
114
 
@@ -116,7 +116,14 @@ def generate_certificate(certificate_template, first_name, last_name, hf_usernam
116
  def create_initial_csv(path):
117
  """Create an initial CSV file with headers if it doesn't exist."""
118
  # Define the headers for our CSV file
119
- headers = ['hf_username', 'first_name', 'last_name', 'certificate_type', 'datetime', 'pdf_path']
 
 
 
 
 
 
 
120
  # Create a new DataFrame with no data and these headers
121
  df = pd.DataFrame(columns=headers)
122
  # Save the DataFrame to a CSV file
@@ -128,7 +135,12 @@ def add_certified_user(hf_username, first_name, last_name, certificate_type):
128
  Add the certified user to the dataset and include their certificate PDF.
129
  """
130
  print("ADD CERTIFIED USER")
131
- repo = Repository(local_dir="data", clone_from=DATASET_REPO_URL, git_user="wseo", git_email="[email protected]")
 
 
 
 
 
132
  repo.git_pull()
133
 
134
  csv_full_path = os.path.join("data", CERTIFIED_USERS_FILENAME)
@@ -139,7 +151,7 @@ def add_certified_user(hf_username, first_name, last_name, certificate_type):
139
  history = pd.read_csv(csv_full_path)
140
 
141
  # Check if this hf_username is already in our dataset:
142
- check = history.loc[history['hf_username'] == hf_username]
143
  if not check.empty:
144
  history = history.drop(labels=check.index[0], axis=0)
145
 
@@ -148,24 +160,26 @@ def add_certified_user(hf_username, first_name, last_name, certificate_type):
148
  # Copy the PDF from its current location to the target directory in the repository
149
  pdf_repo_filename = f"{hf_username}.pdf" # Create a specific name for the PDF file
150
  pdf_repo_path_full = os.path.join(pdfs_repo_path, pdf_repo_filename)
151
-
152
  # Create the pdfs directory if it doesn't exist
153
  os.makedirs(pdfs_repo_path, exist_ok=True)
154
-
155
- shutil.copy('./certificate.pdf', pdf_repo_path_full) # Copy the file
156
-
157
- # Now, add a new entry to your CSV for this user and their PDF
158
- new_row = pd.DataFrame({
159
- 'hf_username': hf_username,
160
- 'first_name': first_name,
161
- 'last_name': last_name,
162
- 'certificate_type': certificate_type,
163
- 'datetime': time.time(), # This captures the current time
164
- 'pdf_path': pdf_repo_path_full[5:] # This is the relative path to the PDF within the repo
165
- }, index=[0])
166
-
 
 
167
  history = pd.concat([new_row, history[:]]).reset_index(drop=True)
168
-
169
  # Save the updated CSV
170
  history.to_csv(os.path.join("data", CERTIFIED_USERS_FILENAME), index=False)
171
 
@@ -174,9 +188,9 @@ def add_certified_user(hf_username, first_name, last_name, certificate_type):
174
  repo.push_to_hub(commit_message="Update certified users list and add PDF")
175
 
176
 
177
- def create_certificate(passed, certificate_type, hf_username, first_name, last_name):
178
  """
179
- Generates certificate, adds message, saves username of the certified user
180
  :param passed: boolean whether the user passed enough assignments
181
  :param certificate_type: type of the certificate - completion or excellence
182
  :param hf_username: Hugging Face Hub username entered by user
@@ -185,36 +199,41 @@ def create_certificate(passed, certificate_type, hf_username, first_name, last_n
185
  """
186
 
187
  if passed and certificate_type == "excellence":
188
- # Generate a certificate of
189
- certificate, pdf = generate_certificate("./certificate-excellence.png", first_name, last_name, hf_username)
 
 
190
  # Add this user to our database
191
- add_certified_user(hf_username, first_name, last_name, certificate_type)
192
  # Add a message
193
  message = f"""
194
- Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰! \n
195
- Since you contributed to models, datasets, and spaces- you get a Certificate of Excellence πŸŽ“. \n
196
- You can download your certificate below ⬇️ \n
197
- Don't hesitate to share your certificate link below on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€—
198
- https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf
 
199
  """
200
- elif passed and certificate_type == "completion":
201
  # Generate a certificate of completion
202
- certificate, pdf = generate_certificate("./certificate-completion.png", first_name, last_name, hf_username)
 
 
203
  # Add this user to our database
204
- add_certified_user(hf_username, first_name, last_name, certificate_type)
205
  # Add a message
206
  message = f"""
207
- Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰! \n
208
- Since you contributed to at least one model, dataset, or space- you get a Certificate of Completion πŸŽ“. \n
209
- You can download your certificate below ⬇️ \n
210
- Don't hesitate to share your certificate link below on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€— \n
 
211
  You can try to get a Certificate of Excellence if you contribute to all types of repos, please don't hesitate to do so.
212
- https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf
213
  """
214
  else:
215
  # Not passed yet
216
  certificate = Image.new("RGB", (100, 100), (255, 255, 255))
217
- pdf = "./fail.pdf"
218
  # Add a message
219
  message = """
220
  You didn't pass the minimum of one contribution to get a certificate of completion.
@@ -225,19 +244,23 @@ def create_certificate(passed, certificate_type, hf_username, first_name, last_n
225
 
226
 
227
  def certification(hf_username, first_name, last_name):
228
- passed, certificate_type = check_if_passed(hf_username)
229
- certificate, message, pdf = create_certificate(passed, certificate_type, hf_username, first_name, last_name)
230
- print("MESSAGE", message)
 
 
 
 
 
 
 
 
 
231
 
232
- if passed:
233
- visible = True
234
- else:
235
- visible = False
236
-
237
- return message, pdf, certificate, output_row.update(visible=visible)
238
 
239
  with gr.Blocks() as demo:
240
- gr.Markdown(f"""
 
241
  # Get your 2023 Hackathon Certificate πŸŽ“
242
  The certification process is completely free:
243
  - To get a *certificate of completion*: you need to **contribute to at least one model, dataset, or space**.
@@ -246,9 +269,12 @@ with gr.Blocks() as demo:
246
  For more information about the certification process [check the hackathon page on certification](https://pseudo-lab.github.io/huggingface-hackathon23/submit.html#certification).
247
 
248
  Don't hesitate to share your certificate on Twitter (tag me [@wonhseo](https://twitter.com/wonhseo), [@pseudolab](https://twitter.com/pseudo-lab), and [@huggingface](https://twitter.com/huggingface)) and on LinkedIn.
249
- """)
250
-
251
- hf_username = gr.Textbox(placeholder="wseo", label="Your Hugging Face Username (case sensitive)")
 
 
 
252
  first_name = gr.Textbox(placeholder="Wonhyeong", label="Your First Name")
253
  last_name = gr.Textbox(placeholder="Seo", label="Your Last Name")
254
 
@@ -259,7 +285,11 @@ with gr.Blocks() as demo:
259
  output_pdf = gr.File()
260
  output_img = gr.components.Image(type="pil")
261
 
262
- check_progress_button.click(fn=certification, inputs=[hf_username, first_name, last_name], outputs=[output_text, output_pdf, output_img, output_row])
 
 
 
 
263
 
264
-
265
- demo.launch(debug=True)
 
5
  from PIL import Image, ImageDraw, ImageFont
6
 
7
  from datetime import date
8
+ import time
9
 
10
  import os
11
  import sys
 
31
  :param organization: HF Hub organization
32
  """
33
  repo_list = {
34
+ "model": api.list_models,
35
+ "dataset": api.list_datasets,
36
+ "space": api.list_spaces,
37
  }
38
 
39
  for repo in repo_list[repo_type](author=organization):
 
49
  :param hf_username: HF Hub username
50
  :param organization: HF Hub organization
51
  """
52
+ has_models = has_contributions("model", hf_username, organization)
53
+ has_datasets = has_contributions("dataset", hf_username, organization)
54
+ has_spaces = has_contributions("space", hf_username, organization)
55
 
56
  return (has_models, has_datasets, has_spaces)
57
 
 
61
  Check if given user contributed to hackathon
62
  :param hf_username: HF Hub username
63
  """
64
+
65
+ passed = False
66
  certificate_type = ""
67
 
68
  # If the user contributed to models, datasets and spaces then assign excellence
69
  if all(get_hub_footprint(hf_username, ORGANIZATION)):
70
+ passed = True
71
+ certificate_type = "excellence"
72
  elif any(get_hub_footprint(hf_username, ORGANIZATION)):
73
+ passed = True
74
+ certificate_type = "completion"
75
 
76
  return passed, certificate_type
77
 
 
90
 
91
  name_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 60)
92
  username_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 18)
93
+
94
  name = str(first_name) + " " + str(last_name)
95
  print("NAME", name)
96
+
97
  # Debug line name
98
+ # d.line(((0, 419), (1000, 419)), "gray")
99
+ # d.line(((538, 0), (538, 1400)), "gray")
100
+
101
  # Name
102
+ d.text((538, 419), name, fill=(87, 87, 87), anchor="mm", font=name_font)
103
 
104
  # Debug line id
105
+ # d.line(((815, 0), (815, 1400)), "gray")
106
 
107
  # Date of certification
108
+ d.text((815, 327), f"HKH23-{hf_username}", fill=(117, 117, 117), font=username_font)
109
 
110
+ pdf = im.convert("RGB")
111
+ pdf.save("certificate.pdf")
112
 
113
  return im, "./certificate.pdf"
114
 
 
116
  def create_initial_csv(path):
117
  """Create an initial CSV file with headers if it doesn't exist."""
118
  # Define the headers for our CSV file
119
+ headers = [
120
+ "hf_username",
121
+ "first_name",
122
+ "last_name",
123
+ "certificate_type",
124
+ "datetime",
125
+ "pdf_path",
126
+ ]
127
  # Create a new DataFrame with no data and these headers
128
  df = pd.DataFrame(columns=headers)
129
  # Save the DataFrame to a CSV file
 
135
  Add the certified user to the dataset and include their certificate PDF.
136
  """
137
  print("ADD CERTIFIED USER")
138
+ repo = Repository(
139
+ local_dir="data",
140
+ clone_from=DATASET_REPO_URL,
141
+ git_user="wseo",
142
+ git_email="[email protected]",
143
+ )
144
  repo.git_pull()
145
 
146
  csv_full_path = os.path.join("data", CERTIFIED_USERS_FILENAME)
 
151
  history = pd.read_csv(csv_full_path)
152
 
153
  # Check if this hf_username is already in our dataset:
154
+ check = history.loc[history["hf_username"] == hf_username]
155
  if not check.empty:
156
  history = history.drop(labels=check.index[0], axis=0)
157
 
 
160
  # Copy the PDF from its current location to the target directory in the repository
161
  pdf_repo_filename = f"{hf_username}.pdf" # Create a specific name for the PDF file
162
  pdf_repo_path_full = os.path.join(pdfs_repo_path, pdf_repo_filename)
163
+
164
  # Create the pdfs directory if it doesn't exist
165
  os.makedirs(pdfs_repo_path, exist_ok=True)
166
+
167
+ shutil.copy("./certificate.pdf", pdf_repo_path_full) # Copy the file
168
+
169
+ new_row = pd.DataFrame(
170
+ {
171
+ "hf_username": hf_username,
172
+ "first_name": first_name,
173
+ "last_name": last_name,
174
+ "certificate_type": certificate_type,
175
+ "datetime": time.time(), # current time
176
+ "pdf_path": pdf_repo_path_full[5:], # relative path to the PDF within the repo
177
+ },
178
+ index=[0],
179
+ )
180
+
181
  history = pd.concat([new_row, history[:]]).reset_index(drop=True)
182
+
183
  # Save the updated CSV
184
  history.to_csv(os.path.join("data", CERTIFIED_USERS_FILENAME), index=False)
185
 
 
188
  repo.push_to_hub(commit_message="Update certified users list and add PDF")
189
 
190
 
191
+ def create_certificate(passed, certificate_type, hf_username, first_name, last_name):
192
  """
193
+ Generates certificate, adds message, saves username of the certified user
194
  :param passed: boolean whether the user passed enough assignments
195
  :param certificate_type: type of the certificate - completion or excellence
196
  :param hf_username: Hugging Face Hub username entered by user
 
199
  """
200
 
201
  if passed and certificate_type == "excellence":
202
+ # Generate a certificate of
203
+ certificate, pdf = generate_certificate(
204
+ "./certificate-excellence.png", first_name, last_name, hf_username
205
+ )
206
  # Add this user to our database
207
+ add_certified_user(hf_username, first_name, last_name, certificate_type)
208
  # Add a message
209
  message = f"""
210
+ Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰!
211
+ Since you contributed to models, datasets, and spaces- you get a Certificate of Excellence πŸŽ“.
212
+ You can download your certificate below ⬇️
213
+ https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf\n
214
+ Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€—
215
+
216
  """
217
+ elif passed and certificate_type == "completion":
218
  # Generate a certificate of completion
219
+ certificate, pdf = generate_certificate(
220
+ "./certificate-completion.png", first_name, last_name, hf_username
221
+ )
222
  # Add this user to our database
223
+ add_certified_user(hf_username, first_name, last_name, certificate_type)
224
  # Add a message
225
  message = f"""
226
+ Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰!
227
+ Since you contributed to at least one model, dataset, or space- you get a Certificate of Completion πŸŽ“.
228
+ You can download your certificate below ⬇️
229
+ https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf\n
230
+ Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€— \n
231
  You can try to get a Certificate of Excellence if you contribute to all types of repos, please don't hesitate to do so.
 
232
  """
233
  else:
234
  # Not passed yet
235
  certificate = Image.new("RGB", (100, 100), (255, 255, 255))
236
+ pdf = "./fail.pdf"
237
  # Add a message
238
  message = """
239
  You didn't pass the minimum of one contribution to get a certificate of completion.
 
244
 
245
 
246
  def certification(hf_username, first_name, last_name):
247
+ passed, certificate_type = check_if_passed(hf_username)
248
+ certificate, message, pdf = create_certificate(
249
+ passed, certificate_type, hf_username, first_name, last_name
250
+ )
251
+ print("MESSAGE", message)
252
+
253
+ if passed:
254
+ visible = True
255
+ else:
256
+ visible = False
257
+
258
+ return message, pdf, certificate, output_row.update(visible=visible)
259
 
 
 
 
 
 
 
260
 
261
  with gr.Blocks() as demo:
262
+ gr.Markdown(
263
+ f"""
264
  # Get your 2023 Hackathon Certificate πŸŽ“
265
  The certification process is completely free:
266
  - To get a *certificate of completion*: you need to **contribute to at least one model, dataset, or space**.
 
269
  For more information about the certification process [check the hackathon page on certification](https://pseudo-lab.github.io/huggingface-hackathon23/submit.html#certification).
270
 
271
  Don't hesitate to share your certificate on Twitter (tag me [@wonhseo](https://twitter.com/wonhseo), [@pseudolab](https://twitter.com/pseudo-lab), and [@huggingface](https://twitter.com/huggingface)) and on LinkedIn.
272
+ """
273
+ )
274
+
275
+ hf_username = gr.Textbox(
276
+ placeholder="wseo", label="Your Hugging Face Username (case sensitive)"
277
+ )
278
  first_name = gr.Textbox(placeholder="Wonhyeong", label="Your First Name")
279
  last_name = gr.Textbox(placeholder="Seo", label="Your Last Name")
280
 
 
285
  output_pdf = gr.File()
286
  output_img = gr.components.Image(type="pil")
287
 
288
+ check_progress_button.click(
289
+ fn=certification,
290
+ inputs=[hf_username, first_name, last_name],
291
+ outputs=[output_text, output_pdf, output_img, output_row],
292
+ )
293
 
294
+
295
+ demo.launch(debug=True)