File size: 26,501 Bytes
218c9af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import gradio as gr
import pandas as pd
import tempfile
import os
import uuid
import urllib.parse
from datasets import load_dataset
from datetime import datetime
from gradio_client import Client, handle_file
from certificate_upload_module import upload_user_certificate
from PIL import Image


hf_token = os.getenv("HF_TOKEN")

# HTML template for the certificate (unchanged)
CERTIFICATE_HTML_TEMPLATE = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gradio Agents & MCP Hackathon 2025 - Certificate of Participation</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700&family=Inter:wght@300;400;500;600&display=swap');
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            width: 2000px;
            height: 1414px;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        
        .certificate {
            width: 2000px;
            height: 1414px;
            background: linear-gradient(135deg, #FF6B35 0%, #F7931E 50%, #FF8C00 100%);
            position: relative;
            margin: 0;
            border-radius: 0;
            overflow: hidden;
            box-shadow: 0 33px 100px rgba(0,0,0,0.3);
            print-color-adjust: exact;
            -webkit-print-color-adjust: exact;
        }
        
        .certificate::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="20" cy="20" r="1" fill="white" opacity="0.1"/><circle cx="80" cy="40" r="1" fill="white" opacity="0.1"/><circle cx="40" cy="80" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>') repeat;
            opacity: 0.3;
        }
        
        .inner-border {
            position: absolute;
            top: 50px;
            left: 50px;
            right: 50px;
            bottom: 50px;
            border: 5px solid rgba(255,255,255,0.8);
            border-radius: 0;
            background: rgba(255,255,255,0.97);
        }
        
        .content {
            position: relative;
            padding: 100px 133px;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            text-align: center;
            z-index: 2;
        }
        
        .header {
            margin-bottom: 33px;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 33px;
            margin-bottom: 33px;
        }
        
        .gradio-logo {
            height: 66px;
        }
        
        .hf-logo {
            height: 58px;
        }
        
        .logo-text {
            font-size: 46px;
            font-weight: bold;
            color: #E85D04;
            font-family: 'Inter', sans-serif;
        }
        
        .event-title {
            font-family: 'Playfair Display', serif;
            font-size: 60px;
            font-weight: 700;
            color: #2d3748;
            margin-bottom: 17px;
            line-height: 1.2;
        }
        
        .certificate-type {
            font-family: 'Inter', sans-serif;
            font-size: 30px;
            color: #E85D04;
            font-weight: 500;
            letter-spacing: 3.3px;
            text-transform: uppercase;
        }
        
        .main-content {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin: 66px 0;
        }
        
        .certifies-text {
            font-family: 'Inter', sans-serif;
            font-size: 33px;
            color: #4a5568;
            margin-bottom: 33px;
        }
        
        .participant-name {
            font-family: 'Playfair Display', serif;
            font-size: 80px;
            font-weight: 700;
            color: #2d3748;
            margin: 33px 0;
            padding: 17px 0;
            border-bottom: 5px solid #E85D04;
            border-top: 2px solid #e2e8f0;
            background: linear-gradient(90deg, transparent 0%, rgba(232, 93, 4, 0.1) 50%, transparent 100%);
        }
        
        .description {
            font-family: 'Inter', sans-serif;
            font-size: 30px;
            color: #4a5568;
            line-height: 1.6;
            max-width: 1333px;
            margin: 50px auto;
        }
        
        .project-info {
            background: rgba(232, 93, 4, 0.1);
            padding: 33px;
            border-radius: 17px;
            margin: 33px 0;
            border-left: 7px solid #E85D04;
        }
        
        .project-title {
            font-family: 'Inter', sans-serif;
            font-size: 26px;
            color: #2d3748;
            font-weight: 600;
        }
        
        .track-info {
            font-family: 'Inter', sans-serif;
            font-size: 23px;
            color: #E85D04;
            margin-top: 8px;
        }
        
        .footer {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 66px;
            align-items: end;
            margin-top: 50px;
        }
        
        .date-section {
            text-align: left;
        }
        
        .signatures-section {
            text-align: center;
        }
        
        .verification-section {
            text-align: right;
        }
        
        .date, .certificate-id {
            font-family: 'Inter', sans-serif;
            font-size: 23px;
            color: #4a5568;
            margin-bottom: 8px;
        }
        
        .date-value, .id-value {
            font-family: 'Inter', sans-serif;
            font-size: 26px;
            font-weight: 600;
            color: #2d3748;
        }
        
        .signature-line {
            border-top: 3px solid #2d3748;
            width: 333px;
            margin: 33px auto 17px;
        }
        
        .signature-title {
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            color: #4a5568;
            text-transform: uppercase;
            letter-spacing: 1.7px;
        }
        
        .sponsors {
            margin-top: 66px;
            padding-top: 50px;
            border-top: 2px solid #e2e8f0;
        }
        
        .sponsors-title {
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            color: #4a5568;
            text-transform: uppercase;
            letter-spacing: 1.7px;
            margin-bottom: 25px;
        }
        
        .sponsor-logos {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: wrap;
            gap: 25px;
        }
        
        .sponsor-logo {
            background: #E85D04;
            color: white;
            padding: 13px 26px;
            border-radius: 33px;
            font-family: 'Inter', sans-serif;
            font-size: 20px;
            font-weight: 600;
        }
        
        .stats {
            display: flex;
            justify-content: center;
            gap: 50px;
            margin: 33px 0;
            font-family: 'Inter', sans-serif;
            font-size: 23px;
            color: #E85D04;
        }
        
        .stat {
            text-align: center;
        }
        
        .stat-number {
            font-weight: 700;
            font-size: 30px;
            color: #2d3748;
        }
        
        @media print {
            body { margin: 0; }
            .certificate { 
                margin: 0; 
                box-shadow: none;
                page-break-inside: avoid;
            }
        }
    </style>
</head>
<body>
    <div class="certificate">
        <div class="inner-border">
            <div class="content">
                <div class="header">
                    <div class="logo-section">
                        <img src="https://www.gradio.app/_app/immutable/assets/gradio-logo-with-title.3SNGTZpF.svg" alt="Gradio" class="gradio-logo">
                        <img src="https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo-with-title.svg" alt="Hugging Face" class="hf-logo">
                    </div>
                    <div class="event-title">Gradio Agents & MCP Hackathon 2025</div>
                    <div class="certificate-type">Certificate of Participation</div>
                </div>
                
                <div class="main-content">
                    <div class="certifies-text">This certifies that</div>
                    
                    <div class="participant-name" id="participantName">
                        {participant_name}
                    </div>
                    
                    <div class="description">
                        has successfully participated in and completed the <strong>Gradio Agents & MCP Hackathon 2025</strong>, 
                        a global developer event focused on building AI agents using Gradio and Model Context Protocol.
                    </div>
                    
                    <div class="project-info">
                        <div class="project-title">Project: <span id="projectTitle">{project_name}</span></div>
                        <div class="track-info">Track: <span id="trackName">{track_name}</span></div>
                    </div>
                    
                    <div class="stats">
                        <div class="stat">
                            <div class="stat-number">4,200+</div>
                            <div>Participants</div>
                        </div>
                        <div class="stat">
                            <div class="stat-number">630+</div>
                            <div>Submissions</div>
                        </div>
                        <div class="stat">
                            <div class="stat-number">$16,500</div>
                            <div>Total Prizes</div>
                        </div>
                    </div>
                </div>
                
                <div class="footer">
                    <div class="date-section">
                        <div class="date">Event Date</div>
                        <div class="date-value">June 2-8, 2025</div>
                    </div>
                    
                    <div class="signatures-section">
                        <div class="signature-line"></div>
                        <div class="signature-title">Gradio & Hugging Face Team</div>
                    </div>
                    
                    <div class="verification-section">
                        <div class="certificate-id">Certificate ID</div>
                        <div class="id-value" id="certificateId">{certificate_id}</div>
                    </div>
                </div>
                
                <div class="sponsors">
                    <div class="sponsors-title">Proudly Sponsored By</div>
                    <div class="sponsor-logos">
                        <div class="sponsor-logo">Anthropic</div>
                        <div class="sponsor-logo">OpenAI</div>
                        <div class="sponsor-logo">Nebius</div>
                        <div class="sponsor-logo">SambaNova</div>
                        <div class="sponsor-logo">LlamaIndex</div>
                        <div class="sponsor-logo">Hyperbolic</div>
                        <div class="sponsor-logo">Modal Labs</div>
                        <div class="sponsor-logo">Mistral AI</div>
                        <div class="sponsor-logo">Hugging Face</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>"""

gradio_client = Client("https://ysharma-hackathon-certificate-html-to-image.hf.space/",hf_token=hf_token)

def load_user_data(oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    """Load user data from the private dataset"""
    if not oauth_token or not profile:
        return None, None, "❌ Please log in first to access your certificate data."
    
    try:
        # Get username from profile
        username = profile.username if hasattr(profile, 'username') else profile.name
        
        # Get HF_TOKEN from environment variable
        hf_token = os.environ.get('HF_TOKEN')
        if not hf_token:
            return None, None, "❌ HF_TOKEN environment variable not set. Please contact administrators."
        
        # Load the private dataset
        dataset = load_dataset("ysharma/hackathon-data-for-certificates", token=hf_token, split="train")
        
        # Convert to pandas for easier filtering
        df = dataset.to_pandas()
        
        # Find user by HF username
        user_row = df[df['HF-username'] == username]
        
        if user_row.empty:
            return None, None, f"❌ Unable to find your project in the hackathon database. Username searched: {username}. Please contact the organizers on our Discord: https://discord.gg/Qe3jMKVczR"
        
        # Get user data
        user_data = user_row.iloc[0]
        project_name = user_data['Space-Name']
        track = user_data['Hackathon-Track']
        
        # Handle "No Track" case
        if track == "No Track":
            track = "Agent Demo Track"
        
        return project_name, track, f"βœ… Found your project: {project_name} in track: {track}"
        
    except Exception as e:
        return None, None, f"❌ Error loading data: {str(e)}. Please contact the organizers on our Discord: https://discord.gg/Qe3jMKVczR"

def show_login_status(profile: gr.OAuthProfile | None):
    """Display login status"""
    if profile:
        username = profile.username if hasattr(profile, 'username') else profile.name
        return f"βœ… Logged in as: {username}"
    else:
        return "❌ Please log in with your Hugging Face account"

def show_main_interface(profile: gr.OAuthProfile | None):
    """Show/hide main interface based on login status"""
    if profile:
        return gr.update(visible=True)
    else:
        return gr.update(visible=False)

def get_participant_name(profile: gr.OAuthProfile | None):
    """Get participant name from profile"""
    print(f">>>>>>inside get_participant_name :: profile: {profile}")
    if profile:
        name = profile.name if hasattr(profile, 'name') else ""
        username = profile.username if hasattr(profile, 'username') else ""
        return name, username
    return "", ""

def get_user_project_data(oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    """Get user project data for project name field"""
    if not oauth_token or not profile:
        return ""
    
    project_name, track, status = load_user_data(oauth_token, profile)
    return project_name or ""

def get_user_track_data(oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    """Get user track data for track field"""
    if not oauth_token or not profile:
        return ""
    
    project_name, track, status = load_user_data(oauth_token, profile)
    return track or ""

def get_data_status(oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    """Get data loading status message"""
    if not oauth_token or not profile:
        return "❌ Please log in first"
    
    project_name, track, status = load_user_data(oauth_token, profile)
    return status



def generate_linkedin_url(participant_name, project_name, track_name, certificate_id):
    """Generate LinkedIn Add to Profile URL with pre-populated certificate details"""
    
    # Certificate details for LinkedIn
    cert_name = "Gradio Agents & MCP Hackathon 2025"
    organization_name = "Hugging Face"
    issue_year = "2025"
    issue_month = "6"  # June
    
    # Build the LinkedIn URL parameters
    params = {
        'startTask': 'CERTIFICATION_NAME',
        'name': cert_name,
        'organizationName': organization_name,
        'issueYear': issue_year,
        'issueMonth': issue_month,
        'certId': certificate_id
    }
    
    # URL encode the parameters
    encoded_params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
    linkedin_url = f"https://www.linkedin.com/profile/add?{encoded_params}"
    
    return linkedin_url

def create_certificate(participant_name: str, project_name: str, track_name: str, 
                      oauth_token: gr.OAuthToken | None, profile: gr.OAuthProfile | None):
    """Generate the certificate HTML and create LinkedIn integration"""
    
    print(f">>>>>>1. Inside create_certificate :: profile: {profile}")
    if not oauth_token or not profile:
        return None, "❌ Please log in first to generate your certificate.", ""
    
    if not participant_name.strip():
        participant_name = profile.name if hasattr(profile, 'name') else "Participant"
    
    if not project_name.strip():
        return None, "❌ Please enter a project name.", ""
    
    if not track_name.strip():
        track_name = "Agent Demo Track"
    
    # Generate unique certificate ID
    certificate_id = f"GRADIO2025-{str(uuid.uuid4())[:8].upper()}"
    
    # Use string replacement instead of format to avoid CSS conflicts
    certificate_html = CERTIFICATE_HTML_TEMPLATE.replace("{participant_name}", participant_name)
    certificate_html = certificate_html.replace("{project_name}", project_name)
    certificate_html = certificate_html.replace("{track_name}", track_name)
    certificate_html = certificate_html.replace("{certificate_id}", certificate_id)
    
    # Save to a temporary HTML file
    with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.html', encoding='utf-8') as f:
        f.write(certificate_html)
        html_temp_path = f.name

    # Generate certificate image
    image_temp_path = gradio_client.predict(
        html_file=handle_file(html_temp_path),
        api_name="/predict"
    )[0]
    
    certificate_image = Image.open(image_temp_path)
    print(f">>>>>>2. Inside create_certificate :: profile: {profile}")
    _, hf_username = get_participant_name(profile)
    print(f">>>>>>hf_username: {hf_username}")
    
    # Upload certificate and get status
    upload_status = certificate_upload(certificate_image, hf_username)
    
    # Generate LinkedIn URL
    linkedin_url = generate_linkedin_url(participant_name, project_name, track_name, certificate_id)
    
    return image_temp_path, f"{upload_status}", linkedin_url

def certificate_upload(certificate_image, hf_username):
    """Upload to dataset and return status"""
    success, message = upload_user_certificate(certificate_image, hf_username)
    
    if success:
        return f"βœ… Certificate generated and saved! {message}"
    else:
        return f"⚠️ Certificate generated but upload failed: {message}"

# Create the Gradio interface
with gr.Blocks(title="Gradio Agents & MCP Hackathon 2025 - Certificate Generator", ) as demo:
    
    gr.Markdown("""
    # πŸŽ“ Gradio Agents & MCP Hackathon 2025
    ### Certificate Generator - Generate your personalized certificate of participation for the Gradio Agents & MCP Hackathon 2025!
    """)
    
    # Login section
    with gr.Row():
        with gr.Column():
            login_btn = gr.LoginButton(value="πŸ” Sign in with Hugging Face", variant="primary")
            login_status = gr.Markdown("❌ Please log in with your Hugging Face account")
    
    # Main interface (initially hidden)
    with gr.Column(visible=False) as main_interface:
        
        gr.Markdown("### πŸ“ Certificate Information")
        
        # Status message for data fetching
        data_status = gr.Markdown("")
        
        with gr.Row():
            with gr.Column():
                participant_name = gr.Textbox(
                    label="πŸ‘€ Participant Name",
                    placeholder="Your name (will be auto-filled from HF profile)",
                    info="This will appear on your certificate"
                )
                
                participant_username = gr.Textbox(
                    label="πŸ‘€ Participant username",
                    visible=False,
                )
                
                project_name = gr.Textbox(
                    label="πŸš€ Project Name",
                    placeholder="Your project name will be auto-loaded",
                    info="Edit this if you want to change the project name on your certificate"
                )
                
                track_name = gr.Textbox(
                    label="🎯 Hackathon Track",
                    placeholder="Your track will be auto-loaded",
                    info="Edit this if you want to change the track name on your certificate"
                )
        
        with gr.Row():
            generate_btn = gr.Button("πŸŽ“ Generate Certificate", variant="primary", size="lg")

        # Output section
        with gr.Row():
            with gr.Column():
                certificate_file = gr.File(
                    label="πŸ“„ Download Your Certificate",
                    type="filepath",
                    interactive=False
                )
                generation_status = gr.Markdown("")
        
        # LinkedIn integration section
        with gr.Row():
            with gr.Column():
                gr.Markdown("### πŸ”— LinkedIn Integration")
                
                linkedin_url_display = gr.Textbox(
                    label="LinkedIn Add to Profile URL",
                    placeholder="Generate your certificate first to get the LinkedIn URL",
                    interactive=False,
                    info="This URL will pre-populate your LinkedIn certification form"
                )
                
                linkedin_btn = gr.HTML("""
                    <div id="linkedin-button-container" style="margin-top: 10px;">
                        <p>Generate your certificate first to enable LinkedIn integration</p>
                    </div>
                """)
        
        gr.Markdown("""
        πŸ“‹ **How it works:**
        1. The app fetches your name, project name, and Hackathon track details. You can update this information. **Please ensure the details are accurate before proceeding to generate the certificate.**
        2. Click "Generate Certificate" to create your personalized certificate. You can't make multiple entries; regenerating gives a new image but doesn't replace it in the dataset. The dataset is mainly for records, so no need to worry if you have entered incorrect info initially.
        2. Download the certificate image file 
        3. Click "Add to LinkedIn Profile" - this opens LinkedIn with pre-filled details:
           - βœ… Certificate name: "Gradio Agents & MCP Hackathon 2025"
           - βœ… Organization: "Hugging Face"  
           - βœ… Issue date: June 2025
           - βœ… Certificate ID
        4. **Upload your certificate image** in the "Certification media" field
        5. **Add skills**: MCP, AI Agents, Gradio, Hugging Face
        6. Click "Add" to save to your LinkedIn profile!
        
        πŸ’‘ **Note**: LinkedIn doesn't allow automatic image uploads for security reasons, so you'll need to manually upload your certificate image.
        """, elem_id="instructions")
    
    # Hidden state to store LinkedIn URL
    linkedin_url_state = gr.State("")
    
    # Event handlers - using the correct OAuth pattern
    
    # Update login status
    demo.load(
        fn=show_login_status,
        inputs=None,
        outputs=[login_status]
    )
    
    # Show/hide main interface based on login
    demo.load(
        fn=show_main_interface,
        inputs=None,
        outputs=[main_interface]
    )
    
    # Auto-populate participant name
    demo.load(
        fn=get_participant_name,
        inputs=None,
        outputs=[participant_name, participant_username]
    )
    
    # Auto-populate project data
    demo.load(
        fn=get_user_project_data,
        inputs=None,
        outputs=[project_name]
    )
    
    # Auto-populate track data  
    demo.load(
        fn=get_user_track_data,
        inputs=None,
        outputs=[track_name]
    )
    
    # Show data status
    demo.load(
        fn=get_data_status,
        inputs=None,
        outputs=[data_status]
    )
       
    # Function to update LinkedIn button
    def update_linkedin_button(linkedin_url):
        if linkedin_url:
            return f"""
            <div style="margin-top: 10px;">
                <a href="{linkedin_url}" target="_blank" style="display: inline-block; text-decoration: none;">
                    <div style="background: #0077B5; color: white; padding: 12px 24px; border-radius: 8px; font-weight: 600; font-size: 16px; cursor: pointer; border: none; box-shadow: 0 2px 4px rgba(0,0,0,0.1); transition: background 0.2s;">
                        πŸ”— Add to LinkedIn Profile
                    </div>
                </a>
                <div style="margin-top: 8px; padding: 8px; background: #e3f2fd; border-radius: 6px; font-size: 12px; color: #1565c0;">
                    <strong>Ready to add to LinkedIn!</strong> Click the button to open LinkedIn with pre-filled details, then upload your certificate image manually.
                </div>
            </div>
            """
        else:
            return """
            <div style="margin-top: 10px;">
                <p style="color: #666; font-style: italic;">Generate your certificate first to enable LinkedIn integration</p>
            </div>
            """
    
    # Generate certificate button
    generate_btn.click(
        fn=create_certificate,
        inputs=[participant_name, project_name, track_name],
        outputs=[certificate_file, generation_status, linkedin_url_state]
    ).then(
        fn=lambda url: (url, update_linkedin_button(url)),
        inputs=[linkedin_url_state],
        outputs=[linkedin_url_display, linkedin_btn]
    )

demo.launch()