File size: 4,890 Bytes
66c7fa8
f38a19c
66c7fa8
 
 
 
 
 
9d3e66c
a925989
66c7fa8
 
 
a925989
7eb55c5
 
 
bfa6d5d
3fe18b0
bfa6d5d
 
 
66c7fa8
 
 
91da203
66c7fa8
 
 
 
 
 
 
a925989
66c7fa8
 
 
31082cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30fe3e9
31082cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13d680a
6c52ec4
 
 
e25d4bd
92bfcb6
fa4f5a8
 
13d680a
fa4f5a8
 
 
13176b6
fa4f5a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fce5fdd
fa4f5a8
fce5fdd
 
fa4f5a8
e25d4bd
 
 
 
9d1966a
 
e25d4bd
 
 
 
 
 
596bdf8
 
e25d4bd
 
 
 
 
596bdf8
e25d4bd
 
 
19c64f0
 
e25d4bd
 
 
 
 
a925989
e25d4bd
 
fa4f5a8
 
 
 
 
e25d4bd
 
66c7fa8
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
import gradio as gr
from huggingface_hub import HfApi, Repository, whoami, snapshot_download
import json
import tempfile
import os

# Config
DATASET_REPO = "competition-private/teams"  
DATASET_FILE = "teams.json"
hf_token = os.environ.get("TOKEN")

# Authenticate HF user via Gradio

def submit_form(email, team, affiliation, student, oauth_token: gr.OAuthToken = None):
    # Use HF API with token
    api = HfApi()
    
    if oauth_token is None or oauth_token.token is None:
        raise gr.Error("You must be logged in to register!")
    
    user_info = api.whoami(token=oauth_token.token)
    hf_user_id = user_info['name']

    # Validate required fields
    if not email or not team or not student:
        raise gr.Error("Please fill all required fields.")

    # Prepare new record
    new_record = {
        "email": email,
        "team": team,
        "affiliation": affiliation or "",
        "student": student,
        "hf_id": hf_user_id
    }

    # Clone repo to temp folder
    # Step 1: Download snapshot of the dataset repo
    local_dir = snapshot_download(
        repo_id=DATASET_REPO,
        repo_type="dataset",
        use_auth_token=hf_token,
        local_dir=tempfile.mkdtemp(),  # So you can write to it
        local_dir_use_symlinks=False  # Important for modifying files
    )

    dataset_path = os.path.join(local_dir, DATASET_FILE)

    # Step 2: Load existing data
    if os.path.exists(dataset_path):
        with open(dataset_path, "r") as f:
            data = json.load(f)
    else:
        data = []

    # Step 3: Prevent duplicate submissions
    if any(entry["hf_id"] == hf_user_id for entry in data):
        return "You have already registered."

    data.append(new_record)

    # Step 4: Save updated file locally
    with open(dataset_path, "w") as f:
        json.dump(data, f, indent=2)

    # Step 5: Upload the updated file to the dataset repo
    api = HfApi()
    api.upload_file(
        path_or_fileobj=dataset_path,
        path_in_repo=DATASET_FILE,
        repo_id=DATASET_REPO,
        repo_type="dataset",
        token=hf_token,
        commit_message=f"Add registration for {hf_user_id}"
    )

    return "Registration successful! Now you need to join the competition's HF organization click on https://huggingface.co/organizations/e2lmc-competition/share/RyoEZjTiJQMpOLyTaHCsaTEFdRABGJkUrh"
    return f"""πŸš€ Registration successful!
πŸ‘‰ Now you need to join the competition's HF organization, [**join the competition organization**](https://huggingface.co/organizations/e2lmc-competition/share/RyoEZjTiJQMpOLyTaHCsaTEFdRABGJkUrh)
"""

def check_registration(oauth_token: gr.OAuthToken = None):
    if oauth_token is None or oauth_token.token is None:
        raise gr.Error("You must be logged in to register!")
    api = HfApi()
    
    user_info = api.whoami(token=oauth_token.token)
    hf_user_id = user_info['name']
    
    local_dir = snapshot_download(
        repo_id=DATASET_REPO,
        repo_type="dataset",
        use_auth_token=hf_token,
        local_dir=tempfile.mkdtemp(),
        local_dir_use_symlinks=False
    )
    dataset_path = os.path.join(local_dir, DATASET_FILE)

    if os.path.exists(dataset_path):
        with open(dataset_path, "r") as f:
            data = json.load(f)
    else:
        data = []

    for entry in data:
        if entry["hf_id"] == hf_user_id:
            return f"""βœ… You are registered in team: **{entry['team']}**

πŸ‘‰ To submit your system, [**join the competition organization**](https://huggingface.co/organizations/e2lmc-competition/share/RyoEZjTiJQMpOLyTaHCsaTEFdRABGJkUrh)
"""
    return "❌ You are not registered yet."

with gr.Blocks() as demo:
    gr.Markdown("### Registration Form (HuggingFace login required)")

    # gr.Markdown("Login to Hugging Face to register to E2LM Competition.")
    gr.Markdown("<span style='color:red'><strong>Note</strong>: You must log-in to Hugging Face to register to the competition.</span>")
    gr.LoginButton(min_width=250)
    
    # Hidden text to store HF username from auth
    hf_username = gr.Textbox(label="HF Username", visible=False)

    #with gr.Row():
    email = gr.Textbox(label="E-mail*", placeholder="[email protected]", lines=1)
    team = gr.Textbox(label="Team name*", lines=1)

    affiliation = gr.Textbox(label="Affiliation (optional)", lines=1)

    student = gr.Radio(
        choices=["Yes", "No"],
        label="Are you a student?*",
        value=None
    )

    #output = gr.Textbox(label="Status", interactive=False)
    output = gr.Markdown(label="Status")

    submit_btn = gr.Button("Submit")

    submit_btn.click(
        fn=submit_form,
        inputs=[email, team, affiliation, student],
        outputs=output
    )
    check_btn = gr.Button("Check Registration Status")
    check_btn.click(
        fn=check_registration,
        outputs=output
    )


demo.launch()