Spaces:
Sleeping
Sleeping
| """ | |
| API key and environment configuration management. | |
| Keys are stored in session state only — never written to disk. | |
| """ | |
| import os | |
| from dotenv import load_dotenv | |
| # All supported API keys with metadata | |
| API_KEYS = { | |
| "NCBI_API_KEY": { | |
| "label": "NCBI API Key", | |
| "description": "Improves PubTator/PMC OA rate limits (3→10 req/sec)", | |
| "url": "https://www.ncbi.nlm.nih.gov/account/settings/", | |
| "required": False, | |
| }, | |
| "S2_API_KEY": { | |
| "label": "Semantic Scholar API Key", | |
| "description": "Dedicated S2 quota (1 req/sec) for article search/TLDR/citations", | |
| "url": "https://www.semanticscholar.org/product/api", | |
| "required": False, | |
| }, | |
| "ONCOKB_TOKEN": { | |
| "label": "OncoKB Token", | |
| "description": "Production OncoKB variant therapy & level evidence", | |
| "url": "https://www.oncokb.org/account/register", | |
| "required": False, | |
| }, | |
| "OPENFDA_API_KEY": { | |
| "label": "OpenFDA API Key", | |
| "description": "Better OpenFDA rate limits for drug safety/adverse event lookups", | |
| "url": "https://open.fda.gov/apis/authentication/", | |
| "required": False, | |
| }, | |
| "NCI_API_KEY": { | |
| "label": "NCI CTS API Key", | |
| "description": "NCI Clinical Trials Search (--source nci)", | |
| "url": "https://clinicaltrialsapi.cancer.gov/", | |
| "required": False, | |
| }, | |
| "DISGENET_API_KEY": { | |
| "label": "DisGeNET API Key", | |
| "description": "Scored gene-disease associations in gene/disease lookups", | |
| "url": "https://www.disgenet.com/", | |
| "required": False, | |
| }, | |
| "UMLS_API_KEY": { | |
| "label": "UMLS API Key", | |
| "description": "Clinical crosswalk enrichment in discover command", | |
| "url": "https://uts.nlm.nih.gov/uts/signup-login", | |
| "required": False, | |
| }, | |
| "ALPHAGENOME_API_KEY": { | |
| "label": "AlphaGenome API Key", | |
| "description": "Variant effect prediction (get variant ... predict)", | |
| "url": "https://deepmind.google/science/alphagenome/", | |
| "required": False, | |
| }, | |
| } | |
| # Additional config env vars | |
| CONFIG_VARS = { | |
| "BIOMCP_STUDY_DIR": { | |
| "label": "Study Directory", | |
| "description": "Local study root for cBioPortal datasets (leave blank for default)", | |
| }, | |
| } | |
| def load_env_keys() -> dict[str, str]: | |
| """Load API keys from .env file and environment, return as dict.""" | |
| load_dotenv() | |
| keys = {} | |
| for key_name in API_KEYS: | |
| keys[key_name] = os.environ.get(key_name, "") | |
| for key_name in CONFIG_VARS: | |
| keys[key_name] = os.environ.get(key_name, "") | |
| return keys | |
| def build_env_overrides(session_keys: dict[str, str]) -> dict[str, str]: | |
| """ | |
| Build env overrides dict from session keys. | |
| Only includes non-empty values. | |
| """ | |
| return {k: v for k, v in session_keys.items() if v} | |