File size: 1,104 Bytes
2689723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d9338d
2689723
 
 
 
 
9d9338d
2689723
 
9d9338d
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
from __future__ import annotations
import re
from pydantic import BaseModel
from typing import List

DUAL_USE_PATTERNS = [
    r"step-?by-?step",
    r"protocol",
    r"wet[- ]?lab",
    r"culture\s*conditions",
    r"viral\s*vector",
    r"pathogen",
    r"gain[- ]of[- ]function",
    r"increase\s*virulence",
    r"synthesis\s*of\s*toxin",
    r"biosafety\s*level\s*(2|3|4)",
    r"kill\s*curve",
    r"CFU|colony\s*forming\s*units",
]

class SafetyDecision(BaseModel):
    allowed: bool
    rationale: str
    redactions: List[str] = []

class SafetyGuard:
    """Blocks operational/dual‑use outputs; allows high‑level literature review only."""
    def gate(self, text: str) -> SafetyDecision:
        hits = [p for p in DUAL_USE_PATTERNS if re.search(p, text, flags=re.I)]
        if hits:
            return SafetyDecision(
                allowed=False,
                rationale="Operational/dual‑use intent detected. Only high‑level review permitted.",
                redactions=hits,
            )
        return SafetyDecision(allowed=True, rationale="High‑level research intent.")