annabossler commited on
Commit
d6059b9
·
verified ·
1 Parent(s): 820c44b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -77
app.py CHANGED
@@ -6,8 +6,7 @@ from ase.io import read, write
6
  from ase.io.trajectory import Trajectory
7
  import hashlib
8
 
9
- # ==== Forzar visor HTML con 3Dmol.js ====
10
- # (desactiva Molecule3D porque requiere formatos con conectividad)
11
  HAVE_MOL3D = False
12
 
13
  # ==== Fallback HTML con 3Dmol.js ====
@@ -93,15 +92,21 @@ def _load_orbmol_calc():
93
 
94
  def predict_molecule(structure_file, charge=0, spin_multiplicity=1):
95
  """
96
- Single Point Energy + fuerzas (OrbMol). Ahora acepta archivos subidos.
97
  """
98
  try:
99
  calc = _load_orbmol_calc()
100
  if not structure_file:
101
  return "Error: Please upload a structure file", "Error"
102
 
103
- # structure_file es un objeto File de Gradio
104
- file_path = structure_file.name if hasattr(structure_file, 'name') else structure_file
 
 
 
 
 
 
105
 
106
  atoms = read(file_path)
107
  atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
@@ -126,17 +131,22 @@ from simulation_scripts_orbmol import (
126
  run_relaxation_simulation,
127
  )
128
 
129
- # ==== Wrappers: ahora usan archivos subidos ====
130
  def md_wrapper(structure_file, charge, spin, steps, tempK, timestep_fs, ensemble):
131
  try:
132
  if not structure_file:
133
  return ("Error: Please upload a structure file", None, "", "", "", None, "")
134
 
135
- # structure_file es un objeto File de Gradio
136
- file_path = structure_file.name if hasattr(structure_file, 'name') else structure_file
 
 
 
 
 
137
 
138
  traj_path, log_text, script_text, explanation = run_md_simulation(
139
- file_path, # Pasar el path del archivo directamente
140
  int(steps),
141
  20,
142
  float(timestep_fs),
@@ -158,11 +168,16 @@ def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
158
  if not structure_file:
159
  return ("Error: Please upload a structure file", None, "", "", "", None, "")
160
 
161
- # structure_file es un objeto File de Gradio
162
- file_path = structure_file.name if hasattr(structure_file, 'name') else structure_file
 
 
 
 
 
163
 
164
  traj_path, log_text, script_text, explanation = run_relaxation_simulation(
165
- file_path, # Pasar el path del archivo directamente
166
  int(steps),
167
  float(fmax),
168
  int(charge),
@@ -187,11 +202,12 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
187
  gr.Markdown("# OrbMol — Quantum-Accurate Molecular Predictions")
188
  gr.Markdown("Upload molecular structure files (.xyz, .pdb, .cif, .traj) for energy and force calculations.")
189
 
190
- # CAMBIADO: File upload en lugar de Textbox
191
  xyz_input = gr.File(
192
- label="Upload Structure File (.xyz/.pdb/.cif/.traj)",
193
  file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
194
- file_count="single"
 
195
  )
196
 
197
  with gr.Row():
@@ -203,9 +219,6 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
203
  spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
204
  spe_status = gr.Textbox(label="Status", interactive=False, max_lines=1)
205
 
206
- # EJEMPLOS DE ARCHIVOS - puedes comentar esta línea si no quieres ejemplos
207
- # gr.Examples(examples=[["examples/water.xyz", 0, 1]], inputs=[xyz_input, charge_input, spin_input])
208
-
209
  run_spe.click(predict_molecule, [xyz_input, charge_input, spin_input], [spe_out, spe_status])
210
 
211
  # -------- MD --------
@@ -215,11 +228,11 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
215
  gr.Markdown("## Molecular Dynamics Simulation")
216
  gr.Markdown("Upload your molecular structure and configure MD parameters.")
217
 
218
- # CAMBIADO: File upload en lugar de Textbox
219
  xyz_md = gr.File(
220
- label="Upload Structure File (.xyz/.pdb/.cif/.traj)",
221
  file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
222
- file_count="single"
 
223
  )
224
 
225
  with gr.Row():
@@ -234,59 +247,4 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
234
  run_md_btn = gr.Button("Run MD Simulation", variant="primary")
235
 
236
  with gr.Column(variant="panel", min_width=520):
237
- md_status = gr.Textbox(label="MD Status", interactive=False)
238
- md_traj = gr.File(label="Trajectory (.traj)", interactive=False)
239
- md_viewer_placeholder = gr.HTML(visible=False) # no usamos Molecule3D
240
- md_html = gr.HTML(label="Trajectory Viewer")
241
- md_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
242
- md_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
243
- md_explain = gr.Markdown()
244
-
245
- run_md_btn.click(
246
- md_wrapper,
247
- inputs=[xyz_md, charge_md, spin_md, steps_md, temp_md, timestep_md, ensemble_md],
248
- outputs=[md_status, md_traj, md_log, md_script, md_explain, md_viewer_placeholder, md_html],
249
- )
250
-
251
- # -------- Relax --------
252
- with gr.Tab("Relaxation / Optimization"):
253
- with gr.Row():
254
- with gr.Column(scale=2):
255
- gr.Markdown("## Structure Relaxation/Optimization")
256
- gr.Markdown("Upload your molecular structure for geometry optimization.")
257
-
258
- # CAMBIADO: File upload en lugar de Textbox
259
- xyz_rlx = gr.File(
260
- label="Upload Structure File (.xyz/.pdb/.cif/.traj)",
261
- file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
262
- file_count="single"
263
- )
264
-
265
- steps_rlx = gr.Slider(minimum=1, maximum=2000, value=300, step=1, label="Max Steps")
266
- fmax_rlx = gr.Slider(minimum=0.001, maximum=0.5, value=0.05, step=0.001, label="Fmax (eV/Å)")
267
- with gr.Row():
268
- charge_rlx = gr.Slider(minimum=-10, maximum=10, value=0, step=1, label="Charge")
269
- spin_rlx = gr.Slider(minimum=1, maximum=11, value=1, step=1, label="Spin")
270
- relax_cell = gr.Checkbox(False, label="Relax Unit Cell")
271
- run_rlx_btn = gr.Button("Run Optimization", variant="primary")
272
-
273
- with gr.Column(variant="panel", min_width=520):
274
- rlx_status = gr.Textbox(label="Status", interactive=False)
275
- rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False)
276
- rlx_viewer_placeholder = gr.HTML(visible=False)
277
- rlx_html = gr.HTML(label="Final Structure")
278
- rlx_log = gr.Textbox(label="Log", interactive=False, lines=15, max_lines=25)
279
- rlx_script = gr.Code(label="Reproduction Script", language="python", interactive=False, lines=20, max_lines=30)
280
- rlx_explain = gr.Markdown()
281
-
282
- run_rlx_btn.click(
283
- relax_wrapper,
284
- inputs=[xyz_rlx, steps_rlx, fmax_rlx, charge_rlx, spin_rlx, relax_cell],
285
- outputs=[rlx_status, rlx_traj, rlx_log, rlx_script, rlx_explain, rlx_viewer_placeholder, rlx_html],
286
- )
287
-
288
- print("Starting OrbMol model loading…")
289
- _ = _load_orbmol_calc()
290
-
291
- if __name__ == "__main__":
292
- demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
6
  from ase.io.trajectory import Trajectory
7
  import hashlib
8
 
9
+ # ==== Forzar visor HTML con 3Dmol.js (sin gradio_molecule3d) ====
 
10
  HAVE_MOL3D = False
11
 
12
  # ==== Fallback HTML con 3Dmol.js ====
 
92
 
93
  def predict_molecule(structure_file, charge=0, spin_multiplicity=1):
94
  """
95
+ Single Point Energy + fuerzas (OrbMol). Maneja archivos de Gradio correctamente.
96
  """
97
  try:
98
  calc = _load_orbmol_calc()
99
  if not structure_file:
100
  return "Error: Please upload a structure file", "Error"
101
 
102
+ # En Gradio (con type='filepath'), structure_file es directamente un path (str)
103
+ file_path = structure_file
104
+
105
+ # Verificar que el archivo existe y no está vacío
106
+ if not os.path.exists(file_path):
107
+ return f"Error: File not found: {file_path}", "Error"
108
+ if os.path.getsize(file_path) == 0:
109
+ return f"Error: Empty file: {file_path}", "Error"
110
 
111
  atoms = read(file_path)
112
  atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
 
131
  run_relaxation_simulation,
132
  )
133
 
134
+ # ==== Wrappers (MD / Relax): usan directamente el path ====
135
  def md_wrapper(structure_file, charge, spin, steps, tempK, timestep_fs, ensemble):
136
  try:
137
  if not structure_file:
138
  return ("Error: Please upload a structure file", None, "", "", "", None, "")
139
 
140
+ file_path = structure_file # ya es un path (str)
141
+
142
+ # Validaciones rápidas
143
+ if not os.path.exists(file_path):
144
+ return ("Error: File not found: " + str(file_path), None, "", "", "", None, "")
145
+ if os.path.getsize(file_path) == 0:
146
+ return ("Error: Empty file: " + str(file_path), None, "", "", "", None, "")
147
 
148
  traj_path, log_text, script_text, explanation = run_md_simulation(
149
+ file_path,
150
  int(steps),
151
  20,
152
  float(timestep_fs),
 
168
  if not structure_file:
169
  return ("Error: Please upload a structure file", None, "", "", "", None, "")
170
 
171
+ file_path = structure_file # ya es un path (str)
172
+
173
+ # Validaciones rápidas
174
+ if not os.path.exists(file_path):
175
+ return ("Error: File not found: " + str(file_path), None, "", "", "", None, "")
176
+ if os.path.getsize(file_path) == 0:
177
+ return ("Error: Empty file: " + str(file_path), None, "", "", "", None, "")
178
 
179
  traj_path, log_text, script_text, explanation = run_relaxation_simulation(
180
+ file_path,
181
  int(steps),
182
  float(fmax),
183
  int(charge),
 
202
  gr.Markdown("# OrbMol — Quantum-Accurate Molecular Predictions")
203
  gr.Markdown("Upload molecular structure files (.xyz, .pdb, .cif, .traj) for energy and force calculations.")
204
 
205
+ # IMPORTANTE: type='filepath'
206
  xyz_input = gr.File(
207
+ label="Upload Structure File (.xyz/.pdb/.cif/.traj/.mol/.sdf)",
208
  file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
209
+ file_count="single",
210
+ type="filepath"
211
  )
212
 
213
  with gr.Row():
 
219
  spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
220
  spe_status = gr.Textbox(label="Status", interactive=False, max_lines=1)
221
 
 
 
 
222
  run_spe.click(predict_molecule, [xyz_input, charge_input, spin_input], [spe_out, spe_status])
223
 
224
  # -------- MD --------
 
228
  gr.Markdown("## Molecular Dynamics Simulation")
229
  gr.Markdown("Upload your molecular structure and configure MD parameters.")
230
 
 
231
  xyz_md = gr.File(
232
+ label="Upload Structure File (.xyz/.pdb/.cif/.traj/.mol/.sdf)",
233
  file_types=[".xyz", ".pdb", ".cif", ".traj", ".mol", ".sdf"],
234
+ file_count="single",
235
+ type="filepath"
236
  )
237
 
238
  with gr.Row():
 
247
  run_md_btn = gr.Button("Run MD Simulation", variant="primary")
248
 
249
  with gr.Column(variant="panel", min_width=520):
250
+ md_status = gr.Text_