File size: 917 Bytes
1411075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# genesis/api_clients/pubmed_api.py
import requests
import os

NCBI_API_KEY = os.getenv("NCBI_API_KEY")  # Stored in HF Secrets
BASE_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils"

def search_pubmed(query: str, max_results: int = 10):
    """Search PubMed articles."""
    url = f"{BASE_URL}/esearch.fcgi"
    params = {
        "db": "pubmed",
        "term": query,
        "retmax": max_results,
        "retmode": "json",
        "api_key": NCBI_API_KEY
    }
    r = requests.get(url, params=params)
    r.raise_for_status()
    return r.json()

def fetch_pubmed_details(id_list):
    """Fetch article details for given PubMed IDs."""
    ids = ",".join(id_list)
    url = f"{BASE_URL}/efetch.fcgi"
    params = {
        "db": "pubmed",
        "id": ids,
        "retmode": "xml",
        "api_key": NCBI_API_KEY
    }
    r = requests.get(url, params=params)
    r.raise_for_status()
    return r.text