Spaces:
Runtime error
Runtime error
Write Chemiscope artifacts inside workspace
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import json
|
|
| 4 |
import re
|
| 5 |
import urllib.parse
|
| 6 |
import urllib.request
|
|
|
|
| 7 |
|
| 8 |
import chemiscope
|
| 9 |
from ase import Atoms
|
|
@@ -185,6 +186,7 @@ def smiles_to_name(smiles: str) -> str:
|
|
| 185 |
|
| 186 |
CHEMISCOPE_TEMPLATE_URL = "https://chemiscope.org/chemiscope_standalone.html"
|
| 187 |
CHEMISCOPE_TEMPLATE_CACHE = Path(tempfile.gettempdir()) / "chemiscope_standalone.html"
|
|
|
|
| 188 |
_CHEMISCOPE_TEMPLATE = None
|
| 189 |
_MAX_CHEMISCOPE_MOLECULES = 12
|
| 190 |
|
|
@@ -221,6 +223,12 @@ def _load_chemiscope_template():
|
|
| 221 |
return template
|
| 222 |
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
def _smiles_list_from_block(smiles_block: str):
|
| 225 |
"""Split a block of SMILES lines/CSV text into a unique, validated list."""
|
| 226 |
tokens = re.split(r"[,\n;]+", smiles_block or "")
|
|
@@ -300,7 +308,7 @@ def _infer_space_origin():
|
|
| 300 |
return ""
|
| 301 |
|
| 302 |
|
| 303 |
-
def _build_chemiscope_embed(dataset_payload: dict, dataset_path: str) -> str:
|
| 304 |
"""Create HTML content for Chemiscope visualization served locally from the Space."""
|
| 305 |
template_html = _load_chemiscope_template()
|
| 306 |
dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
|
|
@@ -403,18 +411,17 @@ def smiles_to_chemiscope_dataset(smiles_block: str):
|
|
| 403 |
settings=settings,
|
| 404 |
)
|
| 405 |
|
| 406 |
-
|
| 407 |
-
tmp_file.close()
|
| 408 |
chemiscope.write_input(
|
| 409 |
-
|
| 410 |
frames=frames,
|
| 411 |
properties=properties,
|
| 412 |
meta=meta,
|
| 413 |
settings=settings,
|
| 414 |
)
|
| 415 |
|
| 416 |
-
viewer_html = _build_chemiscope_embed(dataset,
|
| 417 |
-
return viewer_html,
|
| 418 |
|
| 419 |
|
| 420 |
def smiles_to_molecular_orbitals(smiles_input: str, name_input: str) -> str:
|
|
|
|
| 4 |
import re
|
| 5 |
import urllib.parse
|
| 6 |
import urllib.request
|
| 7 |
+
import uuid
|
| 8 |
|
| 9 |
import chemiscope
|
| 10 |
from ase import Atoms
|
|
|
|
| 186 |
|
| 187 |
CHEMISCOPE_TEMPLATE_URL = "https://chemiscope.org/chemiscope_standalone.html"
|
| 188 |
CHEMISCOPE_TEMPLATE_CACHE = Path(tempfile.gettempdir()) / "chemiscope_standalone.html"
|
| 189 |
+
CHEMISCOPE_ASSET_DIR = Path("chemiscope_artifacts")
|
| 190 |
_CHEMISCOPE_TEMPLATE = None
|
| 191 |
_MAX_CHEMISCOPE_MOLECULES = 12
|
| 192 |
|
|
|
|
| 223 |
return template
|
| 224 |
|
| 225 |
|
| 226 |
+
def _artifact_path(name: str) -> Path:
|
| 227 |
+
"""Create (if needed) and return a path inside the chemiscope artifacts directory."""
|
| 228 |
+
CHEMISCOPE_ASSET_DIR.mkdir(parents=True, exist_ok=True)
|
| 229 |
+
return CHEMISCOPE_ASSET_DIR / name
|
| 230 |
+
|
| 231 |
+
|
| 232 |
def _smiles_list_from_block(smiles_block: str):
|
| 233 |
"""Split a block of SMILES lines/CSV text into a unique, validated list."""
|
| 234 |
tokens = re.split(r"[,\n;]+", smiles_block or "")
|
|
|
|
| 308 |
return ""
|
| 309 |
|
| 310 |
|
| 311 |
+
def _build_chemiscope_embed(dataset_payload: dict, dataset_path: str | Path) -> str:
|
| 312 |
"""Create HTML content for Chemiscope visualization served locally from the Space."""
|
| 313 |
template_html = _load_chemiscope_template()
|
| 314 |
dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
|
|
|
|
| 411 |
settings=settings,
|
| 412 |
)
|
| 413 |
|
| 414 |
+
dataset_path = _artifact_path(f"dataset_{uuid.uuid4().hex}.json.gz")
|
|
|
|
| 415 |
chemiscope.write_input(
|
| 416 |
+
str(dataset_path),
|
| 417 |
frames=frames,
|
| 418 |
properties=properties,
|
| 419 |
meta=meta,
|
| 420 |
settings=settings,
|
| 421 |
)
|
| 422 |
|
| 423 |
+
viewer_html = _build_chemiscope_embed(dataset, dataset_path)
|
| 424 |
+
return viewer_html, str(dataset_path)
|
| 425 |
|
| 426 |
|
| 427 |
def smiles_to_molecular_orbitals(smiles_input: str, name_input: str) -> str:
|