File size: 963 Bytes
8012660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import math
from dataclasses import dataclass


def sigmoid(value: float) -> float:
    if value >= 0:
        z = math.exp(-value)
        return 1.0 / (1.0 + z)
    z = math.exp(value)
    return z / (1.0 + z)


def clamp_probability(value: float) -> float:
    if not math.isfinite(value):
        raise ValueError("probability must be finite")
    return max(0.0, min(1.0, value))


@dataclass(frozen=True)
class ModelResult:
    detector_key: str
    model_version: str
    ai_probability: float
    raw_score: float
    latency_ms: int
    input_size: int

    def as_dict(self) -> dict[str, object]:
        return {
            "detector_key": self.detector_key,
            "model_version": self.model_version,
            "ai_probability": clamp_probability(self.ai_probability),
            "raw_score": self.raw_score,
            "latency_ms": self.latency_ms,
            "input_size": self.input_size,
        }