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 from functools import lru_cache hf_token = os.getenv("HF_TOKEN") # Lazy initialization of gradio_client to avoid event loop issues _gradio_client = None def get_gradio_client(): """Lazy initialization of the Gradio client to avoid asyncio event loop conflicts""" global _gradio_client if _gradio_client is None: _gradio_client = Client("https://ysharma-hackathon-certificate-html-to-image.hf.space/", token=hf_token) return _gradio_client def check_username_in_cell(cell_value, target_username): """ Check if target_username exists in a cell that may contain comma-separated usernames. Handles: "ysharma", "Gideon, jjin6573", "Lucian25, SJLee-0525, Sungjoon Lee", etc. """ if pd.isna(cell_value) or cell_value is None: return False # Split by comma and strip whitespace, then compare case-insensitively usernames_in_cell = [u.strip().lower() for u in str(cell_value).split(',')] return target_username.lower() in usernames_in_cell # HTML template for the certificate CERTIFICATE_HTML_TEMPLATE = """
Generate your personalized certificate of participation
Click to open LinkedIn with pre-filled details, then upload your certificate image.