wseo's picture
feat: check if user passed hackathon (#3)
fac922b
raw
history blame
No virus
8.98 kB
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 date
import time
import os
import sys
import pandas as pd
import json
api = HfApi()
HF_TOKEN = os.environ.get("HF_TOKEN")
# Public dataset repo containing the pdfs of already certified users
DATASET_REPO_URL = "https://huggingface.co/datasets/wseo/huggingface-krew-hackathon23"
CERTIFIED_USERS_FILENAME = "usernames.csv"
ORGANIZATION = "pseudolab"
def has_contributions(repo_type, hf_username, organization):
"""
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
"""
repo_list = {
'model': api.list_models,
'dataset': api.list_datasets,
'space': api.list_spaces
}
for repo in repo_list[repo_type](author=organization):
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
if all(get_hub_footprint(hf_username, ORGANIZATION)):
passed = True
certificate_type = "excellence"
elif any(get_hub_footprint(hf_username, ORGANIZATION)):
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(((863, 0), (863, 1400)), "gray")
# Date of certification
d.text((863, 336), f"HKH23-{username}", fill=(117,117,117), anchor="mm", font=username_font)
pdf = im.convert('RGB')
pdf.save('certificate.pdf')
return im, "./certificate.pdf"
def add_certified_user(hf_username, first_name, last_name, certificate_type):
"""
Add the certified user to the database
"""
print("ADD CERTIFIED USER")
repo = Repository(local_dir="usernames", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN)
repo.git_pull()
history = pd.read_csv(os.path.join("usernames", CERTIFIED_USERS_FILENAME))
# 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)
new_row = pd.DataFrame({'hf_username': hf_username, 'first_name': first_name, 'last_name': last_name, 'certificate_type': certificate_type, 'datetime': time.time()}, index=[0])
history = pd.concat([new_row, history[:]]).reset_index(drop=True)
history.to_csv(os.path.join("usernames", CERTIFIED_USERS_FILENAME), index=False)
repo.push_to_hub(commit_message="Update certified users list")
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 = """
Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰! \n
Since you contributed to models, datasets, and spaces- you get a Certificate of Excellence πŸŽ“. \n
You can download your certificate below ⬇️ \n
Don't hesitate to share your certificate image below on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab 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 = """
Congratulations, you successfully completed the 2023 Hackathon πŸŽ‰! \n
Since you contributed to at least one model, dataset, or space- you get a Certificate of Completion πŸŽ“. \n
You can download your certificate below ⬇️ \n
Don't hesitate to share your certificate image below on Twitter and Linkedin (you can tag me @wonhseo, @pseudolab and @huggingface) πŸ€— \n
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 get a certificate of completion.
For more information about the certification process, refer to the submit 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)
with gr.Blocks() as demo:
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**.
For more information about the certification process [check the hackathon page on certification](https://pseudo-lab.github.io/huggingface-hackathon23/submit.html#certification).
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()
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])
demo.launch(debug=True)