File size: 14,312 Bytes
794a9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c59f759
794a9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c59f759
794a9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c59f759
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
"""
Build Small Hackathon β€” Certificate Generator
=============================================
Workflow:
  1. User signs in with Hugging Face (OAuth).
  2. We take their HF username and look it up in the private eligibility dataset
     `build-small-hackathon/build-small-apps-for-certificates`
     (registered participants βˆͺ org Space contributors).
       β€’ username found WITH a space  -> autofill full name + project name
       β€’ username found WITHOUT a space -> autofill full name, no project
       β€’ username not found             -> show contact message, do not proceed
  3. All autofilled fields are editable.
  4. A button generates the certificate (HTML -> PNG via the renderer Space) and
     saves it to the public gallery dataset `build-small-hackathon/build-small-certificates`.
"""
import os
import uuid
import tempfile
import urllib.parse
from functools import lru_cache

import gradio as gr
import pandas as pd
from PIL import Image
from datasets import load_dataset
from gradio_client import Client, handle_file

from certificate_upload_module import upload_user_certificate

HF_TOKEN = os.getenv("HF_TOKEN")

ELIGIBILITY_DATASET = "build-small-hackathon/build-small-apps-for-certificates"
RENDERER_SPACE = "https://ysharma-hackathon-certificate-html-to-image.hf.space/"

DISCORD_INVITE = "https://discord.gg/92sEPT2Zhv"
CONTACT_EMAIL = "hello@gradio.app"

_TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "certificate_template.html")
with open(_TEMPLATE_PATH, encoding="utf-8") as _f:
    CERTIFICATE_HTML_TEMPLATE = _f.read()

PROJECT_SECTION_WITH_NAME = '<div class="project"><span class="lbl">Project</span> {project_name}</div>'
PROJECT_SECTION_EMPTY = ""


# ----------------------------------------------------------------------------- renderer client
_gradio_client = None


def get_gradio_client():
    """Lazy init of the HTML->image renderer client (avoids event-loop issues)."""
    global _gradio_client
    if _gradio_client is None:
        _gradio_client = Client(RENDERER_SPACE, hf_token=HF_TOKEN)
    return _gradio_client


# ----------------------------------------------------------------------------- eligibility lookup
@lru_cache(maxsize=1)
def _eligibility_index():
    """Load the eligibility dataset once into {username_lower: (full_name, space_name)}."""
    ds = load_dataset(ELIGIBILITY_DATASET, split="train", token=HF_TOKEN)
    df = ds.to_pandas().fillna("")
    index = {}
    for _, row in df.iterrows():
        uname = str(row.get("hf_username", "")).strip()
        if not uname:
            continue
        index[uname.lower()] = (
            str(row.get("full_name", "")).strip(),
            str(row.get("space_name", "")).strip(),
        )
    return index


def lookup_participant(username: str):
    """Return (state, full_name, space_name).

    state ∈ {"found_project", "found_no_project", "not_found"}.
    """
    if not username:
        return "not_found", "", ""
    try:
        index = _eligibility_index()
    except Exception as e:
        print(f"[ERROR] eligibility load failed: {e}")
        return "error", "", ""

    entry = index.get(username.strip().lower())
    if entry is None:
        return "not_found", "", ""
    full_name, space_name = entry
    if space_name:
        return "found_project", full_name, space_name
    return "found_no_project", full_name, ""


# ----------------------------------------------------------------------------- on login
def on_load(profile: gr.OAuthProfile | None):
    """Runs on page load. Drives which panel is shown and pre-fills the form."""
    if profile is None:
        return (
            "Please sign in with your Hugging Face account to continue.",
            gr.update(visible=False),   # main_interface
            gr.update(visible=False),   # contact_box
            "",                          # name
            gr.update(value="", visible=True),  # project
            "",                          # data_status
        )

    username = profile.username
    profile_name = profile.name or username
    state, full_name, space_name = lookup_participant(username)

    if state == "not_found":
        return (
            f"Signed in as **{username}**.",
            gr.update(visible=False),
            gr.update(visible=True),
            "",
            gr.update(value="", visible=True),
            "",
        )
    if state == "error":
        return (
            f"Signed in as **{username}**.",
            gr.update(visible=False),
            gr.update(visible=True),
            "",
            gr.update(value="", visible=True),
            "",
        )

    name_value = full_name or profile_name

    if state == "found_project":
        status = f"βœ… Found your submission β€” **{space_name}**. Review the details below, then generate."
        project_update = gr.update(value=space_name, visible=True)
    else:  # found_no_project
        status = (
            "βœ… You're on the participant list! We couldn't find a Space submission under your "
            "name β€” you can add a project below or leave it blank."
        )
        project_update = gr.update(value="", visible=True)

    return (
        f"Signed in as **{username}**.",
        gr.update(visible=True),
        gr.update(visible=False),
        name_value,
        project_update,
        status,
    )


# ----------------------------------------------------------------------------- LinkedIn helper
def generate_linkedin_url(participant_name, project_name):
    params = {
        "startTask": "CERTIFICATION_NAME",
        "name": "Build Small Hackathon 2026",
        "organizationName": "Hugging Face",
        "issueYear": "2026",
        "issueMonth": "6",
    }
    return "https://www.linkedin.com/profile/add?" + urllib.parse.urlencode(
        params, quote_via=urllib.parse.quote
    )


# ----------------------------------------------------------------------------- generate
def create_certificate(participant_name, project_name,
                       oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    if profile is None:
        return None, None, "❌ Please sign in first to generate your certificate.", ""

    username = profile.username
    state, _, _ = lookup_participant(username)
    if state == "not_found":
        return None, None, (
            "❌ We couldn't find you in the Build Small Hackathon records, so we can't issue a "
            f"certificate. Please reach out on [Discord]({DISCORD_INVITE}) or email {CONTACT_EMAIL}."
        ), ""
    if state == "error":
        return None, None, "❌ Couldn't reach the hackathon records right now. Please try again shortly.", ""

    participant_name = (participant_name or "").strip() or (profile.name or username)
    project_name = (project_name or "").strip()

    project_section = (
        PROJECT_SECTION_WITH_NAME.replace("{project_name}", project_name)
        if project_name else PROJECT_SECTION_EMPTY
    )

    html = CERTIFICATE_HTML_TEMPLATE.replace("{participant_name}", participant_name)
    html = html.replace("{project_section}", project_section)

    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".html", encoding="utf-8") as f:
        f.write(html)
        html_path = f.name

    try:
        client = get_gradio_client()
        image_path = client.predict(html_file=handle_file(html_path), api_name="/predict")[0]
    except Exception as e:
        return None, None, f"❌ Error generating certificate image: {e}", ""

    # Save to the public gallery dataset
    try:
        cert_image = Image.open(image_path)
        ok, msg = upload_user_certificate(cert_image, username)
        save_note = msg if ok else f"(generated β€” gallery save note: {msg})"
    except Exception as e:
        save_note = f"(generated β€” gallery save skipped: {e})"

    linkedin_url = generate_linkedin_url(participant_name, project_name)
    status = f"πŸŽ‰ Your certificate is ready, {participant_name}! {save_note}"
    return image_path, image_path, status, linkedin_url


def render_linkedin_button(url):
    if not url:
        return "<div style='padding:.5rem;color:var(--ink-soft)'>Generate your certificate to enable LinkedIn sharing.</div>"
    return f"""
    <a href="{url}" target="_blank" class="bs-linkedin">Add to LinkedIn profile β†’</a>
    <p style="margin-top:8px;font-size:.85rem;color:#6b6655">
      Opens LinkedIn with the certification pre-filled β€” then upload your downloaded image.</p>
    """


# ----------------------------------------------------------------------------- theming
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Archivo:wght@400;500;600;700;800;900&family=Spline+Sans+Mono:wght@400;500;600&display=swap');
:root{
  --paper:#f4eee1; --kraft:#e4d5b7; --kraft-deep:#d6c19a; --line:#cdbb95;
  --ink:#33312b; --ink-soft:rgba(51,49,43,.62); --forest:#3d6a55; --forest-ink:#20392d; --amber:#e0913a;
}
.gradio-container{max-width:880px !important;margin:auto !important;
  font-family:'Archivo',system-ui,sans-serif !important;background:var(--paper) !important;}
.gradio-container .prose, .gradio-container label, .gradio-container p{color:var(--ink) !important;}
.bs-hero{position:relative;overflow:hidden;background:var(--kraft);border:2px solid var(--ink);
  box-shadow:6px 6px 0 var(--ink);padding:30px 34px;margin-bottom:22px;}
.bs-hero .kick{font-family:'Spline Sans Mono',monospace;font-size:12px;letter-spacing:.24em;
  text-transform:uppercase;color:var(--forest);font-weight:600;}
.bs-hero h1{font-family:'Archivo';font-weight:900;font-stretch:120%;font-size:46px;line-height:.95;
  letter-spacing:-.02em;color:var(--ink);margin:8px 0 6px;}
.bs-hero p{color:#5a5446;font-size:15px;margin:0;}
.bs-section{font-family:'Spline Sans Mono',monospace;font-size:12px;letter-spacing:.18em;
  text-transform:uppercase;color:var(--ink-soft);font-weight:600;margin:6px 0 2px;display:flex;
  align-items:center;gap:9px;}
.bs-section::before{content:"";width:20px;height:2px;background:var(--amber);display:inline-block;}
button.primary, .bs-generate button{background:var(--forest) !important;border:2px solid var(--forest-ink) !important;
  color:#fff !important;border-radius:0 !important;font-family:'Archivo' !important;font-weight:800 !important;
  box-shadow:4px 4px 0 var(--forest-ink) !important;transition:transform .1s,box-shadow .1s !important;}
.bs-generate button:hover{transform:translate(2px,2px) !important;box-shadow:2px 2px 0 var(--forest-ink) !important;}
.bs-contact{background:#fbeee0;border:2px solid var(--amber);box-shadow:4px 4px 0 var(--ink);padding:18px 22px;}
.bs-contact a{color:var(--forest);font-weight:700;}
.bs-linkedin{display:inline-block;background:var(--ink);color:var(--paper) !important;text-decoration:none;
  font-weight:800;padding:11px 20px;border:2px solid var(--ink);box-shadow:3px 3px 0 var(--amber);}
input, textarea{border-radius:0 !important;border:2px solid var(--ink) !important;
  background:#fff !important;font-family:'Spline Sans Mono',monospace !important;}
footer{display:none !important;}
"""

BS_THEME = gr.themes.Base(
    primary_hue=gr.themes.colors.green,
    secondary_hue=gr.themes.colors.orange,
    neutral_hue=gr.themes.colors.stone,
    font=gr.themes.GoogleFont("Archivo"),
).set(button_large_radius="0px", button_small_radius="0px", block_radius="0px")


# ----------------------------------------------------------------------------- UI
with gr.Blocks(title="Build Small β€” Certificate Generator") as demo:
    gr.HTML(
        """
        <div class="bs-hero">
          <div class="kick">Hugging Face Γ— Gradio Β· 2026</div>
          <h1>Build Small β€” Certificate</h1>
          <p>Sign in with Hugging Face to claim your certificate of participation. Built something
             small, local, and yours? Let's make it official.</p>
        </div>
        """
    )

    with gr.Group():
        login_btn = gr.LoginButton(value="Sign in with Hugging Face")
        login_status = gr.Markdown("Please sign in with your Hugging Face account to continue.")

    contact_box = gr.HTML(
        f"""
        <div class="bs-contact">
          <strong>We couldn't find you in the Build Small Hackathon records.</strong><br>
          Certificates are issued to registered participants and Build Small org Space contributors.
          If you think this is a mistake, reach out on
          <a href="{DISCORD_INVITE}" target="_blank">Discord</a> or email
          <a href="mailto:{CONTACT_EMAIL}">{CONTACT_EMAIL}</a>.
        </div>
        """,
        visible=False,
    )

    with gr.Column(visible=False) as main_interface:
        gr.HTML('<div class="bs-section">Your details</div>')
        data_status = gr.Markdown("")
        participant_name = gr.Textbox(
            label="Full name",
            info="This appears on your certificate β€” edit if you'd like it shown differently.",
        )
        project_name = gr.Textbox(
            label="Project / Space name (optional)",
            info="Auto-filled from your Build Small submission. Leave blank for a certificate without a project.",
        )
        with gr.Row(elem_classes=["bs-generate"]):
            generate_btn = gr.Button("Generate my certificate", variant="primary", size="lg")

        gr.HTML('<div class="bs-section">Your certificate</div>')
        certificate_image = gr.Image(label="Preview", type="filepath", interactive=False)
        certificate_file = gr.File(label="Download (PNG)", interactive=False)
        generation_status = gr.Markdown("")

        gr.HTML('<div class="bs-section">Share</div>')
        linkedin_html = gr.HTML(render_linkedin_button(""))

    linkedin_state = gr.State("")

    demo.load(
        fn=on_load,
        inputs=None,
        outputs=[login_status, main_interface, contact_box, participant_name, project_name, data_status],
    )

    generate_btn.click(
        fn=create_certificate,
        inputs=[participant_name, project_name],
        outputs=[certificate_image, certificate_file, generation_status, linkedin_state],
    ).then(
        fn=lambda url: render_linkedin_button(url),
        inputs=[linkedin_state],
        outputs=[linkedin_html],
    )


if __name__ == "__main__":
    demo.launch(theme=BS_THEME, css=CUSTOM_CSS)