Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,10 @@ import matplotlib.pyplot as plt
|
|
13 |
import seaborn as sns
|
14 |
from typing import Optional, Dict, List, Any
|
15 |
import os
|
|
|
16 |
|
|
|
|
|
17 |
|
18 |
# API Endpoints (Centralized Configuration)
|
19 |
API_ENDPOINTS = {
|
@@ -63,6 +66,7 @@ def _query_api(endpoint: str, params: Optional[Dict] = None) -> Optional[Dict]:
|
|
63 |
return response.json()
|
64 |
except requests.exceptions.RequestException as e:
|
65 |
st.error(f"API request failed: {e} for endpoint {endpoint}. Please check connectivity and the endpoint.")
|
|
|
66 |
return None
|
67 |
|
68 |
def _query_pubmed(query: str, email: Optional[str] = PUBMED_EMAIL) -> Optional[Dict]:
|
@@ -105,6 +109,7 @@ def _draw_molecule(smiles: str) -> Optional[any]:
|
|
105 |
return None
|
106 |
except Exception as e:
|
107 |
st.error(f"Error generating molecule image: {str(e)}")
|
|
|
108 |
return None
|
109 |
|
110 |
|
@@ -159,32 +164,34 @@ def _get_pharmgkb_data(gene:str) -> Optional[Dict]:
|
|
159 |
if data and 'clinicalAnnotations' in data:
|
160 |
return data
|
161 |
else:
|
162 |
-
|
163 |
-
|
164 |
|
165 |
def _get_bioportal_data(ontology: str, term: str) -> Optional[Dict]:
|
166 |
"""Fetches data from BioPortal."""
|
167 |
if not BIOPORTAL_API_KEY:
|
168 |
-
|
169 |
-
|
170 |
if not term:
|
171 |
-
|
172 |
-
|
|
|
173 |
headers = {
|
174 |
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
|
175 |
}
|
176 |
params = {
|
177 |
-
|
178 |
-
|
179 |
-
"apikey": BIOPORTAL_API_KEY
|
180 |
}
|
|
|
181 |
url = f"{API_ENDPOINTS['bioportal']}/search"
|
|
|
182 |
try:
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
except requests.exceptions.RequestException as e:
|
187 |
-
st.error(f"BioPortal API request failed: {e}
|
|
|
188 |
return None
|
189 |
|
190 |
def _save_pdf_report(report_content: str, filename: str):
|
@@ -414,7 +421,7 @@ with tabs[4]:
|
|
414 |
if st.button("Search BioPortal"):
|
415 |
with st.spinner("Searching Ontology..."):
|
416 |
bioportal_data = _get_bioportal_data(ontology_select, ontology_search_term)
|
417 |
-
if bioportal_data and
|
418 |
st.subheader(f"BioPortal Search Results for {ontology_select}")
|
419 |
for result in bioportal_data['collection']:
|
420 |
st.write(f"- {result['prefLabel']} ({result['@id']})")
|
|
|
13 |
import seaborn as sns
|
14 |
from typing import Optional, Dict, List, Any
|
15 |
import os
|
16 |
+
import logging
|
17 |
|
18 |
+
# Setup logging
|
19 |
+
logging.basicConfig(level=logging.ERROR) #Log only errors
|
20 |
|
21 |
# API Endpoints (Centralized Configuration)
|
22 |
API_ENDPOINTS = {
|
|
|
66 |
return response.json()
|
67 |
except requests.exceptions.RequestException as e:
|
68 |
st.error(f"API request failed: {e} for endpoint {endpoint}. Please check connectivity and the endpoint.")
|
69 |
+
logging.error(f"API request failed: {e} for endpoint {endpoint}.")
|
70 |
return None
|
71 |
|
72 |
def _query_pubmed(query: str, email: Optional[str] = PUBMED_EMAIL) -> Optional[Dict]:
|
|
|
109 |
return None
|
110 |
except Exception as e:
|
111 |
st.error(f"Error generating molecule image: {str(e)}")
|
112 |
+
logging.error(f"Error generating molecule image: {str(e)}")
|
113 |
return None
|
114 |
|
115 |
|
|
|
164 |
if data and 'clinicalAnnotations' in data:
|
165 |
return data
|
166 |
else:
|
167 |
+
return None
|
|
|
168 |
|
169 |
def _get_bioportal_data(ontology: str, term: str) -> Optional[Dict]:
|
170 |
"""Fetches data from BioPortal."""
|
171 |
if not BIOPORTAL_API_KEY:
|
172 |
+
st.error("BioPortal API key not found. Please add the BIOPORTAL_API_KEY to secrets.")
|
173 |
+
return None
|
174 |
if not term:
|
175 |
+
st.error("Please provide a search term.")
|
176 |
+
return None
|
177 |
+
|
178 |
headers = {
|
179 |
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
|
180 |
}
|
181 |
params = {
|
182 |
+
"q": term,
|
183 |
+
"ontologies": ontology
|
|
|
184 |
}
|
185 |
+
|
186 |
url = f"{API_ENDPOINTS['bioportal']}/search"
|
187 |
+
|
188 |
try:
|
189 |
+
response = requests.get(url, headers=headers, params=params, timeout=15)
|
190 |
+
response.raise_for_status()
|
191 |
+
return response.json()
|
192 |
except requests.exceptions.RequestException as e:
|
193 |
+
st.error(f"BioPortal API request failed: {e} Please check connectivity and ensure you have the correct API Key.")
|
194 |
+
logging.error(f"BioPortal API request failed: {e}")
|
195 |
return None
|
196 |
|
197 |
def _save_pdf_report(report_content: str, filename: str):
|
|
|
421 |
if st.button("Search BioPortal"):
|
422 |
with st.spinner("Searching Ontology..."):
|
423 |
bioportal_data = _get_bioportal_data(ontology_select, ontology_search_term)
|
424 |
+
if bioportal_data and 'collection' in bioportal_data:
|
425 |
st.subheader(f"BioPortal Search Results for {ontology_select}")
|
426 |
for result in bioportal_data['collection']:
|
427 |
st.write(f"- {result['prefLabel']} ({result['@id']})")
|