"""Label-set format shared with the extension (see ../shared/labelset.example.json).""" from __future__ import annotations from typing import Literal from pydantic import BaseModel, Field, model_validator class Label(BaseModel): key: str rubric: str = "" color: str = "#1d9bf0" icon: str = "" action: Literal["mark", "collapse", "hide"] = "mark" class Example(BaseModel): text: str label: str class LabelSet(BaseModel): id: str = "custom" name: str = "" description: str = "" labels: list[Label] = Field(min_length=2) examples: list[Example] = [] none: bool = False threshold: float = 0.6 @model_validator(mode="after") def _check(self) -> "LabelSet": keys = [l.key for l in self.labels] if len(set(keys)) != len(keys): raise ValueError("duplicate label keys") unknown = {e.label for e in self.examples} - set(keys) if unknown: raise ValueError(f"examples use unknown labels: {sorted(unknown)}") return self class Author(BaseModel): handle: str | None = None class Post(BaseModel): id: str text: str author: Author | None = None parent_text: str | None = None class Result(BaseModel): label: str probabilities: dict[str, float] confidence: float backend: str class ClassifyRequest(BaseModel): labelset: LabelSet posts: list[Post] class ClassifyResponse(BaseModel): results: dict[str, Result]