Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
burtenshaw commited on
Commit ·
8adf41a
1
Parent(s): 8fecf33
connect certificate app with dataset
Browse files1 unmodified line2345678910111213141550 unmodified lines6667686970717273747576777879808182838485868712 unmodified lines10010110210310410521 unmodified lines1271281291301311321331341 unmodified lineimport requestsfrom io import BytesIOfrom datetime import dateimport gradio as grfrom PIL import Image, ImageDraw, ImageFontfrom huggingface_hub import whoamifrom criteria import check_certification as check_certification_criteriafrom org import join_finishers_orgdef download_profile_picture(profile_url: str):"""Download profile picture from URL."""50 unmodified linesreturn None, None, None, Nonedef create_linkedin_button(username: str) -> str:"""Create LinkedIn 'Add to Profile' button HTML."""current_year = date.today().yearcurrent_month = date.today().month# URL encode the certificate URLcert_url = "https://huggingface.co/agents-course-finishers"linkedin_params = {"startTask": "CERTIFICATION_NAME","name": "Hugging Face Course Certificate",# "organizationId": "40479", # Hugging Face's LinkedIn Organization ID"organizationName": "Hugging Face","issueYear": str(current_year),"issueMonth": str(current_month),"certUrl": cert_url,"certId": username, # Using username as cert ID}12 unmodified lines"""def check_certification(token: gr.OAuthToken | None):"""Check certification status for logged-in user."""if token is None:21 unmodified linesprofile_url=profile_url,)# Add LinkedIn button for passed certificateslinkedin_button = create_linkedin_button(username)result_message = f"{result.message}\n\n{linkedin_button}"else:certificate_img = None1 unmodified line23456789101112131415161750 unmodified lines6869707172737475767778798081828384858687888912 unmodified lines10210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914021 unmodified lines1621631641651661671681691701711721 unmodified lineimport requestsfrom io import BytesIOfrom datetime import dateimport tempfileimport gradio as grfrom PIL import Image, ImageDraw, ImageFontfrom huggingface_hub import whoami, upload_filefrom criteria import check_certification as check_certification_criteriafrom org import join_finishers_orgCERTIFYING_ORG_LINKEDIN_ID = os.getenv("CERTIFYING_ORG_LINKEDIN_ID", "000000")def download_profile_picture(profile_url: str):"""Download profile picture from URL."""50 unmodified linesreturn None, None, None, Nonedef create_linkedin_button(username: str, cert_url: str | None) -> str:"""Create LinkedIn 'Add to Profile' button HTML."""current_year = date.today().yearcurrent_month = date.today().month# Use the dataset certificate URL if available, otherwise fallback to defaultcertificate_url = cert_url or "https://huggingface.co/agents-course-finishers"linkedin_params = {"startTask": "CERTIFICATION_NAME","name": "Hugging Face Course Certificate","organizationName": "Hugging Face","organizationId": CERTIFYING_ORG_LINKEDIN_ID,"organizationIdissueYear": str(current_year),"issueMonth": str(current_month),"certUrl": certificate_url,"certId": username, # Using username as cert ID}12 unmodified lines"""def upload_certificate_to_hub(username: str, certificate_img) -> str:"""Upload certificate to the dataset hub and return the URL."""# Save image to temporary filewith tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:certificate_img.save(tmp.name)try:# Upload file to hubrepo_id = "agents-course/certificates"path_in_repo = f"certificates/{username}/{date.today()}.png"upload_file(path_or_fileobj=tmp.name,path_in_repo=path_in_repo,repo_id=repo_id,repo_type="dataset",)# Construct the URL to the imagecert_url = (f"https://huggingface.co/datasets/{repo_id}/resolve/main/{path_in_repo}")# Clean up temp fileos.unlink(tmp.name)return cert_urlexcept Exception as e:print(f"Error uploading certificate: {e}")os.unlink(tmp.name)return Nonedef check_certification(token: gr.OAuthToken | None):"""Check certification status for logged-in user."""if token is None:21 unmodified linesprofile_url=profile_url,)# Upload certificate to hub and get URLcert_url = upload_certificate_to_hub(username, certificate_img)# Add LinkedIn button for passed certificateslinkedin_button = create_linkedin_button(username, cert_url)result_message = f"{result.message}\n\n{linkedin_button}"else:certificate_img = None
app.py+46-8