Spaces:
Sleeping
Sleeping
Update genesis/api_clients/pubmed_api.py
Browse files
genesis/api_clients/pubmed_api.py
CHANGED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# genesis/api_clients/pubmed_api.py
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
NCBI_API_KEY = os.getenv("NCBI_API_KEY") # Stored in HF Secrets
|
6 |
+
BASE_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"
|
7 |
+
|
8 |
+
def search_pubmed(query: str, max_results: int = 10):
|
9 |
+
"""Search PubMed articles."""
|
10 |
+
url = f"{BASE_URL}/esearch.fcgi"
|
11 |
+
params = {
|
12 |
+
"db": "pubmed",
|
13 |
+
"term": query,
|
14 |
+
"retmax": max_results,
|
15 |
+
"retmode": "json",
|
16 |
+
"api_key": NCBI_API_KEY
|
17 |
+
}
|
18 |
+
r = requests.get(url, params=params)
|
19 |
+
r.raise_for_status()
|
20 |
+
return r.json()
|
21 |
+
|
22 |
+
def fetch_pubmed_details(id_list):
|
23 |
+
"""Fetch article details for given PubMed IDs."""
|
24 |
+
ids = ",".join(id_list)
|
25 |
+
url = f"{BASE_URL}/efetch.fcgi"
|
26 |
+
params = {
|
27 |
+
"db": "pubmed",
|
28 |
+
"id": ids,
|
29 |
+
"retmode": "xml",
|
30 |
+
"api_key": NCBI_API_KEY
|
31 |
+
}
|
32 |
+
r = requests.get(url, params=params)
|
33 |
+
r.raise_for_status()
|
34 |
+
return r.text
|