Spaces:
Running
Running
File size: 24,091 Bytes
e23f83d 3cf0050 e23f83d 7d8bb7d 3cf0050 6d4df16 1b94f8b 48c538b 1b94f8b 48c538b 1b94f8b 48c538b 1b94f8b 48c538b 1b94f8b 48c538b 1b94f8b 48c538b 7d8bb7d 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 48c538b 6d4df16 0ade04a 94990ac 820c44b 38d2ff0 6d4df16 1fedfc5 d6059b9 38d2ff0 d6059b9 38d2ff0 6d4df16 820c44b 6d4df16 1b94f8b 6d4df16 c2be249 fd2ecc8 6d4df16 1b94f8b 4ed9de7 73fc96d 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 0ade04a 48c538b 38d2ff0 c2be249 6d4df16 48c538b 3494b39 820c44b 38d2ff0 6d4df16 0ade04a 1b94f8b 0ade04a 1b94f8b 0ade04a 94990ac 1fedfc5 0ade04a 02d3f79 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 c2be249 48c538b 38d2ff0 3f17e4b 48c538b 3f17e4b 38d2ff0 6d4df16 3f17e4b 1b94f8b 3f17e4b 94990ac 3f17e4b 02d3f79 6d4df16 38d2ff0 6d4df16 38d2ff0 6d4df16 3f17e4b 48c538b 38d2ff0 2fe706b 6d4df16 3f17e4b e72bc1b ea0059e e72bc1b 7d8bb7d 1b94f8b 7d8bb7d 94990ac 7d8bb7d 6d4df16 7d8bb7d 6d4df16 1fedfc5 7d8bb7d 6d4df16 7d8bb7d 6d4df16 7d8bb7d 94990ac 1b94f8b 48c538b 7d8bb7d 48c538b 6d4df16 7d8bb7d 6d4df16 7d8bb7d 6d4df16 0fc2a04 ea0059e e72bc1b 7d8bb7d 94990ac 7d8bb7d 0ade04a 7d8bb7d 0ade04a 7d8bb7d 6d4df16 e72bc1b 7d8bb7d 6d4df16 7d8bb7d 6d4df16 7d8bb7d 6d4df16 1b94f8b 94990ac 1b94f8b 48c538b 1b94f8b 48c538b 6d4df16 7d8bb7d 6d4df16 3f17e4b ea0059e 3f17e4b 7d8bb7d 94990ac 7d8bb7d 3f17e4b 7d8bb7d 3f17e4b 7d8bb7d 6d4df16 7d8bb7d 6d4df16 7d8bb7d 6d4df16 1b94f8b 94990ac 1b94f8b 48c538b 1b94f8b 48c538b 6d4df16 7d8bb7d 6d4df16 2fe706b 6d4df16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
import os
os.environ["TORCH_DYNAMO_DISABLE"] = "1"
import tempfile
import numpy as np
import gradio as gr
from ase.io import read, write
from ase.io.trajectory import Trajectory
from gradio_molecule3d import Molecule3D
from simulation_scripts_orbmol import load_orbmol_model, run_md_simulation, run_relaxation_simulation
import hashlib
from pathlib import Path
# ==== Configuración Molecule3D ====
DEFAULT_MOLECULAR_REPRESENTATIONS = [
{
"model": 0,
"chain": "",
"resname": "",
"style": "sphere",
"color": "Jmol",
"around": 0,
"byres": False,
"scale": 0.3,
},
{
"model": 0,
"chain": "",
"resname": "",
"style": "stick",
"color": "Jmol",
"around": 0,
"byres": False,
"scale": 0.2,
},
]
DEFAULT_MOLECULAR_SETTINGS = {
"backgroundColor": "white",
"orthographic": False,
"disableFog": False,
}
# ==== UI definition ====
# ==== SPE Inputs and Outputs ====
input_sp = gr.File(
label="Upload Structure File",
file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
file_count="single",
render=False
)
task_name_sp = gr.Radio(
["OMol", "OMat", "OMol-Direct"],
value="OMol",
label="Model Type",
render=False
)
total_charge_sp = gr.Slider(-10, 10, 0, step=1, label="Charge",render=False)
spin_multiplicity_sp = gr.Slider(1, 11, 1, step=1, label="Spin Multiplicity",render=False)
run_sp = gr.Button("Run OrbMol Prediction", variant="primary",render=False)
spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False,render=False)
spe_status = gr.Textbox(label="Status", interactive=False,render=False)
spe_viewer = Molecule3D(
label="Input Structure Viewer",
reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
config=DEFAULT_MOLECULAR_SETTINGS,
render=False
)
#==== MD Inputs and Outputs ====
input_md = gr.File(
label="Upload Structure File",
file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
file_count="single",
render=False
)
task_name_md = gr.Radio(
["OMol", "OMat", "OMol-Direct"],
value="OMol",
label="Model Type",
render=False
)
charge_md = gr.Slider(-10, 10, 0, step=1, label="Charge",render=False)
spin_md = gr.Slider(1, 11, 1, step=1, label="Spin Multiplicity",render=False)
steps_md = gr.Slider(10, 2000, 100, step=10, label="Steps",render=False)
temp_md = gr.Slider(10, 1500, 300, step=10, label="Temperature (K)",render=False)
timestep_md = gr.Slider(0.1, 5.0, 1.0, step=0.1, label="Timestep (fs)",render=False)
ensemble_md = gr.Radio(["NVE", "NVT"], value="NVE", label="Ensemble",render=False)
run_md_btn = gr.Button("Run MD Simulation", variant="primary",render=False)
md_status = gr.Textbox(label="MD Status", interactive=False,render=False)
md_traj = gr.File(label="Trajectory (.traj)", interactive=False,render=False)
md_viewer = Molecule3D(
label="MD Result Viewer",
reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
config=DEFAULT_MOLECULAR_SETTINGS,
render=False
)
md_log = gr.Textbox(label="Log", interactive=False, lines=15,render=False)
md_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20,render=False)
md_explain = gr.Markdown(render=False)
#==== Relax Inputs and Outputs ====
input_rlx = gr.File(
label="Upload Structure File",
file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
file_count="single",
render=False
)
task_name_rlx = gr.Radio(
["OMol", "OMat", "OMol-Direct"],
value="OMol",
label="Model Type",
render=False
)
steps_rlx = gr.Slider(1, 2000, 300, step=1, label="Max Steps",render=False)
fmax_rlx = gr.Slider(0.001, 0.5, 0.05, step=0.001, label="Fmax (eV/Å)",render=False)
charge_rlx = gr.Slider(-10, 10, 0, step=1, label="Charge",render=False)
spin_rlx = gr.Slider(1, 11, 1, step=1, label="Spin",render=False)
relax_cell = gr.Checkbox(False, label="Relax Unit Cell",render=False)
run_rlx_btn = gr.Button("Run Optimization", variant="primary",render=False)
rlx_status = gr.Textbox(label="Status", interactive=False,render=False)
rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False,render=False)
rlx_viewer = Molecule3D(
label="Optimized Structure Viewer",
reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
config=DEFAULT_MOLECULAR_SETTINGS,
render=False
)
rlx_log = gr.Textbox(label="Log", interactive=False, lines=15,render=False)
rlx_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20,render=False)
rlx_explain = gr.Markdown(render=False)
# ==== Conversión a PDB para Molecule3D ====
def convert_to_pdb_for_viewer(file_path):
"""Convierte cualquier archivo a PDB para Molecule3D"""
if not file_path or not os.path.exists(file_path):
return None
try:
atoms = read(file_path)
cache_dir = os.path.join(tempfile.gettempdir(), "gradio")
os.makedirs(cache_dir, exist_ok=True)
pdb_path = os.path.join(cache_dir, f"mol_{hashlib.md5(file_path.encode()).hexdigest()[:12]}.pdb")
write(pdb_path, atoms, format="proteindatabank")
return pdb_path
except Exception as e:
print(f"Error converting to PDB: {e}")
return None
# ==== OrbMol SPE ====
def predict_molecule(structure_file, task_name, charge=0, spin_multiplicity=1):
"""Single Point Energy + fuerzas (OrbMol)"""
try:
calc = load_orbmol_model(task_name)
if not structure_file:
return "Error: Please upload a structure file", "Error", None
file_path = structure_file
if not os.path.exists(file_path):
return f"Error: File not found: {file_path}", "Error", None
if os.path.getsize(file_path) == 0:
return f"Error: Empty file: {file_path}", "Error", None
atoms = read(file_path)
if task_name in ["OMol", "OMol-Direct"]:
atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
atoms.calc = calc
energy = atoms.get_potential_energy()
forces = atoms.get_forces()
lines = [
f"Model: {task_name}",
f"Total Energy: {energy:.6f} eV",
"",
"Atomic Forces:"
]
for i, fc in enumerate(forces):
lines.append(f"Atom {i+1}: [{fc[0]:.4f}, {fc[1]:.4f}, {fc[2]:.4f}] eV/Å")
max_force = float(np.max(np.linalg.norm(forces, axis=1)))
lines += ["", f"Max Force: {max_force:.4f} eV/Å"]
pdb_file = convert_to_pdb_for_viewer(file_path)
return "\n".join(lines), f"Calculation completed with {task_name}", pdb_file
except Exception as e:
import traceback
traceback.print_exc()
return f"Error during calculation: {e}", "Error", None
# ==== Wrappers MD y Relax ====
def md_wrapper(structure_file, task_name, charge, spin, steps, tempK, timestep_fs, ensemble):
try:
if not structure_file:
return ("Error: Please upload a structure file", None, "", "", "", None)
traj_path, log_text, script_text, explanation = run_md_simulation(
structure_file,
int(steps),
20,
float(timestep_fs),
float(tempK),
"NVT" if ensemble == "NVT" else "NVE",
str(task_name),
int(charge),
int(spin),
)
status = f"MD completed: {int(steps)} steps at {int(tempK)} K ({ensemble})"
pdb_file = convert_to_pdb_for_viewer(traj_path)
return (status, traj_path, log_text, script_text, explanation, pdb_file)
except Exception as e:
import traceback
traceback.print_exc()
return (f"Error: {e}", None, "", "", "", None)
def relax_wrapper(structure_file, task_name, steps, fmax, charge, spin, relax_cell):
try:
if not structure_file:
return ("Error: Please upload a structure file", None, "", "", "", None)
traj_path, log_text, script_text, explanation = run_relaxation_simulation(
structure_file,
int(steps),
float(fmax),
str(task_name),
int(charge),
int(spin),
bool(relax_cell),
)
status = f"Relaxation finished (<={int(steps)} steps, fmax={float(fmax)} eV/Å)"
pdb_file = convert_to_pdb_for_viewer(traj_path)
return (status, traj_path, log_text, script_text, explanation, pdb_file)
except Exception as e:
import traceback
traceback.print_exc()
return (f"Error: {e}", None, "", "", "", None)
# ==== UI ====
with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
with gr.Tabs():
# -------- HOME TAB (NUEVA) --------
with gr.Tab("Home"):
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Learn more about OrbMol")
with gr.Accordion("What is OrbMol?", open=False):
gr.Markdown("""
OrbMol is a suite of quantum-accurate machine learning models for molecular predictions. Built on the **Orb-v3 architecture**, OrbMol provides fast and accurate calculations of energies, forces, and molecular properties at the level of advanced quantum chemistry methods.
""")
with gr.Accordion("Available Models", open=False):
gr.Markdown("""
**OMol** and **OMol-Direct**
- **Training dataset**: OMol25 (>100M calculations on small molecules, biomolecules, metal complexes, and electrolytes)
- **Level of theory**: ωB97M-V/def2-TZVPD with non-local dispersion; solvation treated explicitly
- **Inputs**: total charge & spin multiplicity
- **Applications**: biology, organic chemistry, protein folding, small-molecule drugs, organic liquids, homogeneous catalysis
- **Caveats**: trained only on aperiodic systems → periodic/inorganic cases may not work well
- **Difference**: OMol enforces energy–force consistency; OMol-Direct relaxes this for efficiency
**OMat**
- **Training dataset**: OMat24 (>100M inorganic calculations, from Materials Project, Alexandria, and far-from-equilibrium samples)
- **Level of theory**: PBE/PBE+U with Materials Project settings; VASP 54 pseudopotentials; no dispersion
- **Inputs**: No support for spin and charge
- **Applications**: inorganic discovery, photovoltaics, alloys, superconductors, electronic/optical materials
""")
with gr.Accordion("Supported File Formats", open=False):
gr.Markdown("""
OrbMol supports: `.xyz`, `.pdb`, `.cif`, `.traj`, `.mol`, `.sdf`
""")
with gr.Accordion("Resources & Support", open=False):
gr.Markdown("""
- [Orb-v3 paper](https://arxiv.org/abs/2504.06231)
- [Orb-Models GitHub repository](https://github.com/orbital-materials/orb-models)
""")
with gr.Column(scale=2):
gr.Image("logo_color_text.png",
show_share_button=False,
show_download_button=False,
show_label=False,
show_fullscreen_button=False)
gr.Markdown("# OrbMol — Quantum-Accurate Molecular Predictions")
gr.Markdown("""
Welcome to the OrbMol demo! Use the tabs above to access different functionalities:
1. **Single Point Energy**: Calculate energies and forces
2. **Molecular Dynamics**: Run MD simulations
3. **Relaxation / Optimization**: Optimize structures
Supported formats: `.xyz`, `.pdb`, `.cif`, `.traj`, `.mol`, `.sdf`
""")
# -------- SPE (IDÉNTICA A LA QUE FUNCIONABA) --------
with gr.Tab("Single Point Energy"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("# OrbMol: Single Point Energy")
gr.Markdown("Run a quantum-accurate single point energy calculation with OrbMol models.")
gr.Markdown("**Supported formats:** .xyz, .pdb, .cif, .traj, .mol, .sdf")
gr.Examples(
examples=[[str(Path(__file__).parent/ "./examples/10mer_b-DNA.pdb"),
"OMol",
0,
1,
"",
],
[str(Path(__file__).parent/ "./examples/PtO2.cif"),
"OMat",
0,
1,
"",
],
],
example_labels=[
"A 10-mer DNA (A tiny DNA fragment)",
"An inorganic crystal (PtO2)",
],
inputs=[
input_sp,
task_name_sp,
total_charge_sp,
spin_multiplicity_sp
],
outputs=[
spe_out,
spe_status,
spe_viewer
],
fn=predict_molecule,
run_on_click=True,
cache_examples=True,
label="Try an example!",
)
input_sp.render()
task_name_sp.render()
with gr.Row():
total_charge_sp.render()
spin_multiplicity_sp.render()
run_sp.render()
with gr.Column(variant="panel", min_width=500):
spe_out.render()
spe_status.render()
spe_viewer.render()
task_name_sp.change(
lambda x: (
gr.update(visible=x in ["OMol", "OMol-Direct"]),
gr.update(visible=x in ["OMol", "OMol-Direct"])
),
[task_name_sp],
[total_charge_sp, spin_multiplicity_sp]
)
run_sp.click(
predict_molecule,
[input_sp, task_name_sp, total_charge_sp, spin_multiplicity_sp],
[spe_out, spe_status, spe_viewer]
)
# -------- MD (IDÉNTICA A LA QUE FUNCIONABA) --------
with gr.Tab("Molecular Dynamics"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## OrbMol: Molecular Dynamics Simulation")
gr.Markdown("Run a quantum-accurate molecular dynamics simulation with OrbMol models.")
gr.Markdown("**Supported formats:** .xyz, .pdb, .cif, .traj, .mol, .sdf")
gr.Examples(
examples=[[str(Path(__file__).parent/ "./examples/Cyclohexane.sdf"),
"OMol",
0,
1,
1000,
300,
0.5,
"NVT",
"",
],
[str(Path(__file__).parent/ "./examples/Cyclohexane.sdf"),
"OMol-Direct",
0,
1,
1000,
300,
0.5,
"NVT",
"",
],
],
example_labels=[
"Cyclohexane with OMol",
"Cyclohexane with OMol-Direct",
],
inputs=[
input_md,
task_name_md,
charge_md,
spin_md,
steps_md,
temp_md,
timestep_md,
ensemble_md
],
outputs=[
md_status,
md_traj,
md_log,
md_script,
md_explain,
md_viewer
],
fn=md_wrapper,
run_on_click=True,
cache_examples=True,
label="Try an example!",
)
input_md.render()
task_name_md.render()
with gr.Row():
charge_md.render()
spin_md.render()
with gr.Row():
steps_md.render()
temp_md.render()
with gr.Row():
timestep_md.render()
ensemble_md.render()
run_md_btn.render()
with gr.Column(variant="panel", min_width=520):
md_status.render()
md_traj.render()
md_viewer.render()
md_log.render()
md_script.render()
md_explain.render()
task_name_md.change(
lambda x: (
gr.update(visible=x in ["OMol", "OMol-Direct"]),
gr.update(visible=x in ["OMol", "OMol-Direct"])
),
[task_name_md],
[charge_md, spin_md]
)
run_md_btn.click(
md_wrapper,
[input_md, task_name_md, charge_md, spin_md, steps_md, temp_md, timestep_md, ensemble_md],
[md_status, md_traj, md_log, md_script, md_explain, md_viewer]
)
# -------- Relax (IDÉNTICA A LA QUE FUNCIONABA) --------
with gr.Tab("Relaxation / Optimization"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## OrbMol: Structure Relaxation/Optimization")
gr.Markdown("Run a quantum-accurate structure relaxation with OrbMol models.")
gr.Markdown("**Supported formats:** .xyz, .pdb, .cif, .traj, .mol, .sdf")
gr.Examples(
examples=[[str(Path(__file__).parent/ "./examples/dioctyl_sebacate.xyz"),
"OMol",
1000,
0.05,
0,
1,
False,
"",
],
[str(Path(__file__).parent/ "./examples/Fe2O3.cif"),
"OMat",
1000,
0.05,
0,
1,
False,
"",
],
],
example_labels=[
"A branched Alkane (Dioctyl Sebacate)",
"An inorganic crystal (Fe2O3)",
],
inputs=[
input_rlx,
task_name_rlx,
steps_rlx,
fmax_rlx,
charge_rlx,
spin_rlx,
relax_cell
],
outputs=[
rlx_status,
rlx_traj,
rlx_log,
rlx_script,
rlx_explain,
rlx_viewer
],
fn=relax_wrapper,
run_on_click=True,
cache_examples=True,
label="Try an example!",
)
input_rlx.render()
task_name_rlx.render()
with gr.Row():
steps_rlx.render()
fmax_rlx.render()
with gr.Row():
charge_rlx.render()
spin_rlx.render()
relax_cell.render()
run_rlx_btn.render()
with gr.Column(variant="panel", min_width=520):
rlx_status.render()
rlx_traj.render()
rlx_viewer.render()
rlx_log.render()
rlx_script.render()
rlx_explain.render()
task_name_rlx.change(
lambda x: (
gr.update(visible=x in ["OMol", "OMol-Direct"]),
gr.update(visible=x in ["OMol", "OMol-Direct"])
),
[task_name_rlx],
[charge_rlx, spin_rlx]
)
run_rlx_btn.click(
relax_wrapper,
[input_rlx, task_name_rlx, steps_rlx, fmax_rlx, charge_rlx, spin_rlx, relax_cell],
[rlx_status, rlx_traj, rlx_log, rlx_script, rlx_explain, rlx_viewer]
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True) |