Spaces:
Sleeping
Sleeping
Create biosecurity.py
Browse files- genesis/biosecurity.py +105 -0
genesis/biosecurity.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# genesis/biosecurity.py
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
from datetime import datetime
|
5 |
+
from typing import Dict, Any, List
|
6 |
+
|
7 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
8 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
9 |
+
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
10 |
+
NCBI_API_KEY = os.getenv("NCBI_API_KEY")
|
11 |
+
NCBI_EMAIL = os.getenv("NCBI_EMAIL")
|
12 |
+
|
13 |
+
def search_pubmed_recent(query: str, max_results: int = 5) -> List[Dict[str, str]]:
|
14 |
+
"""Fetch recent PubMed papers for biosecurity context."""
|
15 |
+
try:
|
16 |
+
url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
|
17 |
+
params = {
|
18 |
+
"db": "pubmed",
|
19 |
+
"term": query,
|
20 |
+
"retmax": max_results,
|
21 |
+
"sort": "date",
|
22 |
+
"retmode": "json",
|
23 |
+
"api_key": NCBI_API_KEY,
|
24 |
+
"email": NCBI_EMAIL
|
25 |
+
}
|
26 |
+
r = requests.get(url, params=params, timeout=15)
|
27 |
+
r.raise_for_status()
|
28 |
+
ids = r.json().get("esearchresult", {}).get("idlist", [])
|
29 |
+
|
30 |
+
papers = []
|
31 |
+
if ids:
|
32 |
+
fetch_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
|
33 |
+
fetch_params = {
|
34 |
+
"db": "pubmed",
|
35 |
+
"id": ",".join(ids),
|
36 |
+
"retmode": "json",
|
37 |
+
"api_key": NCBI_API_KEY,
|
38 |
+
"email": NCBI_EMAIL
|
39 |
+
}
|
40 |
+
fr = requests.get(fetch_url, params=fetch_params, timeout=15)
|
41 |
+
fr.raise_for_status()
|
42 |
+
summaries = fr.json().get("result", {})
|
43 |
+
for pmid in ids:
|
44 |
+
if pmid in summaries:
|
45 |
+
papers.append({
|
46 |
+
"title": summaries[pmid].get("title", ""),
|
47 |
+
"url": f"https://pubmed.ncbi.nlm.nih.gov/{pmid}/"
|
48 |
+
})
|
49 |
+
return papers
|
50 |
+
except Exception as e:
|
51 |
+
print(f"[PubMed Error] {e}")
|
52 |
+
return []
|
53 |
+
|
54 |
+
def ai_biosecurity_assessment(entity: str) -> Dict[str, Any]:
|
55 |
+
"""Run AI-powered biosecurity risk assessment."""
|
56 |
+
import openai
|
57 |
+
openai.api_key = OPENAI_API_KEY
|
58 |
+
try:
|
59 |
+
prompt = f"""
|
60 |
+
You are a synthetic biology biosecurity officer.
|
61 |
+
Assess the biosecurity risk of the following entity: {entity}.
|
62 |
+
|
63 |
+
Consider:
|
64 |
+
- Is it a known dangerous pathogen, toxin, or dual-use technology?
|
65 |
+
- Potential misuse (bioterrorism, lab escape)
|
66 |
+
- Regulatory oversight and biosafety levels
|
67 |
+
- Recent trends in research or weaponization
|
68 |
+
- Ethical concerns
|
69 |
+
|
70 |
+
Return:
|
71 |
+
- Risk Score (0-100)
|
72 |
+
- Category (Low, Medium, High)
|
73 |
+
- Reasons
|
74 |
+
- Recommended Actions
|
75 |
+
"""
|
76 |
+
response = openai.ChatCompletion.create(
|
77 |
+
model="gpt-4o-mini",
|
78 |
+
messages=[{"role": "user", "content": prompt}],
|
79 |
+
temperature=0.2
|
80 |
+
)
|
81 |
+
return {"ai_report": response.choices[0].message["content"]}
|
82 |
+
except Exception as e:
|
83 |
+
print(f"[OpenAI Error] {e}")
|
84 |
+
return {"ai_report": "AI risk analysis unavailable."}
|
85 |
+
|
86 |
+
def run_biosecurity_scan(entity: str) -> Dict[str, Any]:
|
87 |
+
"""Main function to scan biosecurity risks."""
|
88 |
+
# Step 1: AI assessment
|
89 |
+
ai_results = ai_biosecurity_assessment(entity)
|
90 |
+
|
91 |
+
# Step 2: PubMed latest research
|
92 |
+
papers = search_pubmed_recent(entity)
|
93 |
+
|
94 |
+
# Step 3: Fake simple scoring logic (can be replaced with ontology check)
|
95 |
+
score = 85 if any(word in entity.lower() for word in ["smallpox", "anthrax", "ebola"]) else 30
|
96 |
+
category = "High" if score >= 70 else ("Medium" if score >= 40 else "Low")
|
97 |
+
|
98 |
+
return {
|
99 |
+
"entity": entity,
|
100 |
+
"score": score,
|
101 |
+
"category": category,
|
102 |
+
"ai_report": ai_results.get("ai_report", ""),
|
103 |
+
"pubmed_links": papers,
|
104 |
+
"timestamp": datetime.utcnow().isoformat()
|
105 |
+
}
|