Spaces:
Running
Running
# genesis/literature.py | |
""" | |
Literature intelligence for GENESIS-AI | |
Fetches biomedical, biotech, and synthetic biology publications. | |
""" | |
import os | |
import requests | |
NCBI_EMAIL = os.getenv("NCBI_EMAIL") | |
PUBMED_SEARCH_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi" | |
PUBMED_FETCH_URL = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi" | |
CHEMBL_API = "https://www.ebi.ac.uk/chembl/api/data/molecule" | |
def search_pubmed(query, max_results=10): | |
"""Search PubMed and return article IDs.""" | |
try: | |
params = { | |
"db": "pubmed", | |
"term": query, | |
"retmax": max_results, | |
"retmode": "json", | |
"email": NCBI_EMAIL | |
} | |
r = requests.get(PUBMED_SEARCH_URL, params=params) | |
r.raise_for_status() | |
return r.json() | |
except Exception as e: | |
return {"error": str(e)} | |
def fetch_pubmed_details(id_list): | |
"""Fetch details for a list of PubMed IDs.""" | |
try: | |
ids = ",".join(id_list) | |
params = {"db": "pubmed", "id": ids, "retmode": "xml"} | |
r = requests.get(PUBMED_FETCH_URL, params=params) | |
r.raise_for_status() | |
return r.text # XML response | |
except Exception as e: | |
return {"error": str(e)} | |
def search_chembl(query): | |
"""Search ChEMBL molecules by name.""" | |
try: | |
r = requests.get(f"{CHEMBL_API}?molecule_synonyms__icontains={query}") | |
r.raise_for_status() | |
return r.json() | |
except Exception as e: | |
return {"error": str(e)} | |