burtenshaw commited on
Commit
8adf41a
·
1 Parent(s): 8fecf33

connect certificate app with dataset

Browse files
Files changed (1) hide show
  1. app.py +46 -8
app.py CHANGED
@@ -2,14 +2,16 @@ import os
2
  import requests
3
  from io import BytesIO
4
  from datetime import date
 
5
 
6
  import gradio as gr
7
  from PIL import Image, ImageDraw, ImageFont
8
- from huggingface_hub import whoami
9
 
10
  from criteria import check_certification as check_certification_criteria
11
  from org import join_finishers_org
12
 
 
13
 
14
  def download_profile_picture(profile_url: str):
15
  """Download profile picture from URL."""
@@ -66,22 +68,22 @@ def get_user_info(oauth_token):
66
  return None, None, None, None
67
 
68
 
69
- def create_linkedin_button(username: str) -> str:
70
  """Create LinkedIn 'Add to Profile' button HTML."""
71
  current_year = date.today().year
72
  current_month = date.today().month
73
 
74
- # URL encode the certificate URL
75
- cert_url = "https://huggingface.co/agents-course-finishers"
76
 
77
  linkedin_params = {
78
  "startTask": "CERTIFICATION_NAME",
79
  "name": "Hugging Face Course Certificate",
80
- # "organizationId": "40479", # Hugging Face's LinkedIn Organization ID
81
  "organizationName": "Hugging Face",
82
- "issueYear": str(current_year),
 
83
  "issueMonth": str(current_month),
84
- "certUrl": cert_url,
85
  "certId": username, # Using username as cert ID
86
  }
87
 
@@ -100,6 +102,39 @@ def create_linkedin_button(username: str) -> str:
100
  """
101
 
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  def check_certification(token: gr.OAuthToken | None):
104
  """Check certification status for logged-in user."""
105
  if token is None:
@@ -127,8 +162,11 @@ def check_certification(token: gr.OAuthToken | None):
127
  profile_url=profile_url,
128
  )
129
 
 
 
 
130
  # Add LinkedIn button for passed certificates
131
- linkedin_button = create_linkedin_button(username)
132
  result_message = f"{result.message}\n\n{linkedin_button}"
133
  else:
134
  certificate_img = None
 
2
  import requests
3
  from io import BytesIO
4
  from datetime import date
5
+ import tempfile
6
 
7
  import gradio as gr
8
  from PIL import Image, ImageDraw, ImageFont
9
+ from huggingface_hub import whoami, upload_file
10
 
11
  from criteria import check_certification as check_certification_criteria
12
  from org import join_finishers_org
13
 
14
+ CERTIFYING_ORG_LINKEDIN_ID = os.getenv("CERTIFYING_ORG_LINKEDIN_ID", "000000")
15
 
16
  def download_profile_picture(profile_url: str):
17
  """Download profile picture from URL."""
 
68
  return None, None, None, None
69
 
70
 
71
+ def create_linkedin_button(username: str, cert_url: str | None) -> str:
72
  """Create LinkedIn 'Add to Profile' button HTML."""
73
  current_year = date.today().year
74
  current_month = date.today().month
75
 
76
+ # Use the dataset certificate URL if available, otherwise fallback to default
77
+ certificate_url = cert_url or "https://huggingface.co/agents-course-finishers"
78
 
79
  linkedin_params = {
80
  "startTask": "CERTIFICATION_NAME",
81
  "name": "Hugging Face Course Certificate",
 
82
  "organizationName": "Hugging Face",
83
+ "organizationId": CERTIFYING_ORG_LINKEDIN_ID,
84
+ "organizationIdissueYear": str(current_year),
85
  "issueMonth": str(current_month),
86
+ "certUrl": certificate_url,
87
  "certId": username, # Using username as cert ID
88
  }
89
 
 
102
  """
103
 
104
 
105
+ def upload_certificate_to_hub(username: str, certificate_img) -> str:
106
+ """Upload certificate to the dataset hub and return the URL."""
107
+ # Save image to temporary file
108
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
109
+ certificate_img.save(tmp.name)
110
+
111
+ try:
112
+ # Upload file to hub
113
+ repo_id = "agents-course/certificates"
114
+ path_in_repo = f"certificates/{username}/{date.today()}.png"
115
+
116
+ upload_file(
117
+ path_or_fileobj=tmp.name,
118
+ path_in_repo=path_in_repo,
119
+ repo_id=repo_id,
120
+ repo_type="dataset",
121
+ )
122
+
123
+ # Construct the URL to the image
124
+ cert_url = (
125
+ f"https://huggingface.co/datasets/{repo_id}/resolve/main/{path_in_repo}"
126
+ )
127
+
128
+ # Clean up temp file
129
+ os.unlink(tmp.name)
130
+
131
+ return cert_url
132
+ except Exception as e:
133
+ print(f"Error uploading certificate: {e}")
134
+ os.unlink(tmp.name)
135
+ return None
136
+
137
+
138
  def check_certification(token: gr.OAuthToken | None):
139
  """Check certification status for logged-in user."""
140
  if token is None:
 
162
  profile_url=profile_url,
163
  )
164
 
165
+ # Upload certificate to hub and get URL
166
+ cert_url = upload_certificate_to_hub(username, certificate_img)
167
+
168
  # Add LinkedIn button for passed certificates
169
+ linkedin_button = create_linkedin_button(username, cert_url)
170
  result_message = f"{result.message}\n\n{linkedin_button}"
171
  else:
172
  certificate_img = None