Spaces:
Runtime error
Runtime error
Serve Chemiscope viewer via chemiscope.org iframe
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import plotly.graph_objects as go
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import re
|
|
|
|
| 5 |
import urllib.request
|
| 6 |
|
| 7 |
import chemiscope
|
|
@@ -283,8 +284,42 @@ def _extract_gasteiger_charges(mol: Chem.Mol):
|
|
| 283 |
return charges
|
| 284 |
|
| 285 |
|
| 286 |
-
def
|
| 287 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
template_html = _load_chemiscope_template()
|
| 289 |
dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
|
| 290 |
combined = template_html + dataset_json
|
|
@@ -292,7 +327,7 @@ def _build_chemiscope_embed(dataset_payload: dict) -> str:
|
|
| 292 |
return (
|
| 293 |
"<div style='width:100%;'>"
|
| 294 |
"<iframe "
|
| 295 |
-
"title='Chemiscope explorer' "
|
| 296 |
"style='width:100%;height:620px;border:none;border-radius:8px;' "
|
| 297 |
f"src='data:text/html;base64,{encoded}'></iframe>"
|
| 298 |
"<p style='font-size:0.9em;margin-top:0.5rem;'>"
|
|
@@ -390,7 +425,7 @@ def smiles_to_chemiscope_dataset(smiles_block: str):
|
|
| 390 |
settings=settings,
|
| 391 |
)
|
| 392 |
|
| 393 |
-
viewer_html = _build_chemiscope_embed(dataset)
|
| 394 |
return viewer_html, tmp_file.name
|
| 395 |
|
| 396 |
|
|
@@ -774,7 +809,7 @@ chemiscope_interface = gr.Interface(
|
|
| 774 |
placeholder="One SMILES per line or comma separated (max 12 molecules).",
|
| 775 |
),
|
| 776 |
outputs=[
|
| 777 |
-
gr.HTML(label="Chemiscope Viewer"
|
| 778 |
gr.File(label="Chemiscope Dataset (.json.gz)"),
|
| 779 |
],
|
| 780 |
api_name="chemiscope_explorer",
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
+
import urllib.parse
|
| 6 |
import urllib.request
|
| 7 |
|
| 8 |
import chemiscope
|
|
|
|
| 284 |
return charges
|
| 285 |
|
| 286 |
|
| 287 |
+
def _infer_space_origin():
|
| 288 |
+
"""Best-effort detection of the public Space base URL."""
|
| 289 |
+
for key in ("SPACE_HTTP_URL", "SPACE_URL"):
|
| 290 |
+
candidate = os.environ.get(key)
|
| 291 |
+
if candidate:
|
| 292 |
+
return candidate.rstrip("/")
|
| 293 |
+
|
| 294 |
+
space_id = os.environ.get("SPACE_ID")
|
| 295 |
+
if space_id and "/" in space_id:
|
| 296 |
+
owner, space = space_id.split("/", 1)
|
| 297 |
+
safe_space = space.replace("_", "-")
|
| 298 |
+
return f"https://{owner}-{safe_space}.hf.space"
|
| 299 |
+
|
| 300 |
+
return ""
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def _build_chemiscope_embed(dataset_payload: dict, dataset_path: str) -> str:
|
| 304 |
+
"""Create HTML content for Chemiscope visualization, preferring hosted datasets."""
|
| 305 |
+
space_origin = _infer_space_origin()
|
| 306 |
+
if space_origin and dataset_path:
|
| 307 |
+
relative_path = Path(dataset_path).as_posix()
|
| 308 |
+
dataset_url = f"{space_origin}/file={urllib.parse.quote(relative_path)}"
|
| 309 |
+
iframe_src = f"https://chemiscope.org/?load={urllib.parse.quote(dataset_url, safe='')}"
|
| 310 |
+
return (
|
| 311 |
+
"<div style='width:100%;'>"
|
| 312 |
+
"<iframe "
|
| 313 |
+
"title='Chemiscope explorer' "
|
| 314 |
+
"style='width:100%;height:620px;border:none;border-radius:8px;' "
|
| 315 |
+
f"src='{iframe_src}'></iframe>"
|
| 316 |
+
"<p style='font-size:0.9em;margin-top:0.5rem;'>"
|
| 317 |
+
"Open in a new tab if the viewer looks blank: "
|
| 318 |
+
f"<a href='{iframe_src}' target='_blank' rel='noopener'>chemiscope.org</a>"
|
| 319 |
+
"</p>"
|
| 320 |
+
"</div>"
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
template_html = _load_chemiscope_template()
|
| 324 |
dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
|
| 325 |
combined = template_html + dataset_json
|
|
|
|
| 327 |
return (
|
| 328 |
"<div style='width:100%;'>"
|
| 329 |
"<iframe "
|
| 330 |
+
"title='Chemiscope explorer (offline fallback)' "
|
| 331 |
"style='width:100%;height:620px;border:none;border-radius:8px;' "
|
| 332 |
f"src='data:text/html;base64,{encoded}'></iframe>"
|
| 333 |
"<p style='font-size:0.9em;margin-top:0.5rem;'>"
|
|
|
|
| 425 |
settings=settings,
|
| 426 |
)
|
| 427 |
|
| 428 |
+
viewer_html = _build_chemiscope_embed(dataset, tmp_file.name)
|
| 429 |
return viewer_html, tmp_file.name
|
| 430 |
|
| 431 |
|
|
|
|
| 809 |
placeholder="One SMILES per line or comma separated (max 12 molecules).",
|
| 810 |
),
|
| 811 |
outputs=[
|
| 812 |
+
gr.HTML(label="Chemiscope Viewer"),
|
| 813 |
gr.File(label="Chemiscope Dataset (.json.gz)"),
|
| 814 |
],
|
| 815 |
api_name="chemiscope_explorer",
|