Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import HfApi, hf_hub_download, Repository
|
3 |
+
from huggingface_hub.repocard import metadata_load
|
4 |
+
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 pandas as pd
|
12 |
+
import json
|
13 |
+
|
14 |
+
api = HfApi()
|
15 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
16 |
+
|
17 |
+
# Private dataset repo containing the list of already certified users
|
18 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/MariaK/audio-course"
|
19 |
+
CERTIFIED_USERS_FILENAME = "usernames.csv"
|
20 |
+
|
21 |
+
# Private space to check if a user has passed.
|
22 |
+
SPACE_ID = "MariaK/Check-Audio-Course-Progress"
|
23 |
+
|
24 |
+
|
25 |
+
def check_if_passed(username):
|
26 |
+
"""
|
27 |
+
Check if given user passed enough assignments
|
28 |
+
:param username: User HF username
|
29 |
+
"""
|
30 |
+
|
31 |
+
passed = False
|
32 |
+
certificate_type = ""
|
33 |
+
|
34 |
+
client = Client(SPACE_ID, hf_token=HF_TOKEN)
|
35 |
+
result = client.predict(username, fn_index=0)
|
36 |
+
with open(result) as json_data:
|
37 |
+
data = json.load(json_data)
|
38 |
+
|
39 |
+
df = pd.DataFrame(data['data'])
|
40 |
+
if len(df[df.iloc[:,0] == 'β
']) == 4:
|
41 |
+
passed = True
|
42 |
+
certificate_type = "excellence"
|
43 |
+
elif len(df[df.iloc[:,0] == 'β
']) == 3:
|
44 |
+
passed = True
|
45 |
+
certificate_type = "completion"
|
46 |
+
|
47 |
+
return passed, certificate_type
|
48 |
+
|
49 |
+
|
50 |
+
def generate_certificate(certificate_template, first_name, last_name):
|
51 |
+
"""
|
52 |
+
Generates certificate from the template
|
53 |
+
:param certificate_template: type of the certificate to generate
|
54 |
+
:param first_name: first name entered by user
|
55 |
+
:param last_name: last name entered by user
|
56 |
+
"""
|
57 |
+
|
58 |
+
im = Image.open(certificate_template)
|
59 |
+
d = ImageDraw.Draw(im)
|
60 |
+
|
61 |
+
name_font = ImageFont.truetype("Quattrocento-Regular.ttf", 100)
|
62 |
+
date_font = ImageFont.truetype("Quattrocento-Regular.ttf", 48)
|
63 |
+
|
64 |
+
name = str(first_name) + " " + str(last_name)
|
65 |
+
print("NAME", name)
|
66 |
+
|
67 |
+
# Debug line name
|
68 |
+
#d.line(((200, 740), (1800, 740)), "gray")
|
69 |
+
#d.line(((1000, 0), (1000, 1400)), "gray")
|
70 |
+
|
71 |
+
# Name
|
72 |
+
d.text((1000, 740), name, fill="black", anchor="mm", font=name_font)
|
73 |
+
|
74 |
+
# Debug line date
|
75 |
+
#d.line(((1500, 0), (1500, 1400)), "gray")
|
76 |
+
|
77 |
+
# Date of certification
|
78 |
+
d.text((1480, 1170), str(date.today()), fill="black", anchor="mm", font=date_font)
|
79 |
+
|
80 |
+
|
81 |
+
pdf = im.convert('RGB')
|
82 |
+
pdf.save('certificate.pdf')
|
83 |
+
|
84 |
+
return im, "./certificate.pdf"
|
85 |
+
|
86 |
+
|
87 |
+
def add_certified_user(hf_username, first_name, last_name, certificate_type):
|
88 |
+
"""
|
89 |
+
Add the certified user to the database
|
90 |
+
"""
|
91 |
+
|
92 |
+
print("ADD CERTIFIED USER")
|
93 |
+
repo = Repository(local_dir="usernames", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN)
|
94 |
+
repo.git_pull()
|
95 |
+
|
96 |
+
history = pd.read_csv(os.path.join("usernames", CERTIFIED_USERS_FILENAME))
|
97 |
+
|
98 |
+
# Check if this hf_username is already in our dataset:
|
99 |
+
check = history.loc[history['hf_username'] == hf_username]
|
100 |
+
if not check.empty:
|
101 |
+
history = history.drop(labels=check.index[0], axis=0)
|
102 |
+
|
103 |
+
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])
|
104 |
+
history = pd.concat([new_row, history[:]]).reset_index(drop=True)
|
105 |
+
|
106 |
+
history.to_csv(os.path.join("usernames", CERTIFIED_USERS_FILENAME), index=False)
|
107 |
+
repo.push_to_hub(commit_message="Update certified users list")
|
108 |
+
|
109 |
+
|
110 |
+
def create_certificate(passed, certificate_type, hf_username, first_name, last_name):
|
111 |
+
"""
|
112 |
+
Generates certificate, adds message, saves username of the certified user
|
113 |
+
:param passed: boolean whether the user passed enough assignments
|
114 |
+
:param certificate_type: type of the certificate - completion or excellence
|
115 |
+
:param first_name: first name entered by user
|
116 |
+
:param last_name: last name entered by user
|
117 |
+
"""
|
118 |
+
|
119 |
+
if passed and certificate_type == "excellence":
|
120 |
+
# Generate a certificate of excellence
|
121 |
+
certificate, pdf = generate_certificate("./certificate-excellence.png", first_name, last_name)
|
122 |
+
# Add this user to our database
|
123 |
+
add_certified_user(hf_username, first_name, last_name, certificate_type)
|
124 |
+
# Add a message
|
125 |
+
message = """
|
126 |
+
Congratulations, you successfully completed the Hugging Face Audio Course π! \n
|
127 |
+
Since you pass 100% of the hands-on you get a Certificate of Excellence π. \n
|
128 |
+
You can download your certificate below β¬οΈ \n
|
129 |
+
Don't hesitate to share your certificate image below on Twitter and Linkedin (you can tag me @mariakhalusova and @huggingface) π€
|
130 |
+
"""
|
131 |
+
elif passed and certificate_type == "completion":
|
132 |
+
|
133 |
+
# Generate a certificate of completion
|
134 |
+
certificate, pdf = generate_certificate("./certificate-completion.png", first_name, last_name)
|
135 |
+
# Add this user to our database
|
136 |
+
add_certified_user(hf_username, first_name, last_name, certificate_type)
|
137 |
+
# Add a message
|
138 |
+
message = """
|
139 |
+
Congratulations, you successfully completed the Hugging Face Deep Reinforcement Learning Course π! \n
|
140 |
+
Since you pass 80% of the hands-on you get a Certificate of Completion π. \n
|
141 |
+
You can download your certificate below β¬οΈ \n
|
142 |
+
Don't hesitate to share your certificate image below on Twitter and Linkedin (you can tag me @mariakhalusova and @huggingface) π€ \n
|
143 |
+
You can try to get a Certificate of Excellence if you pass 100% of the hands-on, don't hesitate to check which unit you didn't pass and update these models.
|
144 |
+
"""
|
145 |
+
else:
|
146 |
+
# Not passed yet
|
147 |
+
certificate = Image.new("RGB", (100, 100), (255, 255, 255))
|
148 |
+
pdf = "./fail.pdf"
|
149 |
+
|
150 |
+
# Add a message
|
151 |
+
message = """
|
152 |
+
You didn't pass the minimum of 3 out of 4 of the hands-on to get a certificate of completion.
|
153 |
+
For more information about the certification process [check the course page on certification](https://huggingface.co/learn/audio-course/chapter8/certification).
|
154 |
+
Use the [self-evaluation space](https://huggingface.co/spaces/MariaK/Check-my-progress-Audio-Course) to see which assignments have not been completed.
|
155 |
+
"""
|
156 |
+
return certificate, message, pdf
|
157 |
+
|
158 |
+
|
159 |
+
def certification(hf_username, first_name, last_name):
|
160 |
+
passed, certificate_type = check_if_passed(hf_username)
|
161 |
+
certificate, message, pdf = create_certificate(passed, certificate_type, hf_username, first_name, last_name)
|
162 |
+
print("MESSAGE", message)
|
163 |
+
|
164 |
+
if passed:
|
165 |
+
visible = True
|
166 |
+
else:
|
167 |
+
visible = False
|
168 |
+
|
169 |
+
return message, pdf, certificate, output_row.update(visible=visible)
|
170 |
+
|
171 |
+
with gr.Blocks() as demo:
|
172 |
+
gr.Markdown(f"""
|
173 |
+
# Get your Hugging Face Audio Course Certificate π
|
174 |
+
The certification process is completely free:
|
175 |
+
- To get a *certificate of completion*: you need to **pass 3 out of 4 hands-on assignments**.
|
176 |
+
- To get a *certificate of excellence*: you need to **pass 4 out of 4 hands-on assignments**.
|
177 |
+
For more information about the certification process [check the course page on certification](https://huggingface.co/learn/audio-course/chapter8/certification).
|
178 |
+
Don't hesitate to share your certificate on Twitter (tag me @mariakhalusova and @huggingface) and on LinkedIn.
|
179 |
+
""")
|
180 |
+
|
181 |
+
hf_username = gr.Textbox(placeholder="MariaK", label="Your Hugging Face Username (case sensitive)")
|
182 |
+
first_name = gr.Textbox(placeholder="Jane", label="Your First Name")
|
183 |
+
last_name = gr.Textbox(placeholder="Doe", label="Your Last Name")
|
184 |
+
|
185 |
+
check_progress_button = gr.Button(value="Check if I pass and get the certificate")
|
186 |
+
output_text = gr.components.Textbox()
|
187 |
+
|
188 |
+
with gr.Row(visible=True) as output_row:
|
189 |
+
output_pdf = gr.File()
|
190 |
+
output_img = gr.components.Image(type="pil")
|
191 |
+
|
192 |
+
check_progress_button.click(fn=certification, inputs=[hf_username, first_name, last_name], outputs=[output_text, output_pdf, output_img, output_row])
|
193 |
+
|
194 |
+
|
195 |
+
demo.launch(debug=True)
|