Spaces:
Sleeping
Sleeping
Update genesis/api_clients/umls_api.py
Browse files- genesis/api_clients/umls_api.py +35 -23
genesis/api_clients/umls_api.py
CHANGED
@@ -1,30 +1,42 @@
|
|
1 |
# genesis/api_clients/umls_api.py
|
2 |
-
import requests
|
3 |
import os
|
|
|
4 |
|
5 |
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
r.raise_for_status()
|
13 |
-
# Extract TGT URL
|
14 |
-
return r.headers.get("location") or r.text.split('action="')[1].split('"')[0]
|
15 |
|
16 |
-
def
|
17 |
-
"""
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return r.json()
|
|
|
1 |
# genesis/api_clients/umls_api.py
|
|
|
2 |
import os
|
3 |
+
import requests
|
4 |
|
5 |
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
6 |
+
UMLS_AUTH_URL = "https://utslogin.nlm.nih.gov/cas/v1/api-key"
|
7 |
+
UMLS_SEARCH_URL = "https://uts-ws.nlm.nih.gov/rest/search/current"
|
8 |
+
|
9 |
+
def get_umls_ticket():
|
10 |
+
"""
|
11 |
+
Get a single-use ticket for UMLS API queries.
|
12 |
+
"""
|
13 |
+
if not UMLS_API_KEY:
|
14 |
+
raise ValueError("UMLS_API_KEY is missing in environment variables")
|
15 |
|
16 |
+
params = {"apikey": UMLS_API_KEY}
|
17 |
+
res = requests.post(UMLS_AUTH_URL, data=params)
|
18 |
+
res.raise_for_status()
|
19 |
+
return res.text.strip()
|
|
|
|
|
|
|
20 |
|
21 |
+
def search_umls(term: str, max_results: int = 10):
|
22 |
+
"""
|
23 |
+
Search UMLS Metathesaurus for concepts related to a term.
|
24 |
+
"""
|
25 |
+
ticket = get_umls_ticket()
|
26 |
+
params = {
|
27 |
+
"string": term,
|
28 |
+
"ticket": ticket,
|
29 |
+
"pageSize": max_results
|
30 |
+
}
|
31 |
+
res = requests.get(UMLS_SEARCH_URL, params=params)
|
32 |
+
res.raise_for_status()
|
33 |
+
data = res.json()
|
34 |
|
35 |
+
results = []
|
36 |
+
for result in data.get("result", {}).get("results", []):
|
37 |
+
results.append({
|
38 |
+
"name": result.get("name"),
|
39 |
+
"ui": result.get("ui"),
|
40 |
+
"rootSource": result.get("rootSource")
|
41 |
+
})
|
42 |
+
return results
|
|