File size: 1,493 Bytes
d37698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Prediction and label-spec types for localization given labels."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any

from pydantic import BaseModel, Field


@dataclass(frozen=True, slots=True)
class LabelSpec:
    label: str
    multiplicity: int

    def to_dict(self) -> dict[str, Any]:
        return {"label": self.label, "multiplicity": self.multiplicity}

    @classmethod
    def from_dict(cls, raw: dict[str, Any]) -> LabelSpec:
        return cls(label=str(raw["label"]), multiplicity=int(raw["multiplicity"]))


@dataclass(frozen=True, slots=True)
class GoldSegment:
    start_sec: float
    end_sec: float
    label: str

    def to_dict(self) -> dict[str, Any]:
        return {
            "start_sec": float(self.start_sec),
            "end_sec": float(self.end_sec),
            "label": self.label,
        }

    @classmethod
    def from_dict(cls, raw: dict[str, Any]) -> GoldSegment:
        return cls(
            start_sec=float(raw["start_sec"]),
            end_sec=float(raw["end_sec"]),
            label=str(raw["label"]),
        )


class PredictedInterval(BaseModel):
    label_echo: str
    start_sec: float
    end_sec: float


class LabelPrediction(BaseModel):
    label: str
    intervals: list[PredictedInterval] = Field(default_factory=list)


class PredictionResult(BaseModel):
    """Structured model output for localization given labels."""

    labels: list[LabelPrediction] = Field(default_factory=list)