mgbam commited on
Commit
423d67b
·
verified ·
1 Parent(s): da2c28a

Update genesis/biosecurity.py

Browse files
Files changed (1) hide show
  1. 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 typing import Dict, Any
5
- from datetime import datetime
6
 
7
- from .providers import pubmed_fallback_search, run_deepseek_summary
 
8
 
9
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
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
- Run AI-powered biosecurity risk scan for a given biological entity.
37
- Includes AI assessment + PubMed literature.
38
  """
39
- try:
40
- # AI assessment
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
- # Risk score
50
- score = score_biosecurity_risk(ai_report)
51
-
52
- # PubMed citations
53
- citations = pubmed_fallback_search(entity, NCBI_API_KEY, NCBI_EMAIL)
54
-
55
- return {
56
- "entity": entity,
57
- "timestamp": datetime.utcnow().isoformat(),
58
- "risk_score": score,
59
- "report": ai_report,
60
- "citations": citations
61
- }
62
- except Exception as e:
63
- return {
64
- "entity": entity,
65
- "timestamp": datetime.utcnow().isoformat(),
66
- "error": str(e),
67
- "risk_score": 0,
68
- "report": "Error running biosecurity scan.",
69
- "citations": []
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)}