File size: 2,307 Bytes
e76888b
 
 
a596e6e
e76888b
a596e6e
 
e76888b
 
 
 
 
a596e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e76888b
a596e6e
 
 
 
 
e76888b
a596e6e
e76888b
a596e6e
 
 
 
e76888b
a596e6e
e76888b
a596e6e
 
e76888b
a596e6e
 
e76888b
a596e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
# genesis/biosecurity.py
import os
import requests
from typing import Dict, Any
from datetime import datetime

from .providers import pubmed_fallback_search, run_deepseek_summary

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
NCBI_API_KEY = os.getenv("NCBI_API_KEY")
NCBI_EMAIL = os.getenv("NCBI_EMAIL")

RISK_KEYWORDS = {
    "low": ["harmless", "biosafety level 1", "safe", "non-pathogenic"],
    "medium": ["biosafety level 2", "BSL-2", "infectious", "containment"],
    "high": ["BSL-3", "BSL-4", "pandemic potential", "gain-of-function", "biosecurity concern"]
}

def score_biosecurity_risk(text: str) -> int:
    """Score risk based on keywords in AI report."""
    text_lower = text.lower()
    score = 0
    for word in RISK_KEYWORDS["low"]:
        if word in text_lower:
            score += 10
    for word in RISK_KEYWORDS["medium"]:
        if word in text_lower:
            score += 25
    for word in RISK_KEYWORDS["high"]:
        if word in text_lower:
            score += 50
    return min(score, 100)  # cap at 100

def run_biosecurity_scan(entity: str) -> Dict[str, Any]:
    """
    Run AI-powered biosecurity risk scan for a given biological entity.
    Includes AI assessment + PubMed literature.
    """
    try:
        # AI assessment
        prompt = f"""
        You are a synthetic biology & biosecurity expert.
        Assess the potential biosecurity risks of the following entity: {entity}.
        Classify its biosafety level, potential misuse, regulatory concerns, and safe handling guidelines.
        Respond with detailed analysis.
        """
        ai_report = run_deepseek_summary(prompt)

        # Risk score
        score = score_biosecurity_risk(ai_report)

        # PubMed citations
        citations = pubmed_fallback_search(entity, NCBI_API_KEY, NCBI_EMAIL)

        return {
            "entity": entity,
            "timestamp": datetime.utcnow().isoformat(),
            "risk_score": score,
            "report": ai_report,
            "citations": citations
        }
    except Exception as e:
        return {
            "entity": entity,
            "timestamp": datetime.utcnow().isoformat(),
            "error": str(e),
            "risk_score": 0,
            "report": "Error running biosecurity scan.",
            "citations": []
        }