File size: 1,487 Bytes
043267b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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]