"""Pydantic models for blind challenge submission data.""" from datetime import datetime, timezone from config import TRACK_NAMES from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator from utils import _safeify_username from uuid_utils.compat import uuid7 def new_submission_id() -> str: """Return a new submission ID: a UUIDv7, as a string. UUIDv7 IDs start with a millisecond timestamp, so they sort by creation time. The ID ends up in S3 keys and manifest filenames, which therefore list in submission order. ``uuid_utils.compat.uuid7`` is used because the standard library only gained ``uuid.uuid7()`` in Python 3.14; it returns a standard ``uuid.UUID``. IDs from before this change are UUIDv4 and remain valid. Returns ------- str The canonical 36-character string form of the UUID. """ return str(uuid7()) # Display name -> S3 track prefix, from config's TRACK_NAMES (which maps the other # way). Keeping this derived means renaming a track label can't desync the S3 layout. _TRACK_SLUGS: dict[str, str] = {name: track for track, name in TRACK_NAMES.items()} def _track_slug(track_name: str) -> str: """Return the S3 track prefix for a track's display name. Parameters ---------- track_name : str One of ``config.TRACK_NAMES``' display names. Returns ------- str The S3 track prefix, e.g. "regression". Raises ------ ValueError If ``track_name`` isn't a known track. """ try: return _TRACK_SLUGS[track_name] except KeyError: raise ValueError( f"Unknown track {track_name!r}; expected one of {sorted(_TRACK_SLUGS)}" ) from None class Submission(BaseModel): """A single competition submission. Serialisable to JSON for storage in S3 alongside the uploaded prediction file. All fields are stored; only non-private fields are ever surfaced on the leaderboard. Attributes ---------- model_config Pydantic model configuration. ``protected_namespaces=()`` disables Pydantic's "model_" protected-namespace warning, which would otherwise fire on fields like ``model_report_link``. submission_id : str Auto-generated UUIDv7 (time-ordered; see ``new_submission_id``), used as the S3 key component. submitted_at : datetime UTC timestamp of submission. username : str HuggingFace username, from the logged-in OAuth profile (used for deduplication). hf_user_id : str The account's permanent Hugging Face ID (the OAuth ``sub``), which stays the same if the account is renamed. hf_account_created_at : datetime | None When the Hugging Face account was created, for the organisers' audits. safe_username : str Sanitised username for use in S3 keys and file paths. user_alias : str Optional display alias for anonymous submissions. anonymous : bool If True, display user_alias on leaderboard instead of username. participant_name : str Real name — stored privately, never displayed. discord_username : str Discord handle — stored privately. email : str Contact email — stored privately. affiliation : str Institutional affiliation — stored privately. model_report_link : str URL to model report (required before deadline). used_proprietary_data : bool Whether proprietary data was used in training. open_source_code : bool Whether the participant's code is open-source and publicly available. track : str Competition track, as one of ``config.TRACK_NAMES``' display names. filename : str Original uploaded filename. s3_key : str Full S3 object key for the uploaded prediction file. Populated by submission_store.upload_submission() after upload. """ model_config = ConfigDict(protected_namespaces=()) # --- generated --- submission_id: str = Field(default_factory=new_submission_id) submitted_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) # --- public identity --- username: str hf_user_id: str = "" hf_account_created_at: datetime | None = None safe_username: str = "" user_alias: str = "" anonymous: bool = True # --- private contact --- participant_name: str = "" discord_username: str = "" email: str = "" affiliation: str = "" model_report_link: str = "" used_proprietary_data: bool = False open_source_code: bool = False # --- submission --- track: str filename: str s3_key: str = "" @field_validator("track") @classmethod def _known_track(cls, track: str) -> str: """Reject a track name that isn't one of config's (was a Literal).""" _track_slug(track) return track @model_validator(mode="after") def _populate_safe_username(self) -> "Submission": self.safe_username = _safeify_username(self.username) return self @property def display_name(self) -> str: """Name to show on the leaderboard.""" if self.anonymous and self.user_alias: return self.user_alias return self.username @property def s3_prefix(self) -> str: """S3 prefix for all objects belonging to this submission. Layout:: submissions/ regression/{username}/{submission_id}/ metadata.json predictions.parquet # or .csv classification/{username}/{submission_id}/ metadata.json predictions.parquet # or .csv structure/{username}/{submission_id}/ metadata.json structures.zip """ return ( f"submissions/{_track_slug(self.track)}/{self.safe_username}/" f"{self.submission_id}" )