File size: 8,535 Bytes
60ece04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8037dc7
60ece04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8037dc7
60ece04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8037dc7
60ece04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import json
from datetime import datetime, timezone
from pathlib import Path

import gradio as gr
from huggingface_hub import snapshot_download
from loguru import logger
from pydantic import BaseModel, Field

from envs import API, REPO_ID, USERS_PATH, USERS_REPO
from formatting import styled_error, styled_message, styled_warning

css = """
:root {
    color-scheme: light !important;
    --block-border-width: 0 !important;
    --section-header-text-weight: 600 !important;
    --section-header-text-size: 14px !important;
    --input-radius: var(--radius-xl) !important;
    --font-mono: "Space Mono", monospace !important;
    --text-sm: 12px !important;
    --text-md: 14px !important;
    --text-lg: 16px !important;
    --input-text-size: var(--text-sm) !important;
    --body-text-size: 14px !important;
    --input-background-fill-focus: var(--secondary-300) !important;

    // Button Colors
    --button-primary-background-fill: var(--primary-800) !important;
    --button-secondary-background-fill: var(--secondary-600) !important;
    --checkbox-label-text-color: var(--body-text-color) !important;


    --card-bg-color: #fcecd4;
    --card-btn-color: #D4E4FC;
    --card-btn-color-hover: #7DAEF6;
    --answer-bg-color: #f0f8ff;
    --hover-border-color: #121212;
}

:root .dark {
    color-scheme: dark !important;
    --block-border-width: 0 !important;
    --section-header-text-weight: 600 !important;
    --section-header-text-size: 14px !important;
    --input-radius: var(--radius-xl) !important;
    --font-mono: "Space Mono", monospace !important;
    --text-sm: 12px !important;
    --text-md: 14px !important;
    --text-lg: 16px !important;
    --input-text-size: var(--text-sm) !important;
    --body-text-size: 14px !important;

    --button-primary-background-fill: var(--neutral-100) !important;
    --button-secondary-background-fill: var(--secondary-300) !important;
    --button-primary-text-color: black !important;
    --button-secondary-text-color: black !important;
    --checkbox-label-text-color: var(--body-text-color) !important;

    --card-bg-color: #383127;
    --answer-bg-color: #1a2b3c;
    --hover-border-color: #ffffff;
}

.gradio-app {

}
.warning-header {
    color: red;
}

.neutral-header {
    color: blue;
}

.success-header {
    color: green;
}

.gradio-container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 8px;
}
"""


def restart_space():
    API.restart_space(repo_id=REPO_ID)


def download_dataset_snapshot(repo_id, local_dir):
    try:
        logger.info(f"Downloading dataset snapshot from {repo_id} to {local_dir}")
        snapshot_download(
            repo_id=repo_id,
            local_dir=local_dir,
            repo_type="dataset",
            tqdm_class=None,
        )
    except Exception as e:
        logger.error(f"Error downloading dataset snapshot from {repo_id} to {local_dir}: {e}. Restarting space.")
        API.restart_space(repo_id=repo_id)


download_dataset_snapshot(USERS_REPO, USERS_PATH)


class User(BaseModel):
    """
    Represents a user in the competition system, formatted for HuggingFace datasets.
    """

    username: str = Field(description="HuggingFace username of the user")
    name: str = Field(description="Full name of the user")
    email: str = Field(description="Contact email of the user")
    affiliation: str = Field(description="Affiliation of the user")


def create_user(
    username: str,
    name: str,
    email: str,
    affiliation: str,
    profile: gr.OAuthProfile | None,
) -> User:
    """
    Create a user for a tossup model.

    Args:
        name: Display name of the submission
        description: Detailed description of what the submission does
        user_email: Email of the user who created the submission
        workflow: The workflow configuration for the tossup model

    Returns:
        Submission object if successful, None if validation fails
    """
    # Create the submission
    dt = datetime.now(timezone.utc)
    submission = User(
        username=username,
        name=name,
        email=email,
        affiliation=affiliation,
        created_at=dt.isoformat(),
    )

    return submission


def get_user(username: str) -> User | None:
    """
    Get a user from the registered users dataset.
    """
    out_path = Path(f"{USERS_PATH}/{username}.json")
    if not out_path.exists():
        return None
    with out_path.open("r") as f:
        user_dict = json.load(f)
    return User.model_validate(user_dict)


def is_user_logged_in(profile: gr.OAuthProfile | None) -> bool:
    """
    Check if a user is logged in.
    """
    return profile is not None


def user_signup(
    name: str,
    email: str,
    affiliation: str,
    profile: gr.OAuthProfile | None = None,
) -> User:
    """
    Sign up for the competition.
    """
    if profile is None:
        return styled_error("Please sign in using your HuggingFace account to register for the competition.")
    try:
        username = profile.username
        user = get_user(username)
        new_user = create_user(
            username=username,
            name=name,
            email=email,
            affiliation=affiliation,
            profile=profile,
        )
        # Convert to dictionary format
        user_dict = new_user.model_dump()

        # Upload to HuggingFace dataset
        updated = bool(user)
        API.upload_file(
            path_or_fileobj=json.dumps(user_dict, indent=2).encode(),
            path_in_repo=f"{username}.json",
            repo_id=USERS_REPO,
            repo_type="dataset",
            commit_message=f"{'Update' if updated else 'Add'} user {username}",
        )

        return styled_message(
            f"Successfully {'updated' if updated else 'registered'} user {username} for the competition!<br>"
        )

    except Exception as e:
        logger.exception(e)
        return styled_error(
            "Error registering for the competition. Please try again later, or contact the organizers at <br><a href='mailto:[email protected]'>[email protected]</a>."
        )


def load_user_info(profile: gr.OAuthProfile | None):
    if profile is None:
        return (
            gr.update(interactive=False),
            gr.update(interactive=False),
            gr.update(interactive=False),
            gr.update(interactive=False),
            gr.update(
                value="<h2 class='warning-header'>Please sign in using your HuggingFace account to register for the competition.</h2>",
            ),
        )
    user = get_user(profile.username)
    if user is None:
        return (
            gr.update(interactive=True),
            gr.update(interactive=True),
            gr.update(interactive=True),
            gr.update(interactive=True, value="Register"),
            gr.update(
                value="<h2 class='neutral-header'>Fill in your details below to register for the competition.</h2>",
            ),
        )
    return (
        gr.update(interactive=True, value=user.name),
        gr.update(interactive=True, value=user.email),
        gr.update(interactive=True, value=user.affiliation),
        gr.update(interactive=True, value="Update Information"),
        gr.update(
            value="<h2 class='success-header'>You are already registered! You can update your details below.</h2>",
        ),
    )


with gr.Blocks(title="Register - QANTA 2025 Quizbowl System Submission", css=css) as demo:
    gr.Markdown("# Register for QANTA 2025 Quizbowl System Submission")
    with gr.Row():
        with gr.Column():
            # HuggingFace Login Button
            hf_login_btn = gr.LoginButton(scale=1)
            header = gr.HTML("<h2>Fill in your details below</h2>")

            # Registration form
            name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
            email = gr.Textbox(label="Contact Email", placeholder="Enter your email")
            affiliation = gr.Textbox(
                label="Affiliation",
                placeholder="Enter your organization or institution",
            )

            submit_btn = gr.Button("Register")
            status = gr.Markdown()

            submit_btn.click(
                fn=user_signup,
                inputs=[name, email, affiliation],
                outputs=[status],
            )

    demo.load(
        load_user_info,
        inputs=[],
        outputs=[name, email, affiliation, submit_btn, header],
    )

demo.queue(default_concurrency_limit=40).launch()