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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -78
app.py CHANGED
@@ -55,11 +55,9 @@ if (typeof window.$3Dmol === 'undefined') {{
55
  function initViewer_{viewer_id}() {{
56
  var el = document.getElementById("{viewer_id}");
57
  if (!el || typeof $3Dmol === "undefined") return;
58
-
59
  var viewer = $3Dmol.createViewer(el, {{backgroundColor: 'white'}});
60
  var frames = {frames_json};
61
  var currentFrame = 0;
62
-
63
  function showFrame(i) {{
64
  viewer.clear();
65
  viewer.addModel(frames[i], "xyz");
@@ -67,7 +65,6 @@ function initViewer_{viewer_id}() {{
67
  viewer.zoomTo();
68
  viewer.render();
69
  }}
70
-
71
  showFrame(0);
72
  if (frames.length > 1) {{
73
  setInterval(function() {{
@@ -94,20 +91,19 @@ def _load_orbmol_calc():
94
  _MODEL_CALC = ORBCalculator(orbff, device="cpu")
95
  return _MODEL_CALC
96
 
97
- def predict_molecule(xyz_content, charge=0, spin_multiplicity=1):
98
  """
99
- Single Point Energy + fuerzas (OrbMol). Toma XYZ en texto.
100
  """
101
  try:
102
  calc = _load_orbmol_calc()
103
- if not xyz_content or not xyz_content.strip():
104
- return "Error: Please enter XYZ coordinates", "Error"
105
 
106
- with tempfile.NamedTemporaryFile(mode="w", suffix=".xyz", delete=False) as f:
107
- f.write(xyz_content)
108
- xyz_file = f.name
109
 
110
- atoms = read(xyz_file)
111
  atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
112
  atoms.calc = calc
113
 
@@ -120,11 +116,6 @@ def predict_molecule(xyz_content, charge=0, spin_multiplicity=1):
120
  max_force = float(np.max(np.linalg.norm(forces, axis=1)))
121
  lines += ["", f"Max Force: {max_force:.4f} eV/Å"]
122
 
123
- try:
124
- os.unlink(xyz_file)
125
- except Exception:
126
- pass
127
-
128
  return "\n".join(lines), "Calculation completed with OrbMol"
129
  except Exception as e:
130
  return f"Error during calculation: {e}", "Error"
@@ -135,31 +126,17 @@ from simulation_scripts_orbmol import (
135
  run_relaxation_simulation,
136
  )
137
 
138
- def _string_looks_like_xyz(text: str) -> bool:
 
139
  try:
140
- first = (text or "").strip().splitlines()[0]
141
- int(first.split()[0])
142
- return True
143
- except Exception:
144
- return False
145
-
146
- def _to_file_if_xyz(input_or_path: str):
147
- if isinstance(input_or_path, str) and _string_looks_like_xyz(input_or_path):
148
- tf = tempfile.NamedTemporaryFile(mode="w", suffix=".xyz", delete=False)
149
- tf.write(input_or_path)
150
- tf.flush(); tf.close()
151
- return tf.name, True
152
- return input_or_path, False
153
-
154
- # ==== Wrappers: devuelven HTML (3Dmol.js) ====
155
- def md_wrapper(xyz_content, charge, spin, steps, tempK, timestep_fs, ensemble):
156
- tmp_created = False
157
- path_or_str = xyz_content
158
- try:
159
- path_or_str, tmp_created = _to_file_if_xyz(xyz_content)
160
 
161
  traj_path, log_text, script_text, explanation = run_md_simulation(
162
- path_or_str,
163
  int(steps),
164
  20,
165
  float(timestep_fs),
@@ -175,19 +152,17 @@ def md_wrapper(xyz_content, charge, spin, steps, tempK, timestep_fs, ensemble):
175
 
176
  except Exception as e:
177
  return (f"Error: {e}", None, "", "", "", None, "")
178
- finally:
179
- if tmp_created and isinstance(path_or_str, str) and os.path.exists(path_or_str):
180
- try: os.remove(path_or_str)
181
- except Exception: pass
182
-
183
- def relax_wrapper(xyz_content, steps, fmax, charge, spin, relax_cell):
184
- tmp_created = False
185
- path_or_str = xyz_content
186
  try:
187
- path_or_str, tmp_created = _to_file_if_xyz(xyz_content)
 
 
 
 
188
 
189
  traj_path, log_text, script_text, explanation = run_relaxation_simulation(
190
- path_or_str,
191
  int(steps),
192
  float(fmax),
193
  int(charge),
@@ -201,30 +176,6 @@ def relax_wrapper(xyz_content, steps, fmax, charge, spin, relax_cell):
201
 
202
  except Exception as e:
203
  return (f"Error: {e}", None, "", "", "", None, "")
204
- finally:
205
- if tmp_created and isinstance(path_or_str, str) and os.path.exists(path_or_str):
206
- try: os.remove(path_or_str)
207
- except Exception: pass
208
-
209
- # ==== Ejemplos ====
210
- examples = [
211
- ["""2
212
- Hydrogen molecule
213
- H 0.0 0.0 0.0
214
- H 0.0 0.0 0.74""", 0, 1],
215
- ["""3
216
- Water molecule
217
- O 0.0000 0.0000 0.0000
218
- H 0.7571 0.0000 0.5864
219
- H -0.7571 0.0000 0.5864""", 0, 1],
220
- ["""5
221
- Methane
222
- C 0.0000 0.0000 0.0000
223
- H 1.0890 0.0000 0.0000
224
- H -0.3630 1.0267 0.0000
225
- H -0.3630 -0.5133 0.8887
226
- H -0.3630 -0.5133 -0.8887""", 0, 1],
227
- ]
228
 
229
  # ==== UI ====
230
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
@@ -234,24 +185,43 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
234
  with gr.Row():
235
  with gr.Column(scale=2):
236
  gr.Markdown("# OrbMol — Quantum-Accurate Molecular Predictions")
237
- gr.Markdown("Energías y fuerzas con **charge** y **spin multiplicity**.")
238
- xyz_input = gr.Textbox(label="XYZ Coordinates", lines=12, placeholder="Paste XYZ here…")
 
 
 
 
 
 
 
239
  with gr.Row():
240
  charge_input = gr.Slider(minimum=-10, maximum=10, value=0, step=1, label="Charge")
241
  spin_input = gr.Slider(minimum=1, maximum=11, value=1, step=1, label="Spin Multiplicity")
242
  run_spe = gr.Button("Run OrbMol Prediction", variant="primary")
 
243
  with gr.Column(variant="panel", min_width=500):
244
  spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
245
  spe_status = gr.Textbox(label="Status", interactive=False, max_lines=1)
246
 
247
- gr.Examples(examples=examples, inputs=[xyz_input, charge_input, spin_input])
 
 
248
  run_spe.click(predict_molecule, [xyz_input, charge_input, spin_input], [spe_out, spe_status])
249
 
250
  # -------- MD --------
251
  with gr.Tab("Molecular Dynamics"):
252
  with gr.Row():
253
  with gr.Column(scale=2):
254
- xyz_md = gr.Textbox(label="XYZ Coordinates or path (.xyz/.traj/.pdb/.cif)", lines=12, placeholder="Paste XYZ or path here…")
 
 
 
 
 
 
 
 
 
255
  with gr.Row():
256
  charge_md = gr.Slider(minimum=-10, maximum=10, value=0, step=1, label="Charge")
257
  spin_md = gr.Slider(minimum=1, maximum=11, value=1, step=1, label="Spin Multiplicity")
@@ -282,7 +252,16 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
282
  with gr.Tab("Relaxation / Optimization"):
283
  with gr.Row():
284
  with gr.Column(scale=2):
285
- xyz_rlx = gr.Textbox(label="XYZ Coordinates or path (.xyz/.traj/.pdb/.cif)", lines=12, placeholder="Paste XYZ or path here…")
 
 
 
 
 
 
 
 
 
286
  steps_rlx = gr.Slider(minimum=1, maximum=2000, value=300, step=1, label="Max Steps")
287
  fmax_rlx = gr.Slider(minimum=0.001, maximum=0.5, value=0.05, step=0.001, label="Fmax (eV/Å)")
288
  with gr.Row():
@@ -310,4 +289,4 @@ print("Starting OrbMol model loading…")
310
  _ = _load_orbmol_calc()
311
 
312
  if __name__ == "__main__":
313
- demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
55
  function initViewer_{viewer_id}() {{
56
  var el = document.getElementById("{viewer_id}");
57
  if (!el || typeof $3Dmol === "undefined") return;
 
58
  var viewer = $3Dmol.createViewer(el, {{backgroundColor: 'white'}});
59
  var frames = {frames_json};
60
  var currentFrame = 0;
 
61
  function showFrame(i) {{
62
  viewer.clear();
63
  viewer.addModel(frames[i], "xyz");
 
65
  viewer.zoomTo();
66
  viewer.render();
67
  }}
 
68
  showFrame(0);
69
  if (frames.length > 1) {{
70
  setInterval(function() {{
 
91
  _MODEL_CALC = ORBCalculator(orbff, device="cpu")
92
  return _MODEL_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)}
108
  atoms.calc = calc
109
 
 
116
  max_force = float(np.max(np.linalg.norm(forces, axis=1)))
117
  lines += ["", f"Max Force: {max_force:.4f} eV/Å"]
118
 
 
 
 
 
 
119
  return "\n".join(lines), "Calculation completed with OrbMol"
120
  except Exception as e:
121
  return f"Error during calculation: {e}", "Error"
 
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),
 
152
 
153
  except Exception as e:
154
  return (f"Error: {e}", None, "", "", "", None, "")
155
+
156
+ def relax_wrapper(structure_file, steps, fmax, charge, spin, relax_cell):
 
 
 
 
 
 
157
  try:
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),
 
176
 
177
  except Exception as e:
178
  return (f"Error: {e}", None, "", "", "", None, "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  # ==== UI ====
181
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
 
185
  with gr.Row():
186
  with gr.Column(scale=2):
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():
198
  charge_input = gr.Slider(minimum=-10, maximum=10, value=0, step=1, label="Charge")
199
  spin_input = gr.Slider(minimum=1, maximum=11, value=1, step=1, label="Spin Multiplicity")
200
  run_spe = gr.Button("Run OrbMol Prediction", variant="primary")
201
+
202
  with gr.Column(variant="panel", min_width=500):
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 --------
212
  with gr.Tab("Molecular Dynamics"):
213
  with gr.Row():
214
  with gr.Column(scale=2):
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():
226
  charge_md = gr.Slider(minimum=-10, maximum=10, value=0, step=1, label="Charge")
227
  spin_md = gr.Slider(minimum=1, maximum=11, value=1, step=1, label="Spin Multiplicity")
 
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():
 
289
  _ = _load_orbmol_calc()
290
 
291
  if __name__ == "__main__":
292
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)