Spaces:
Sleeping
Sleeping
Update genesis/biosecurity.py
Browse files- genesis/biosecurity.py +29 -62
genesis/biosecurity.py
CHANGED
@@ -1,70 +1,37 @@
|
|
1 |
# genesis/biosecurity.py
|
2 |
import os
|
3 |
import requests
|
4 |
-
from
|
5 |
-
from datetime import datetime
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
NCBI_API_KEY = os.getenv("NCBI_API_KEY")
|
11 |
-
NCBI_EMAIL = os.getenv("NCBI_EMAIL")
|
12 |
-
|
13 |
-
RISK_KEYWORDS = {
|
14 |
-
"low": ["harmless", "biosafety level 1", "safe", "non-pathogenic"],
|
15 |
-
"medium": ["biosafety level 2", "BSL-2", "infectious", "containment"],
|
16 |
-
"high": ["BSL-3", "BSL-4", "pandemic potential", "gain-of-function", "biosecurity concern"]
|
17 |
-
}
|
18 |
-
|
19 |
-
def score_biosecurity_risk(text: str) -> int:
|
20 |
-
"""Score risk based on keywords in AI report."""
|
21 |
-
text_lower = text.lower()
|
22 |
-
score = 0
|
23 |
-
for word in RISK_KEYWORDS["low"]:
|
24 |
-
if word in text_lower:
|
25 |
-
score += 10
|
26 |
-
for word in RISK_KEYWORDS["medium"]:
|
27 |
-
if word in text_lower:
|
28 |
-
score += 25
|
29 |
-
for word in RISK_KEYWORDS["high"]:
|
30 |
-
if word in text_lower:
|
31 |
-
score += 50
|
32 |
-
return min(score, 100) # cap at 100
|
33 |
-
|
34 |
-
def run_biosecurity_scan(entity: str) -> Dict[str, Any]:
|
35 |
"""
|
36 |
-
|
37 |
-
|
38 |
"""
|
39 |
-
|
40 |
-
|
41 |
-
prompt = f"""
|
42 |
-
You are a synthetic biology & biosecurity expert.
|
43 |
-
Assess the potential biosecurity risks of the following entity: {entity}.
|
44 |
-
Classify its biosafety level, potential misuse, regulatory concerns, and safe handling guidelines.
|
45 |
-
Respond with detailed analysis.
|
46 |
-
"""
|
47 |
-
ai_report = run_deepseek_summary(prompt)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
"
|
57 |
-
"
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
}
|
|
|
1 |
# genesis/biosecurity.py
|
2 |
import os
|
3 |
import requests
|
4 |
+
from genesis.utils.neo4j_utils import run_query
|
|
|
5 |
|
6 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
7 |
+
BIOPORTAL_BASE_URL = "https://data.bioontology.org"
|
8 |
|
9 |
+
def search_biosecurity_terms(query, pagesize=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
"""
|
11 |
+
Search BioPortal for biosecurity-related terms.
|
12 |
+
Stores results in Neo4j for knowledge graph building.
|
13 |
"""
|
14 |
+
headers = {"Authorization": f"apikey token={BIOPORTAL_API_KEY}"}
|
15 |
+
params = {"q": query, "pagesize": pagesize}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
try:
|
18 |
+
response = requests.get(f"{BIOPORTAL_BASE_URL}/search", headers=headers, params=params)
|
19 |
+
response.raise_for_status()
|
20 |
+
results = response.json().get("collection", [])
|
21 |
+
|
22 |
+
# Store in Neo4j
|
23 |
+
for r in results:
|
24 |
+
label = r.get("prefLabel", "Unknown")
|
25 |
+
uri = r.get("@id", "")
|
26 |
+
run_query(
|
27 |
+
"""
|
28 |
+
MERGE (t:BiosecurityTerm {uri: $uri})
|
29 |
+
SET t.label = $label, t.source = "BioPortal"
|
30 |
+
""",
|
31 |
+
{"uri": uri, "label": label}
|
32 |
+
)
|
33 |
+
|
34 |
+
return results
|
35 |
+
|
36 |
+
except requests.RequestException as e:
|
37 |
+
return {"error": str(e)}
|
|