File size: 16,104 Bytes
ce9fae3
 
 
 
 
 
3433650
fdd5360
ce9fae3
 
585af8d
3433650
ce9fae3
 
b76ffcc
ce9fae3
 
 
 
585af8d
b76ffcc
 
ce9fae3
fac922b
 
3433650
b66fcc6
3433650
fac922b
26b4087
fac922b
 
 
 
 
0df7ae2
fac922b
 
fdd5360
 
 
fac922b
 
 
3433650
0df7ae2
fac922b
 
 
 
 
 
 
 
 
 
 
 
fdd5360
 
 
fac922b
 
 
ce9fae3
585af8d
ce9fae3
fac922b
 
ce9fae3
fdd5360
 
f7db876
ce9fae3
fac922b
3433650
a8ff208
fdd5360
 
a8ff208
fdd5360
 
ce9fae3
f7db876
ce9fae3
 
585af8d
ce9fae3
 
 
 
 
585af8d
ce9fae3
 
 
 
 
585af8d
 
fdd5360
ce9fae3
 
fdd5360
ce9fae3
fdd5360
 
 
ce9fae3
fdd5360
ce9fae3
585af8d
fdd5360
ce9fae3
 
fdd5360
ce9fae3
fdd5360
 
ce9fae3
 
 
 
b76ffcc
 
 
fdd5360
 
 
 
 
 
 
 
b76ffcc
 
 
 
 
 
ce9fae3
b76ffcc
 
 
 
fdd5360
 
 
 
 
 
b76ffcc
ce9fae3
b76ffcc
ce9fae3
b76ffcc
 
 
 
 
 
fdd5360
b76ffcc
 
 
bb20658
b76ffcc
 
 
 
fdd5360
b76ffcc
 
fdd5360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b76ffcc
fdd5360
b76ffcc
 
 
 
 
 
ce9fae3
 
fdd5360
ce9fae3
fdd5360
ce9fae3
 
585af8d
ce9fae3
 
 
 
f7db876
fdd5360
 
 
 
f7db876
fdd5360
f7db876
2dba9d3
fdd5360
 
 
d7e7281
f8f6fba
fdd5360
f7db876
fdd5360
f7db876
fdd5360
 
 
f7db876
fdd5360
f7db876
2dba9d3
fdd5360
 
 
d7e7281
3433650
7a64fcf
f7db876
 
 
 
fdd5360
f7db876
 
32e6fbc
b76ffcc
7a64fcf
f7db876
 
ce9fae3
 
 
fdd5360
 
 
 
 
 
 
 
 
 
 
 
ce9fae3
 
3433650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63c1123
3433650
 
 
 
 
fdd5360
3433650
 
 
 
 
fdd5360
3433650
 
 
 
 
 
 
ce9fae3
3433650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9fae3
 
3433650
 
 
 
 
fdd5360
3433650
 
 
 
 
 
 
 
 
61403df
3433650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdd5360
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import gradio as gr
from huggingface_hub import HfApi, hf_hub_download, Repository
from huggingface_hub.repocard import metadata_load
from gradio_client import Client
from PIL import Image, ImageDraw, ImageFont

from datetime import datetime, timezone, timedelta
import time

import os
import sys
from collections import defaultdict
import pandas as pd
import json
import shutil

api = HfApi()
HF_TOKEN = os.environ.get("HF_TOKEN")

# Public dataset repo containing the pdfs of already certified users
DATASET_REPO_URL = f"https://wseo:{HF_TOKEN}@huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023"
CERTIFIED_USERS_FILENAME = "certified.csv"

ORGANIZATION = "pseudolab"

START_DATE = datetime(2023, 10, 20, tzinfo=timezone(timedelta(hours=9)))
END_DATE = datetime(2023, 11, 11, tzinfo=timezone(timedelta(hours=9)))


def has_contributions(repo_type, hf_username, organization, likes=5):
    """
    Check if a user has contributions in the specified repository type.
    :param repo_type: A repo type supported by the Hub
    :param hf_username: HF Hub username
    :param organization: HF Hub organization
    :param likes: Minimum number of likes for a contribution to be considered
    """
    repo_list = {
        "model": api.list_models,
        "dataset": api.list_datasets,
        "space": api.list_spaces,
    }

    for repo in repo_list[repo_type](author=organization):
        if repo.likes < likes:
            continue
        commits = api.list_repo_commits(repo.id, repo_type=repo_type)
        if any(hf_username in commit.authors for commit in commits):
            return True
    return False


def get_hub_footprint(hf_username, organization):
    """
    Check the types of contributions a user has made.
    :param hf_username: HF Hub username
    :param organization: HF Hub organization
    """
    has_models = has_contributions("model", hf_username, organization)
    has_datasets = has_contributions("dataset", hf_username, organization)
    has_spaces = has_contributions("space", hf_username, organization)

    return (has_models, has_datasets, has_spaces)


def check_if_passed(hf_username):
    """
    Check if given user contributed to hackathon
    :param hf_username: HF Hub username
    """

    passed = False
    certificate_type = ""

    # If the user contributed to models, datasets and spaces then assign excellence
    has_models, has_datasets, has_spaces = get_hub_footprint(hf_username, ORGANIZATION)
    if all((has_models, has_datasets, has_spaces)):
        passed = True
        certificate_type = "excellence"
    elif any((has_models, has_datasets, has_spaces)):
        passed = True
        certificate_type = "completion"

    return passed, certificate_type


def generate_certificate(certificate_template, first_name, last_name, hf_username):
    """
    Generates certificate from the template
    :param certificate_template: type of the certificate to generate
    :param first_name: first name entered by user
    :param last_name: last name entered by user
    :param hf_username: Hugging Face Hub username entered by user
    """

    im = Image.open(certificate_template)
    d = ImageDraw.Draw(im)

    name_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 60)
    username_font = ImageFont.truetype("HeiseiMinchoStdW7.otf", 18)

    name = str(first_name) + " " + str(last_name)
    print("NAME", name)

    # Debug line name
    # d.line(((0, 419), (1000, 419)), "gray")
    # d.line(((538, 0), (538, 1400)), "gray")

    # Name
    d.text((538, 419), name, fill=(87, 87, 87), anchor="mm", font=name_font)

    # Debug line id
    # d.line(((815, 0), (815, 1400)), "gray")

    # Date of certification
    d.text((815, 327), f"HKH23-{hf_username}", fill=(117, 117, 117), font=username_font)

    pdf = im.convert("RGB")
    pdf.save("certificate.pdf")

    return im, "./certificate.pdf"


def create_initial_csv(path):
    """Create an initial CSV file with headers if it doesn't exist."""
    # Define the headers for our CSV file
    headers = [
        "hf_username",
        "first_name",
        "last_name",
        "certificate_type",
        "datetime",
        "pdf_path",
    ]
    # Create a new DataFrame with no data and these headers
    df = pd.DataFrame(columns=headers)
    # Save the DataFrame to a CSV file
    df.to_csv(path, index=False)


def add_certified_user(hf_username, first_name, last_name, certificate_type):
    """
    Add the certified user to the dataset and include their certificate PDF.
    """
    print("ADD CERTIFIED USER")
    repo = Repository(
        local_dir="data",
        clone_from=DATASET_REPO_URL,
        git_user="wseo",
        git_email="wonhseo.v@gmail.com",
    )
    repo.git_pull()

    csv_full_path = os.path.join("data", CERTIFIED_USERS_FILENAME)

    if not os.path.isfile(csv_full_path):
        create_initial_csv(csv_full_path)

    history = pd.read_csv(csv_full_path)

    # Check if this hf_username is already in our dataset:
    check = history.loc[history["hf_username"] == hf_username]
    if not check.empty:
        history = history.drop(labels=check.index[0], axis=0)

    pdfs_repo_path = os.path.join("data", "certificates")

    # Copy the PDF from its current location to the target directory in the repository
    pdf_repo_filename = f"{hf_username}.pdf"  # Create a specific name for the PDF file
    pdf_repo_path_full = os.path.join(pdfs_repo_path, pdf_repo_filename)

    # Create the pdfs directory if it doesn't exist
    os.makedirs(pdfs_repo_path, exist_ok=True)

    shutil.copy("./certificate.pdf", pdf_repo_path_full)  # Copy the file

    new_row = pd.DataFrame(
        {
            "hf_username": hf_username,
            "first_name": first_name,
            "last_name": last_name,
            "certificate_type": certificate_type,
            "datetime": time.time(),  # current time
            "pdf_path": pdf_repo_path_full[5:],  # relative path to the PDF within the repo
        },
        index=[0],
    )

    history = pd.concat([new_row, history[:]]).reset_index(drop=True)

    # Save the updated CSV
    history.to_csv(os.path.join("data", CERTIFIED_USERS_FILENAME), index=False)

    # Add the PDF and CSV changes to the repo and push
    repo.git_add()
    repo.push_to_hub(commit_message="Update certified users list and add PDF")


def create_certificate(passed, certificate_type, hf_username, first_name, last_name):
    """
    Generates certificate, adds message, saves username of the certified user
    :param passed: boolean whether the user passed enough assignments
    :param certificate_type: type of the certificate - completion or excellence
    :param hf_username: Hugging Face Hub username entered by user
    :param first_name: first name entered by user
    :param last_name: last name entered by user
    """

    if passed and certificate_type == "excellence":
        # Generate a certificate of
        certificate, pdf = generate_certificate(
            "./certificate-excellence.png", first_name, last_name, hf_username
        )
        # Add this user to our database
        add_certified_user(hf_username, first_name, last_name, certificate_type)
        # Add a message
        message = f"""
        Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰! 
        Since you contributed to models, datasets, and spaces- you get a Certificate of Excellence πŸŽ“.
        You can download your certificate below ⬇️
        https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/certificates/{hf_username}.pdf\n
        Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo, @pseudo-lab and @huggingface) πŸ€—
        
        """
    elif passed and certificate_type == "completion":
        # Generate a certificate of completion
        certificate, pdf = generate_certificate(
            "./certificate-completion.png", first_name, last_name, hf_username
        )
        # Add this user to our database
        add_certified_user(hf_username, first_name, last_name, certificate_type)
        # Add a message
        message = f"""
        Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰!
        Since you contributed to at least one model, dataset, or space- you get a Certificate of Completion πŸŽ“.
        You can download your certificate below ⬇️
        https://huggingface.co/datasets/pseudolab/huggingface-krew-hackathon2023/resolve/main/certificates/{hf_username}.pdf\n
        Don't hesitate to share your certificate link above on Twitter and Linkedin (you can tag me @wonhseo and @huggingface) πŸ€—
        You can try to get a Certificate of Excellence if you contribute to all types of repos, please don't hesitate to do so.
        """
    else:
        # Not passed yet
        certificate = Image.new("RGB", (100, 100), (255, 255, 255))
        pdf = "./fail.pdf"
        # Add a message
        message = """
          You didn't pass the minimum of one contribution to a repo *with 5 or more likes* to get a certificate of completion. 
          For more information about the certification process, refer to the hackathon page.
          If the results here differ from your contributions, make sure you moved your space to the pseudolab organization.
          """
    return certificate, message, pdf


def certification(hf_username, first_name, last_name):
    passed, certificate_type = check_if_passed(hf_username)
    certificate, message, pdf = create_certificate(
        passed, certificate_type, hf_username, first_name, last_name
    )
    print("MESSAGE", message)

    if passed:
        visible = True
    else:
        visible = False

    return message, pdf, certificate, output_row.update(visible=visible)


def make_clickable_repo(name, repo_type):
    if repo_type == "space":
        link = "https://huggingface.co/" + "spaces/" + name
    elif repo_type == "model":
        link = "https://huggingface.co/"  + name
    elif repo_type == "dataset":
        link = "https://huggingface.co/" + "datasets/" + name
    return f'<a target="_blank" href="{link}">{name.split("/")[-1]}</a>'


def make_clickable_user(user_id):
    link = "https://huggingface.co/" + user_id
    return f'<a  target="_blank" href="{link}">{user_id}</a>'


def leaderboard():
    """
    Get the leaderboard of the hackathon.
    
    The leaderboard is a Pandas DataFrame with the following columns:
    - Rank: the rank of the user in the leaderboard
    - User: the Hugging Face username of the user
    - Contributions: the list of contributions of the user (models, datasets, spaces)
    - Likes: the total number of likes of the user's contributions
    """
    repo_list = {
        'model': api.list_models,
        'dataset': api.list_datasets,
        'space': api.list_spaces
    }

    # Repos that should not be included in the leaderboard
    not_included = [
        'README',
        '2023-Hackathon-Certification',
        'huggingface-krew-hackathon2023',
        # template repos as well
    ]

    contributions = defaultdict(list)

    for repo_type in repo_list:
        for repo in repo_list[repo_type](author=ORGANIZATION):
            if repo.id.split('/')[-1] in not_included:
                continue
            commits = api.list_repo_commits(repo.id, repo_type=repo_type)
            for author in set(author for commit in commits if START_DATE < commit.created_at < END_DATE for author in commit.authors):
                contributions[author].append((repo_type, repo.id, repo.likes))

    leaderboard = []
    for user, repo_likes in contributions.items():
        repos = []
        user_likes = 0
        for repo_type, repo, likes in repo_likes:
            repos.append(make_clickable_repo(repo, repo_type))
            user_likes += likes
        leaderboard.append([make_clickable_user(user), '- ' + '\n- '.join(repos), user_likes])
    
    df = pd.DataFrame(data=leaderboard, columns=["User πŸ‘€", "Contributions πŸ› οΈ", "Likes ❀️"])
    df.sort_values(by=["Likes ❀️"], ascending=False, inplace=True)
    df.insert(0, "Rank πŸ†", list(range(1, len(df) + 1)))
    
    return df


with gr.Blocks() as demo:
    gr.Markdown(
        '<img style="display: block; margin-left: auto; margin-right: auto; height: 10em;"'
            ' src="file/hfkr_logo.png"/>\n\n'
        '<h1 style="text-align: center;">Hugging Face KREW Hackathon 2023: Everyday AI</h1>'
    )
    with gr.Row():
        with gr.Column() as certificate_column:
            gr.Markdown(
                f"""
                ## Get your 2023 Hackathon Certificate πŸŽ“
                The certification process is completely free:
                - To get a *certificate of completion*: you need to **contribute to at least one model, dataset, or space**.
                - To get a *certificate of excellence*: you need to **contribute to models, datasets, and spaces**. *(Yes, all three!)*
                
                For more information about the certification process [check the hackathon page on certification](https://pseudo-lab.github.io/huggingface-hackathon23/ko/tutorials/index.html).
                Don't hesitate to share your certificate on Twitter (tag me [@wonhseo](https://twitter.com/wonhseo) and [@huggingface](https://twitter.com/huggingface)) and on LinkedIn.
                """
            )

            hf_username = gr.Textbox(
                placeholder="wseo", label="Your Hugging Face Username (case sensitive)"
            )
            first_name = gr.Textbox(placeholder="Wonhyeong", label="Your First Name")
            last_name = gr.Textbox(placeholder="Seo", label="Your Last Name")

            check_progress_button = gr.Button(value="Check if I pass and get the certificate")
            output_text = gr.components.Textbox(label="Your Result")

            with gr.Row(visible=True) as output_row:
                output_pdf = gr.File()
                output_img = gr.components.Image(type="pil")

            check_progress_button.click(
                fn=certification,
                inputs=[hf_username, first_name, last_name],
                outputs=[output_text, output_pdf, output_img, output_row],
            )
        with gr.Column() as leaderboard_column:
            gr.Markdown(
                f"""
                ## ❀️ Leaderboard
                The leaderboard showcases your contributions for easy sharing on SNS platforms:
                
                - Event #1: *1 repo 1 share* - **share your contributions to the world!**
                - (more on the way!)
                
                For more information about the offline event [check our event-us page](https://event-us.kr/huggingfacekrew/event/72612).
                Don't hesitate to share your contributions on Twitter (tag me [@wonhseo](https://twitter.com/wonhseo) and [@huggingface](https://twitter.com/huggingface)) and on LinkedIn.

                <a class="twitter-share-button" data-size="large"
                   data-text="I'm participating in the Hugging Face KREW Hackathon 2023: Everyday AI! @wonhseo @huggingface"
                   data-url="https://huggingface.co/spaces/pseudolab/2023-Hackathon-Certification"
                   data-hashtags="huggingface,krewhackathon2023"
                   href="https://twitter.com/intent/tweet">
                Tweet</a>
                """
            )
            with gr.Row():
                repos_data = gr.components.Dataframe(
                    type="pandas", datatype=["number", "markdown", "markdown", "number"]
                )
            with gr.Row():
                data_run = gr.Button("Refresh")
                data_run.click(
                    leaderboard, outputs=repos_data
                )

    scripts = """
    async () => {
        const twitter = await import("https://platform.twitter.com/widgets.js");
        globalThis.twitter = twitter;
    }
    """
    
    demo.load(leaderboard, outputs=repos_data)
    demo.load(None, None, None, _js=scripts)

demo.launch(debug=True)