Spaces:
Sleeping
Sleeping
Update genesis/ontology.py
Browse files- genesis/ontology.py +38 -36
genesis/ontology.py
CHANGED
@@ -1,42 +1,44 @@
|
|
1 |
# genesis/ontology.py
|
2 |
-
|
3 |
-
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
expanded_terms = set()
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
if name and name.lower() != query.lower():
|
23 |
-
expanded_terms.add(name)
|
24 |
-
except Exception as e:
|
25 |
-
print(f"[Ontology] UMLS expansion failed: {e}")
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if label and label.lower() != query.lower():
|
38 |
-
expanded_terms.add(label)
|
39 |
-
except Exception as e:
|
40 |
-
print(f"[Ontology] BioPortal expansion failed: {e}")
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# genesis/ontology.py
|
2 |
+
"""
|
3 |
+
Ontology Expansion for GENESIS-AI
|
4 |
+
Uses UMLS & BioPortal to find related terms, synonyms, and hierarchies.
|
5 |
+
"""
|
6 |
|
7 |
+
import os
|
8 |
+
import requests
|
|
|
9 |
|
10 |
+
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
11 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
12 |
|
13 |
+
def expand_with_bioportal(term):
|
14 |
+
"""Expand term using BioPortal ontology search."""
|
15 |
+
try:
|
16 |
+
url = f"https://data.bioontology.org/search?q={term}&apikey={BIOPORTAL_API_KEY}"
|
17 |
+
r = requests.get(url)
|
18 |
+
r.raise_for_status()
|
19 |
+
data = r.json()
|
20 |
+
return list({res["prefLabel"] for res in data.get("collection", []) if "prefLabel" in res})
|
21 |
+
except Exception as e:
|
22 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
def expand_with_umls(term):
|
25 |
+
"""Expand term using UMLS API."""
|
26 |
+
try:
|
27 |
+
url = f"https://uts-ws.nlm.nih.gov/rest/search/current?string={term}&apiKey={UMLS_API_KEY}"
|
28 |
+
r = requests.get(url)
|
29 |
+
r.raise_for_status()
|
30 |
+
data = r.json()
|
31 |
+
return [res["name"] for res in data.get("result", {}).get("results", []) if "name" in res]
|
32 |
+
except Exception as e:
|
33 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
def expand_terms_with_ontology(term):
|
36 |
+
"""Combine BioPortal and UMLS expansions."""
|
37 |
+
bioportal_terms = expand_with_bioportal(term)
|
38 |
+
umls_terms = expand_with_umls(term)
|
39 |
+
results = set()
|
40 |
+
if isinstance(bioportal_terms, list):
|
41 |
+
results.update(bioportal_terms)
|
42 |
+
if isinstance(umls_terms, list):
|
43 |
+
results.update(umls_terms)
|
44 |
+
return list(results) if results else [term]
|