MonishRaman commited on
Commit
fdade21
·
verified ·
1 Parent(s): 98edeea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -101
app.py CHANGED
@@ -1,148 +1,108 @@
1
  import gradio as gr
2
- import os
3
- import uuid
4
  import tempfile
5
- import urllib.parse
6
- from datasets import load_dataset
7
  from gradio_client import Client, handle_file
8
- from PIL import Image
9
- from certificate_upload_module import upload_user_certificate
10
 
11
  # ===============================
12
  # CONFIG
13
  # ===============================
14
 
15
- HTML_TO_PNG_SPACE_URL = "https://monishraman-hackathon-html-to-png.hf.space/"
16
- html_to_png_client = Client(HTML_TO_PNG_SPACE_URL)
17
-
18
- HF_TOKEN = os.getenv("HF_TOKEN")
19
 
20
  # ===============================
21
  # HTML TEMPLATE
22
  # ===============================
23
 
24
- CERTIFICATE_HTML_TEMPLATE = """<!DOCTYPE html>
 
25
  <html>
26
- <body style="margin:0;width:2000px;height:1414px;
27
- background:linear-gradient(135deg,#FF6B35,#FF8C00);
28
- font-family:Arial;display:flex;align-items:center;justify-content:center;">
29
- <div style="background:white;width:90%;height:90%;padding:60px;text-align:center;">
30
- <h1>Certificate of Participation</h1>
31
- <h2>{participant_name}</h2>
32
- <p>Project: {project_name}</p>
33
- <p>Track: {track_name}</p>
34
- <p>ID: {certificate_id}</p>
35
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  </body>
37
- </html>"""
 
38
 
39
  # ===============================
40
- # DATA HELPERS
41
  # ===============================
42
 
43
- def load_user_data(profile):
44
- if not profile or not HF_TOKEN:
45
- return None, None, "❌ Login required"
46
-
47
- dataset = load_dataset(
48
- "ysharma/hackathon-data-for-certificates",
49
- token=HF_TOKEN,
50
- split="train"
51
- )
52
-
53
- df = dataset.to_pandas()
54
- username = profile.username
55
-
56
- row = df[df["HF-username"] == username]
57
- if row.empty:
58
- return None, None, "❌ User not found in dataset"
59
 
60
- return row.iloc[0]["Space-Name"], row.iloc[0]["Hackathon-Track"], "✅ Data loaded"
61
 
62
- # ===============================
63
- # CORE LOGIC
64
- # ===============================
65
-
66
- def create_certificate(name, project, track, profile):
67
- if not profile:
68
- return None, "❌ Login required", ""
69
-
70
- cert_id = f"GRADIO2025-{uuid.uuid4().hex[:8].upper()}"
71
-
72
- html = CERTIFICATE_HTML_TEMPLATE \
73
- .replace("{participant_name}", name) \
74
- .replace("{project_name}", project) \
75
- .replace("{track_name}", track) \
76
- .replace("{certificate_id}", cert_id)
77
 
 
78
  with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w") as f:
79
  f.write(html)
80
  html_path = f.name
81
 
82
- png_path = html_to_png_client.predict(
 
83
  handle_file(html_path),
84
  api_name="/predict"
85
  )
86
 
87
- img = Image.open(png_path)
88
- upload_msg = upload_user_certificate(img, profile.username)[1]
89
-
90
- linkedin_url = generate_linkedin_url(cert_id)
91
-
92
- return png_path, upload_msg, linkedin_url
93
-
94
- # ===============================
95
- # LINKEDIN
96
- # ===============================
97
-
98
- def generate_linkedin_url(cert_id):
99
- params = {
100
- "startTask": "CERTIFICATION_NAME",
101
- "name": "Gradio Agents & MCP Hackathon 2025",
102
- "organizationName": "Hugging Face",
103
- "issueYear": "2025",
104
- "issueMonth": "6",
105
- "certId": cert_id
106
- }
107
- return "https://www.linkedin.com/profile/add?" + urllib.parse.urlencode(params)
108
 
109
  # ===============================
110
  # UI
111
  # ===============================
112
 
113
  with gr.Blocks(title="Hackathon Certificate Generator") as demo:
114
-
115
  gr.Markdown("# 🎓 Hackathon Certificate Generator")
116
 
117
- login = gr.LoginButton()
118
- status = gr.Markdown()
119
-
120
- name = gr.Textbox(label="Name")
121
  project = gr.Textbox(label="Project Name")
122
  track = gr.Textbox(label="Track")
123
 
124
- generate = gr.Button("Generate Certificate")
125
-
126
- output_file = gr.File()
127
- output_msg = gr.Markdown()
128
- linkedin = gr.Textbox(label="LinkedIn URL")
129
 
130
- def on_load(profile):
131
- if profile:
132
- p, t, s = load_user_data(profile)
133
- return f"✅ Logged in as {profile.username}", profile.name, p, t
134
- return "❌ Please log in", "", "", ""
135
-
136
- demo.load(
137
- on_load,
138
- inputs=None,
139
- outputs=[status, name, project, track]
140
- )
141
 
142
- generate.click(
143
- create_certificate,
144
  inputs=[name, project, track],
145
- outputs=[output_file, output_msg, linkedin]
146
  )
147
 
148
  demo.launch()
 
1
  import gradio as gr
 
 
2
  import tempfile
3
+ import uuid
 
4
  from gradio_client import Client, handle_file
 
 
5
 
6
  # ===============================
7
  # CONFIG
8
  # ===============================
9
 
10
+ SPACE_A_URL = "https://monishraman-hackathon-html-to-png.hf.space/"
11
+ client = Client(SPACE_A_URL)
 
 
12
 
13
  # ===============================
14
  # HTML TEMPLATE
15
  # ===============================
16
 
17
+ CERT_HTML = """
18
+ <!DOCTYPE html>
19
  <html>
20
+ <head>
21
+ <style>
22
+ body {{
23
+ width: 2000px;
24
+ height: 1414px;
25
+ margin: 0;
26
+ display: flex;
27
+ justify-content: center;
28
+ align-items: center;
29
+ background: linear-gradient(135deg,#FF6B35,#FF8C00);
30
+ font-family: Arial, sans-serif;
31
+ }}
32
+ .box {{
33
+ background: white;
34
+ padding: 80px;
35
+ text-align: center;
36
+ width: 85%;
37
+ height: 80%;
38
+ }}
39
+ h1 {{ font-size: 60px; }}
40
+ h2 {{ font-size: 48px; }}
41
+ p {{ font-size: 30px; }}
42
+ </style>
43
+ </head>
44
+ <body>
45
+ <div class="box">
46
+ <h1>Certificate of Participation</h1>
47
+ <h2>{name}</h2>
48
+ <p>Project: {project}</p>
49
+ <p>Track: {track}</p>
50
+ <p>Certificate ID: {cid}</p>
51
+ </div>
52
  </body>
53
+ </html>
54
+ """
55
 
56
  # ===============================
57
+ # CORE FUNCTION
58
  # ===============================
59
 
60
+ def generate_certificate(name, project, track):
61
+ if not name or not project or not track:
62
+ return None, "❌ All fields are required"
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ cert_id = f"CERT-{uuid.uuid4().hex[:8].upper()}"
65
 
66
+ html = CERT_HTML.format(
67
+ name=name,
68
+ project=project,
69
+ track=track,
70
+ cid=cert_id
71
+ )
 
 
 
 
 
 
 
 
 
72
 
73
+ # Save HTML
74
  with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w") as f:
75
  f.write(html)
76
  html_path = f.name
77
 
78
+ # Call Space A
79
+ png_path = client.predict(
80
  handle_file(html_path),
81
  api_name="/predict"
82
  )
83
 
84
+ return png_path, f"✅ Certificate generated successfully!\nID: {cert_id}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  # ===============================
87
  # UI
88
  # ===============================
89
 
90
  with gr.Blocks(title="Hackathon Certificate Generator") as demo:
 
91
  gr.Markdown("# 🎓 Hackathon Certificate Generator")
92
 
93
+ name = gr.Textbox(label="Participant Name")
 
 
 
94
  project = gr.Textbox(label="Project Name")
95
  track = gr.Textbox(label="Track")
96
 
97
+ generate_btn = gr.Button("Generate Certificate", variant="primary")
 
 
 
 
98
 
99
+ output_file = gr.File(label="Download Certificate")
100
+ status = gr.Markdown()
 
 
 
 
 
 
 
 
 
101
 
102
+ generate_btn.click(
103
+ generate_certificate,
104
  inputs=[name, project, track],
105
+ outputs=[output_file, status]
106
  )
107
 
108
  demo.launch()