docs: success message format

#5
by wseo - opened
Files changed (1) hide show
  1. app.py +107 -74
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="wonhseo.v@gmail.com")
 
 
 
 
 
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,29 @@ 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 +191,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 +202,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 +247,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 +272,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 +288,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="wonhseo.v@gmail.com",
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
  # Now, add a new entry to your CSV for this user and their PDF
170
+ new_row = pd.DataFrame(
171
+ {
172
+ "hf_username": hf_username,
173
+ "first_name": first_name,
174
+ "last_name": last_name,
175
+ "certificate_type": certificate_type,
176
+ "datetime": time.time(), # This captures the current time
177
+ "pdf_path": pdf_repo_path_full[
178
+ 5:
179
+ ], # This is the relative path to the PDF within the repo
180
+ },
181
+ index=[0],
182
+ )
183
+
184
  history = pd.concat([new_row, history[:]]).reset_index(drop=True)
185
+
186
  # Save the updated CSV
187
  history.to_csv(os.path.join("data", CERTIFIED_USERS_FILENAME), index=False)
188
 
 
191
  repo.push_to_hub(commit_message="Update certified users list and add PDF")
192
 
193
 
194
+ def create_certificate(passed, certificate_type, hf_username, first_name, last_name):
195
  """
196
+ Generates certificate, adds message, saves username of the certified user
197
  :param passed: boolean whether the user passed enough assignments
198
  :param certificate_type: type of the certificate - completion or excellence
199
  :param hf_username: Hugging Face Hub username entered by user
 
202
  """
203
 
204
  if passed and certificate_type == "excellence":
205
+ # Generate a certificate of
206
+ certificate, pdf = generate_certificate(
207
+ "./certificate-excellence.png", first_name, last_name, hf_username
208
+ )
209
  # Add this user to our database
210
+ add_certified_user(hf_username, first_name, last_name, certificate_type)
211
  # Add a message
212
  message = f"""
213
+ Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰!
214
+ Since you contributed to models, datasets, and spaces- you get a Certificate of Excellence πŸŽ“.
215
+ You can download your certificate below ⬇️
216
+ https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf\n
217
+ Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€—
218
+
219
  """
220
+ elif passed and certificate_type == "completion":
221
  # Generate a certificate of completion
222
+ certificate, pdf = generate_certificate(
223
+ "./certificate-completion.png", first_name, last_name, hf_username
224
+ )
225
  # Add this user to our database
226
+ add_certified_user(hf_username, first_name, last_name, certificate_type)
227
  # Add a message
228
  message = f"""
229
+ Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰!
230
+ Since you contributed to at least one model, dataset, or space- you get a Certificate of Completion πŸŽ“.
231
+ You can download your certificate below ⬇️
232
+ https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/pdfs/{hf_username}.pdf\n
233
+ Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€— \n
234
  You can try to get a Certificate of Excellence if you contribute to all types of repos, please don't hesitate to do so.
 
235
  """
236
  else:
237
  # Not passed yet
238
  certificate = Image.new("RGB", (100, 100), (255, 255, 255))
239
+ pdf = "./fail.pdf"
240
  # Add a message
241
  message = """
242
  You didn't pass the minimum of one contribution to get a certificate of completion.
 
247
 
248
 
249
  def certification(hf_username, first_name, last_name):
250
+ passed, certificate_type = check_if_passed(hf_username)
251
+ certificate, message, pdf = create_certificate(
252
+ passed, certificate_type, hf_username, first_name, last_name
253
+ )
254
+ print("MESSAGE", message)
255
+
256
+ if passed:
257
+ visible = True
258
+ else:
259
+ visible = False
260
+
261
+ return message, pdf, certificate, output_row.update(visible=visible)
262
 
 
 
 
 
 
 
263
 
264
  with gr.Blocks() as demo:
265
+ gr.Markdown(
266
+ f"""
267
  # Get your 2023 Hackathon Certificate πŸŽ“
268
  The certification process is completely free:
269
  - To get a *certificate of completion*: you need to **contribute to at least one model, dataset, or space**.
 
272
  For more information about the certification process [check the hackathon page on certification](https://pseudo-lab.github.io/huggingface-hackathon23/submit.html#certification).
273
 
274
  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.
275
+ """
276
+ )
277
+
278
+ hf_username = gr.Textbox(
279
+ placeholder="wseo", label="Your Hugging Face Username (case sensitive)"
280
+ )
281
  first_name = gr.Textbox(placeholder="Wonhyeong", label="Your First Name")
282
  last_name = gr.Textbox(placeholder="Seo", label="Your Last Name")
283
 
 
288
  output_pdf = gr.File()
289
  output_img = gr.components.Image(type="pil")
290
 
291
+ check_progress_button.click(
292
+ fn=certification,
293
+ inputs=[hf_username, first_name, last_name],
294
+ outputs=[output_text, output_pdf, output_img, output_row],
295
+ )
296
 
297
+
298
+ demo.launch(debug=True)