Spaces:
Runtime error

File size: 1,910 Bytes
46085c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f2a085
 
 
46085c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import math

from app.core.config import Settings
from app.schemas import ModelScores, SeverityResult


def clamp(value: float) -> float:
    return max(0.0, min(1.0, value))


def repetition_score(prior_incident_count: int, decay: float) -> float:
    if prior_incident_count <= 0:
        return 0.0
    return clamp(1.0 - math.exp(-decay * prior_incident_count))


def aggression_score(scores: ModelScores) -> float:
    return clamp(
        max(
            scores.toxic,
            scores.severe_toxicity,
            scores.obscene,
            scores.threat,
            scores.insult,
        )
    )


def severity_level(score: float, settings: Settings) -> str:
    if score >= settings.risk_threshold_high:
        return "high"
    if score >= settings.risk_threshold_medium:
        return "medium"
    return "low"


def calculate_severity(
    scores: ModelScores,
    prior_incident_count: int,
    settings: Settings,
) -> SeverityResult:
    total = settings.risk_weight_total or 1.0
    intent = clamp(max(scores.identity_attack, scores.threat, scores.severe_toxicity))
    if scores.threat >= 0.8:
        intent = clamp(max(intent, scores.threat + 0.1))
    repetition = repetition_score(prior_incident_count, settings.repetition_decay)
    aggression = aggression_score(scores)
    weighted = (
        intent * settings.risk_weight_intent
        + repetition * settings.risk_weight_repetition
        + aggression * settings.risk_weight_aggression
    ) / total
    final_score = clamp(weighted)

    return SeverityResult(
        severity_score=round(final_score, 4),
        severity_level=severity_level(final_score, settings),  # type: ignore[arg-type]
        aggression_score=round(aggression, 4),
        intent_score=round(intent, 4),
        repetition_score=round(repetition, 4),
        prior_incident_count=prior_incident_count,
    )