Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,23 +7,43 @@ from ase.io.trajectory import Trajectory
|
|
| 7 |
from gradio_molecule3d import Molecule3D
|
| 8 |
import hashlib
|
| 9 |
|
| 10 |
-
# ==== Molecule3D viewer preparation ====
|
| 11 |
def prepare_molecule_for_viewer(traj_path):
|
| 12 |
"""Convert trajectory to format compatible with Molecule3D viewer"""
|
| 13 |
if not traj_path or not os.path.exists(traj_path):
|
|
|
|
| 14 |
return None
|
| 15 |
|
| 16 |
try:
|
| 17 |
traj = Trajectory(traj_path)
|
| 18 |
if len(traj) == 0:
|
|
|
|
| 19 |
return None
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
# Save last frame as PDB for Molecule3D
|
| 22 |
temp_pdb = tempfile.NamedTemporaryFile(suffix='.pdb', delete=False)
|
| 23 |
write(temp_pdb.name, traj[-1], format='pdb')
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print(f"Error preparing molecule: {e}")
|
| 27 |
return None
|
| 28 |
|
| 29 |
# ==== OrbMol SPE ====
|
|
@@ -49,10 +69,8 @@ def predict_molecule(structure_file, charge=0, spin_multiplicity=1):
|
|
| 49 |
if not structure_file:
|
| 50 |
return "Error: Please upload a structure file", "Error"
|
| 51 |
|
| 52 |
-
# structure_file es directamente el path del archivo en Gradio
|
| 53 |
file_path = structure_file
|
| 54 |
|
| 55 |
-
# Verificar que el archivo existe y no está vacío
|
| 56 |
if not os.path.exists(file_path):
|
| 57 |
return f"Error: File not found: {file_path}", "Error"
|
| 58 |
|
|
@@ -63,8 +81,8 @@ def predict_molecule(structure_file, charge=0, spin_multiplicity=1):
|
|
| 63 |
atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
|
| 64 |
atoms.calc = calc
|
| 65 |
|
| 66 |
-
energy = atoms.get_potential_energy()
|
| 67 |
-
forces = atoms.get_forces()
|
| 68 |
|
| 69 |
lines = [f"Total Energy: {energy:.6f} eV", "", "Atomic Forces:"]
|
| 70 |
for i, fc in enumerate(forces):
|
|
@@ -82,14 +100,14 @@ from simulation_scripts_orbmol import (
|
|
| 82 |
run_relaxation_simulation,
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# ==== Wrappers
|
| 86 |
def md_wrapper(structure_file, charge, spin, steps, tempK, timestep_fs, ensemble):
|
| 87 |
try:
|
| 88 |
if not structure_file:
|
| 89 |
return ("Error: Please upload a structure file", None, "", "", "", None)
|
| 90 |
|
| 91 |
-
# structure_file es directamente el path del archivo
|
| 92 |
file_path = structure_file
|
|
|
|
| 93 |
|
| 94 |
traj_path, log_text, script_text, explanation = run_md_simulation(
|
| 95 |
file_path,
|
|
@@ -103,12 +121,16 @@ def md_wrapper(structure_file, charge, spin, steps, tempK, timestep_fs, ensemble
|
|
| 103 |
)
|
| 104 |
status = f"MD completed: {int(steps)} steps at {int(tempK)} K ({ensemble})"
|
| 105 |
|
|
|
|
|
|
|
| 106 |
# Preparar archivo PDB para el visor 3D
|
| 107 |
pdb_file = prepare_molecule_for_viewer(traj_path)
|
|
|
|
| 108 |
|
| 109 |
return (status, traj_path, log_text, script_text, explanation, pdb_file)
|
| 110 |
|
| 111 |
except Exception as e:
|
|
|
|
| 112 |
return (f"Error: {e}", None, "", "", "", None)
|
| 113 |
|
| 114 |
def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
|
|
@@ -116,8 +138,8 @@ def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
|
|
| 116 |
if not structure_file:
|
| 117 |
return ("Error: Please upload a structure file", None, "", "", "", None)
|
| 118 |
|
| 119 |
-
# structure_file es directamente el path del archivo
|
| 120 |
file_path = structure_file
|
|
|
|
| 121 |
|
| 122 |
traj_path, log_text, script_text, explanation = run_relaxation_simulation(
|
| 123 |
file_path,
|
|
@@ -129,12 +151,16 @@ def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
|
|
| 129 |
)
|
| 130 |
status = f"Relaxation finished (≤ {int(steps)} steps, fmax={float(fmax)} eV/Å)"
|
| 131 |
|
|
|
|
|
|
|
| 132 |
# Preparar archivo PDB para el visor 3D
|
| 133 |
pdb_file = prepare_molecule_for_viewer(traj_path)
|
|
|
|
| 134 |
|
| 135 |
return (status, traj_path, log_text, script_text, explanation, pdb_file)
|
| 136 |
|
| 137 |
except Exception as e:
|
|
|
|
| 138 |
return (f"Error: {e}", None, "", "", "", None)
|
| 139 |
|
| 140 |
# ==== UI ====
|
|
@@ -191,7 +217,38 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
|
|
| 191 |
with gr.Column(variant="panel", min_width=520):
|
| 192 |
md_status = gr.Textbox(label="MD Status", interactive=False)
|
| 193 |
md_traj = gr.File(label="Trajectory (.traj)", interactive=False)
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
md_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
|
| 196 |
md_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
|
| 197 |
md_explain = gr.Markdown()
|
|
@@ -226,7 +283,38 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
|
|
| 226 |
with gr.Column(variant="panel", min_width=520):
|
| 227 |
rlx_status = gr.Textbox(label="Status", interactive=False)
|
| 228 |
rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False)
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
rlx_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
|
| 231 |
rlx_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
|
| 232 |
rlx_explain = gr.Markdown()
|
|
|
|
| 7 |
from gradio_molecule3d import Molecule3D
|
| 8 |
import hashlib
|
| 9 |
|
| 10 |
+
# ==== Molecule3D viewer preparation con debug ====
|
| 11 |
def prepare_molecule_for_viewer(traj_path):
|
| 12 |
"""Convert trajectory to format compatible with Molecule3D viewer"""
|
| 13 |
if not traj_path or not os.path.exists(traj_path):
|
| 14 |
+
print("No trajectory path provided or file doesn't exist")
|
| 15 |
return None
|
| 16 |
|
| 17 |
try:
|
| 18 |
traj = Trajectory(traj_path)
|
| 19 |
if len(traj) == 0:
|
| 20 |
+
print("Empty trajectory")
|
| 21 |
return None
|
| 22 |
|
| 23 |
+
# Debug info
|
| 24 |
+
print(f"Preparing viewer: {len(traj)} frames, {len(traj[-1])} atoms")
|
| 25 |
+
|
| 26 |
# Save last frame as PDB for Molecule3D
|
| 27 |
temp_pdb = tempfile.NamedTemporaryFile(suffix='.pdb', delete=False)
|
| 28 |
write(temp_pdb.name, traj[-1], format='pdb')
|
| 29 |
+
|
| 30 |
+
# Verify PDB was created
|
| 31 |
+
if os.path.exists(temp_pdb.name):
|
| 32 |
+
file_size = os.path.getsize(temp_pdb.name)
|
| 33 |
+
print(f"PDB created: {temp_pdb.name}, size: {file_size} bytes")
|
| 34 |
+
|
| 35 |
+
# Read first few lines to verify content
|
| 36 |
+
with open(temp_pdb.name, 'r') as f:
|
| 37 |
+
first_lines = f.read(200)
|
| 38 |
+
print(f"PDB content preview: {first_lines[:100]}...")
|
| 39 |
+
|
| 40 |
+
return temp_pdb.name
|
| 41 |
+
else:
|
| 42 |
+
print("PDB file was not created")
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"Error preparing molecule for viewer: {e}")
|
| 47 |
return None
|
| 48 |
|
| 49 |
# ==== OrbMol SPE ====
|
|
|
|
| 69 |
if not structure_file:
|
| 70 |
return "Error: Please upload a structure file", "Error"
|
| 71 |
|
|
|
|
| 72 |
file_path = structure_file
|
| 73 |
|
|
|
|
| 74 |
if not os.path.exists(file_path):
|
| 75 |
return f"Error: File not found: {file_path}", "Error"
|
| 76 |
|
|
|
|
| 81 |
atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
|
| 82 |
atoms.calc = calc
|
| 83 |
|
| 84 |
+
energy = atoms.get_potential_energy()
|
| 85 |
+
forces = atoms.get_forces()
|
| 86 |
|
| 87 |
lines = [f"Total Energy: {energy:.6f} eV", "", "Atomic Forces:"]
|
| 88 |
for i, fc in enumerate(forces):
|
|
|
|
| 100 |
run_relaxation_simulation,
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# ==== Wrappers con debug ====
|
| 104 |
def md_wrapper(structure_file, charge, spin, steps, tempK, timestep_fs, ensemble):
|
| 105 |
try:
|
| 106 |
if not structure_file:
|
| 107 |
return ("Error: Please upload a structure file", None, "", "", "", None)
|
| 108 |
|
|
|
|
| 109 |
file_path = structure_file
|
| 110 |
+
print(f"MD Wrapper: Processing {file_path}")
|
| 111 |
|
| 112 |
traj_path, log_text, script_text, explanation = run_md_simulation(
|
| 113 |
file_path,
|
|
|
|
| 121 |
)
|
| 122 |
status = f"MD completed: {int(steps)} steps at {int(tempK)} K ({ensemble})"
|
| 123 |
|
| 124 |
+
print(f"MD completed, trajectory: {traj_path}")
|
| 125 |
+
|
| 126 |
# Preparar archivo PDB para el visor 3D
|
| 127 |
pdb_file = prepare_molecule_for_viewer(traj_path)
|
| 128 |
+
print(f"PDB file for viewer: {pdb_file}")
|
| 129 |
|
| 130 |
return (status, traj_path, log_text, script_text, explanation, pdb_file)
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
+
print(f"MD Wrapper Error: {e}")
|
| 134 |
return (f"Error: {e}", None, "", "", "", None)
|
| 135 |
|
| 136 |
def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
|
|
|
|
| 138 |
if not structure_file:
|
| 139 |
return ("Error: Please upload a structure file", None, "", "", "", None)
|
| 140 |
|
|
|
|
| 141 |
file_path = structure_file
|
| 142 |
+
print(f"Relax Wrapper: Processing {file_path}")
|
| 143 |
|
| 144 |
traj_path, log_text, script_text, explanation = run_relaxation_simulation(
|
| 145 |
file_path,
|
|
|
|
| 151 |
)
|
| 152 |
status = f"Relaxation finished (≤ {int(steps)} steps, fmax={float(fmax)} eV/Å)"
|
| 153 |
|
| 154 |
+
print(f"Relaxation completed, trajectory: {traj_path}")
|
| 155 |
+
|
| 156 |
# Preparar archivo PDB para el visor 3D
|
| 157 |
pdb_file = prepare_molecule_for_viewer(traj_path)
|
| 158 |
+
print(f"PDB file for viewer: {pdb_file}")
|
| 159 |
|
| 160 |
return (status, traj_path, log_text, script_text, explanation, pdb_file)
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
+
print(f"Relax Wrapper Error: {e}")
|
| 164 |
return (f"Error: {e}", None, "", "", "", None)
|
| 165 |
|
| 166 |
# ==== UI ====
|
|
|
|
| 217 |
with gr.Column(variant="panel", min_width=520):
|
| 218 |
md_status = gr.Textbox(label="MD Status", interactive=False)
|
| 219 |
md_traj = gr.File(label="Trajectory (.traj)", interactive=False)
|
| 220 |
+
|
| 221 |
+
# Molecule3D viewer con configuraciones adicionales
|
| 222 |
+
md_viewer = Molecule3D(
|
| 223 |
+
label="3D Structure Viewer",
|
| 224 |
+
reps=[
|
| 225 |
+
{
|
| 226 |
+
"model": 0,
|
| 227 |
+
"chain": "",
|
| 228 |
+
"resname": "",
|
| 229 |
+
"style": "stick",
|
| 230 |
+
"color": "whiteCarbon",
|
| 231 |
+
"residue_range": "",
|
| 232 |
+
"around": 0,
|
| 233 |
+
"byres": False,
|
| 234 |
+
"visible": True,
|
| 235 |
+
"opacity": 1.0
|
| 236 |
+
},
|
| 237 |
+
{
|
| 238 |
+
"model": 0,
|
| 239 |
+
"chain": "",
|
| 240 |
+
"resname": "",
|
| 241 |
+
"style": "sphere",
|
| 242 |
+
"color": "whiteCarbon",
|
| 243 |
+
"residue_range": "",
|
| 244 |
+
"around": 0,
|
| 245 |
+
"byres": False,
|
| 246 |
+
"visible": True,
|
| 247 |
+
"opacity": 0.7
|
| 248 |
+
}
|
| 249 |
+
]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
md_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
|
| 253 |
md_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
|
| 254 |
md_explain = gr.Markdown()
|
|
|
|
| 283 |
with gr.Column(variant="panel", min_width=520):
|
| 284 |
rlx_status = gr.Textbox(label="Status", interactive=False)
|
| 285 |
rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False)
|
| 286 |
+
|
| 287 |
+
# Molecule3D viewer con configuraciones adicionales
|
| 288 |
+
rlx_viewer = Molecule3D(
|
| 289 |
+
label="3D Structure Viewer",
|
| 290 |
+
reps=[
|
| 291 |
+
{
|
| 292 |
+
"model": 0,
|
| 293 |
+
"chain": "",
|
| 294 |
+
"resname": "",
|
| 295 |
+
"style": "stick",
|
| 296 |
+
"color": "whiteCarbon",
|
| 297 |
+
"residue_range": "",
|
| 298 |
+
"around": 0,
|
| 299 |
+
"byres": False,
|
| 300 |
+
"visible": True,
|
| 301 |
+
"opacity": 1.0
|
| 302 |
+
},
|
| 303 |
+
{
|
| 304 |
+
"model": 0,
|
| 305 |
+
"chain": "",
|
| 306 |
+
"resname": "",
|
| 307 |
+
"style": "sphere",
|
| 308 |
+
"color": "whiteCarbon",
|
| 309 |
+
"residue_range": "",
|
| 310 |
+
"around": 0,
|
| 311 |
+
"byres": False,
|
| 312 |
+
"visible": True,
|
| 313 |
+
"opacity": 0.7
|
| 314 |
+
}
|
| 315 |
+
]
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
rlx_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
|
| 319 |
rlx_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
|
| 320 |
rlx_explain = gr.Markdown()
|