annabossler commited on
Commit
02d3f79
·
verified ·
1 Parent(s): 1b94f8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -45,13 +45,13 @@ def predict_molecule(structure_file, task_name, charge=0, spin_multiplicity=1):
45
  try:
46
  calc = load_orbmol_model(task_name)
47
  if not structure_file:
48
- return "Error: Please upload a structure file", "Error", None
49
 
50
  file_path = structure_file
51
  if not os.path.exists(file_path):
52
- return f"Error: File not found: {file_path}", "Error", None
53
  if os.path.getsize(file_path) == 0:
54
- return f"Error: Empty file: {file_path}", "Error", None
55
 
56
  atoms = read(file_path)
57
 
@@ -74,19 +74,18 @@ def predict_molecule(structure_file, task_name, charge=0, spin_multiplicity=1):
74
  max_force = float(np.max(np.linalg.norm(forces, axis=1)))
75
  lines += ["", f"Max Force: {max_force:.4f} eV/Å"]
76
 
77
- # CLAVE: Devolver archivo original, NO un PDB procesado
78
- return "\n".join(lines), f"✅ Calculation completed with {task_name}", file_path
79
 
80
  except Exception as e:
81
  import traceback
82
  traceback.print_exc()
83
- return f"Error during calculation: {e}", "Error", None
84
 
85
  # ==== Wrappers MD y Relax ====
86
  def md_wrapper(structure_file, task_name, 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
  traj_path, log_text, script_text, explanation = run_md_simulation(
92
  structure_file,
@@ -99,20 +98,19 @@ def md_wrapper(structure_file, task_name, charge, spin, steps, tempK, timestep_f
99
  int(charge),
100
  int(spin),
101
  )
102
- status = f"MD completed: {int(steps)} steps at {int(tempK)} K ({ensemble})"
103
 
104
- # CLAVE: Devolver traj_path directamente
105
- return (status, traj_path, log_text, script_text, explanation, traj_path)
106
 
107
  except Exception as e:
108
  import traceback
109
  traceback.print_exc()
110
- return (f"Error: {e}", None, "", "", "", None)
111
 
112
  def relax_wrapper(structure_file, task_name, steps, fmax, charge, spin, relax_cell):
113
  try:
114
  if not structure_file:
115
- return ("Error: Please upload a structure file", None, "", "", "", None)
116
 
117
  traj_path, log_text, script_text, explanation = run_relaxation_simulation(
118
  structure_file,
@@ -123,15 +121,14 @@ def relax_wrapper(structure_file, task_name, steps, fmax, charge, spin, relax_ce
123
  int(spin),
124
  bool(relax_cell),
125
  )
126
- status = f"Relaxation finished ({int(steps)} steps, fmax={float(fmax)} eV/Å)"
127
 
128
- # CLAVE: Devolver traj_path directamente
129
- return (status, traj_path, log_text, script_text, explanation, traj_path)
130
 
131
  except Exception as e:
132
  import traceback
133
  traceback.print_exc()
134
- return (f"Error: {e}", None, "", "", "", None)
135
 
136
  # ==== UI ====
137
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
@@ -163,7 +160,6 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
163
  spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
164
  spe_status = gr.Textbox(label="Status", interactive=False)
165
 
166
- # PATRÓN FACEBOOK UMA: inputs + value=lambda
167
  spe_viewer = Molecule3D(
168
  label="Input Structure Viewer",
169
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
@@ -186,7 +182,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
186
  run_spe.click(
187
  predict_molecule,
188
  [xyz_input, task_name_spe, charge_input, spin_input],
189
- [spe_out, spe_status, spe_viewer]
190
  )
191
 
192
  # -------- MD --------
@@ -220,7 +216,6 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
220
  md_status = gr.Textbox(label="MD Status", interactive=False)
221
  md_traj = gr.File(label="Trajectory (.traj)", interactive=False)
222
 
223
- # PATRÓN FACEBOOK UMA
224
  md_viewer = Molecule3D(
225
  label="MD Result Viewer",
226
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
@@ -247,7 +242,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
247
  run_md_btn.click(
248
  md_wrapper,
249
  [xyz_md, task_name_md, charge_md, spin_md, steps_md, temp_md, timestep_md, ensemble_md],
250
- [md_status, md_traj, md_log, md_script, md_explain, md_viewer]
251
  )
252
 
253
  # -------- Relax --------
@@ -279,7 +274,6 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
279
  rlx_status = gr.Textbox(label="Status", interactive=False)
280
  rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False)
281
 
282
- # PATRÓN FACEBOOK UMA
283
  rlx_viewer = Molecule3D(
284
  label="Optimized Structure Viewer",
285
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
@@ -306,7 +300,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
306
  run_rlx_btn.click(
307
  relax_wrapper,
308
  [xyz_rlx, task_name_rlx, steps_rlx, fmax_rlx, charge_rlx, spin_rlx, relax_cell],
309
- [rlx_status, rlx_traj, rlx_log, rlx_script, rlx_explain, rlx_viewer]
310
  )
311
 
312
  if __name__ == "__main__":
 
45
  try:
46
  calc = load_orbmol_model(task_name)
47
  if not structure_file:
48
+ return "Error: Please upload a structure file", "Error"
49
 
50
  file_path = structure_file
51
  if not os.path.exists(file_path):
52
+ return f"Error: File not found: {file_path}", "Error"
53
  if os.path.getsize(file_path) == 0:
54
+ return f"Error: Empty file: {file_path}", "Error"
55
 
56
  atoms = read(file_path)
57
 
 
74
  max_force = float(np.max(np.linalg.norm(forces, axis=1)))
75
  lines += ["", f"Max Force: {max_force:.4f} eV/Å"]
76
 
77
+ return "\n".join(lines), f"Calculation completed with {task_name}"
 
78
 
79
  except Exception as e:
80
  import traceback
81
  traceback.print_exc()
82
+ return f"Error during calculation: {e}", "Error"
83
 
84
  # ==== Wrappers MD y Relax ====
85
  def md_wrapper(structure_file, task_name, charge, spin, steps, tempK, timestep_fs, ensemble):
86
  try:
87
  if not structure_file:
88
+ return ("Error: Please upload a structure file", None, "", "", "")
89
 
90
  traj_path, log_text, script_text, explanation = run_md_simulation(
91
  structure_file,
 
98
  int(charge),
99
  int(spin),
100
  )
101
+ status = f"MD completed: {int(steps)} steps at {int(tempK)} K ({ensemble})"
102
 
103
+ return (status, traj_path, log_text, script_text, explanation)
 
104
 
105
  except Exception as e:
106
  import traceback
107
  traceback.print_exc()
108
+ return (f"Error: {e}", None, "", "", "")
109
 
110
  def relax_wrapper(structure_file, task_name, steps, fmax, charge, spin, relax_cell):
111
  try:
112
  if not structure_file:
113
+ return ("Error: Please upload a structure file", None, "", "", "")
114
 
115
  traj_path, log_text, script_text, explanation = run_relaxation_simulation(
116
  structure_file,
 
121
  int(spin),
122
  bool(relax_cell),
123
  )
124
+ status = f"Relaxation finished (<={int(steps)} steps, fmax={float(fmax)} eV/Å)"
125
 
126
+ return (status, traj_path, log_text, script_text, explanation)
 
127
 
128
  except Exception as e:
129
  import traceback
130
  traceback.print_exc()
131
+ return (f"Error: {e}", None, "", "", "")
132
 
133
  # ==== UI ====
134
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol Demo") as demo:
 
160
  spe_out = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
161
  spe_status = gr.Textbox(label="Status", interactive=False)
162
 
 
163
  spe_viewer = Molecule3D(
164
  label="Input Structure Viewer",
165
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
 
182
  run_spe.click(
183
  predict_molecule,
184
  [xyz_input, task_name_spe, charge_input, spin_input],
185
+ [spe_out, spe_status]
186
  )
187
 
188
  # -------- MD --------
 
216
  md_status = gr.Textbox(label="MD Status", interactive=False)
217
  md_traj = gr.File(label="Trajectory (.traj)", interactive=False)
218
 
 
219
  md_viewer = Molecule3D(
220
  label="MD Result Viewer",
221
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
 
242
  run_md_btn.click(
243
  md_wrapper,
244
  [xyz_md, task_name_md, charge_md, spin_md, steps_md, temp_md, timestep_md, ensemble_md],
245
+ [md_status, md_traj, md_log, md_script, md_explain]
246
  )
247
 
248
  # -------- Relax --------
 
274
  rlx_status = gr.Textbox(label="Status", interactive=False)
275
  rlx_traj = gr.File(label="Trajectory (.traj)", interactive=False)
276
 
 
277
  rlx_viewer = Molecule3D(
278
  label="Optimized Structure Viewer",
279
  reps=DEFAULT_MOLECULAR_REPRESENTATIONS,
 
300
  run_rlx_btn.click(
301
  relax_wrapper,
302
  [xyz_rlx, task_name_rlx, steps_rlx, fmax_rlx, charge_rlx, spin_rlx, relax_cell],
303
+ [rlx_status, rlx_traj, rlx_log, rlx_script, rlx_explain]
304
  )
305
 
306
  if __name__ == "__main__":