annabossler commited on
Commit
bfe7cbb
·
verified ·
1 Parent(s): e6d00b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -17,7 +17,7 @@ from orb_models.forcefield import pretrained
17
  from orb_models.forcefield.calculator import ORBCalculator
18
 
19
  # -----------------------------
20
- # Global model
21
  # -----------------------------
22
  model_calc = None
23
 
@@ -74,7 +74,7 @@ def predict_molecule(xyz_content, charge=0, spin_multiplicity=1):
74
  return f"❌ Error during calculation: {str(e)}", "Error"
75
 
76
  # -----------------------------
77
- # Helper: convert trajectory → HTML animation
78
  # -----------------------------
79
  def traj_to_html(traj_file):
80
  traj = Trajectory(traj_file)
@@ -89,10 +89,10 @@ def traj_to_html(traj_file):
89
  view.setStyle({"stick": {}})
90
  view.zoomTo()
91
  view.animate({"loop": "forward"})
92
- return view._make_html()
93
 
94
  # -----------------------------
95
- # Molecular dynamics simulation
96
  # -----------------------------
97
  def run_md(xyz_content, charge=0, spin_multiplicity=1, steps=100, temperature=300, timestep=1.0):
98
  try:
@@ -111,11 +111,11 @@ def run_md(xyz_content, charge=0, spin_multiplicity=1, steps=100, temperature=30
111
  atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
112
  atoms.calc = calc
113
 
114
- # Pre-relaxation
115
  opt = LBFGS(atoms)
116
  opt.run(fmax=0.05, steps=20)
117
 
118
- # Velocities
119
  MaxwellBoltzmannDistribution(atoms, temperature_K=2 * temperature)
120
 
121
  # MD setup
@@ -135,20 +135,20 @@ def run_md(xyz_content, charge=0, spin_multiplicity=1, steps=100, temperature=30
135
  return f"❌ Error during MD simulation: {str(e)}", ""
136
 
137
  # -----------------------------
138
- # Gradio UI
139
  # -----------------------------
140
  examples = [
141
  ["""2
142
  Hydrogen molecule
143
  H 0.0 0.0 0.0
144
  H 0.0 0.0 0.74""", 0, 1],
145
-
146
  ["""3
147
  Water molecule
148
  O 0.0000 0.0000 0.0000
149
  H 0.7571 0.0000 0.5864
150
  H -0.7571 0.0000 0.5864""", 0, 1],
151
-
152
  ["""5
153
  Methane
154
  C 0.0000 0.0000 0.0000
@@ -158,25 +158,34 @@ H -0.3630 -0.5133 0.8887
158
  H -0.3630 -0.5133 -0.8887""", 0, 1]
159
  ]
160
 
 
 
 
161
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol + MD Demo") as demo:
162
- gr.Markdown("# OrbMol Demo with Molecular Dynamics")
163
-
164
  with gr.Tab("Single Point Energy"):
165
- xyz_input = gr.Textbox(label="XYZ Coordinates", lines=12)
166
- charge_input = gr.Slider(value=0, minimum=-10, maximum=10, step=1, label="Charge")
167
- spin_input = gr.Slider(value=1, minimum=1, maximum=11, step=1, label="Spin Multiplicity")
168
- run_btn = gr.Button("Run OrbMol Calculation")
169
- results_output = gr.Textbox(label="Results", lines=15)
170
- status_output = gr.Textbox(label="Status")
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  run_btn.click(
172
  predict_molecule,
173
  inputs=[xyz_input, charge_input, spin_input],
174
- outputs=[results_output, status_output],
175
- )
176
- gr.Examples(
177
- examples=examples,
178
- inputs=[xyz_input, charge_input, spin_input],
179
- label="Try these examples"
180
  )
181
 
182
  with gr.Tab("Molecular Dynamics"):
@@ -186,7 +195,7 @@ with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol + MD Demo") as demo:
186
  steps_input = gr.Slider(value=100, minimum=10, maximum=1000, step=10, label="Steps")
187
  temp_input = gr.Slider(value=300, minimum=10, maximum=1000, step=10, label="Temperature (K)")
188
  timestep_input = gr.Slider(value=1.0, minimum=0.1, maximum=5.0, step=0.1, label="Timestep (fs)")
189
- run_md_btn = gr.Button("Run MD Simulation")
190
  md_status = gr.Textbox(label="MD Status", lines=2)
191
  md_view = gr.HTML()
192
  run_md_btn.click(
 
17
  from orb_models.forcefield.calculator import ORBCalculator
18
 
19
  # -----------------------------
20
+ # Global OrbMol model
21
  # -----------------------------
22
  model_calc = None
23
 
 
74
  return f"❌ Error during calculation: {str(e)}", "Error"
75
 
76
  # -----------------------------
77
+ # Trajectory → HTML for Gradio
78
  # -----------------------------
79
  def traj_to_html(traj_file):
80
  traj = Trajectory(traj_file)
 
89
  view.setStyle({"stick": {}})
90
  view.zoomTo()
91
  view.animate({"loop": "forward"})
92
+ return view.render() # ✅ en Gradio funciona mejor que _make_html()
93
 
94
  # -----------------------------
95
+ # MD simulation
96
  # -----------------------------
97
  def run_md(xyz_content, charge=0, spin_multiplicity=1, steps=100, temperature=300, timestep=1.0):
98
  try:
 
111
  atoms.info = {"charge": int(charge), "spin": int(spin_multiplicity)}
112
  atoms.calc = calc
113
 
114
+ # Pre-relax
115
  opt = LBFGS(atoms)
116
  opt.run(fmax=0.05, steps=20)
117
 
118
+ # Initialize velocities
119
  MaxwellBoltzmannDistribution(atoms, temperature_K=2 * temperature)
120
 
121
  # MD setup
 
135
  return f"❌ Error during MD simulation: {str(e)}", ""
136
 
137
  # -----------------------------
138
+ # Examples
139
  # -----------------------------
140
  examples = [
141
  ["""2
142
  Hydrogen molecule
143
  H 0.0 0.0 0.0
144
  H 0.0 0.0 0.74""", 0, 1],
145
+
146
  ["""3
147
  Water molecule
148
  O 0.0000 0.0000 0.0000
149
  H 0.7571 0.0000 0.5864
150
  H -0.7571 0.0000 0.5864""", 0, 1],
151
+
152
  ["""5
153
  Methane
154
  C 0.0000 0.0000 0.0000
 
158
  H -0.3630 -0.5133 -0.8887""", 0, 1]
159
  ]
160
 
161
+ # -----------------------------
162
+ # Gradio UI
163
+ # -----------------------------
164
  with gr.Blocks(theme=gr.themes.Ocean(), title="OrbMol + MD Demo") as demo:
 
 
165
  with gr.Tab("Single Point Energy"):
166
+ with gr.Row():
167
+ with gr.Column(scale=2):
168
+ with gr.Column(variant="panel"):
169
+ gr.Markdown("# OrbMol Demo - Quantum-Accurate Molecular Predictions")
170
+ xyz_input = gr.Textbox(
171
+ label="XYZ Coordinates", lines=12,
172
+ placeholder="Paste XYZ here"
173
+ )
174
+ with gr.Row():
175
+ charge_input = gr.Slider(value=0, label="Charge", minimum=-10, maximum=10, step=1)
176
+ spin_input = gr.Slider(value=1, maximum=11, minimum=1, step=1, label="Spin Multiplicity")
177
+ run_btn = gr.Button("Run OrbMol Prediction", variant="primary", size="lg")
178
+ with gr.Column(variant="panel", min_width=500):
179
+ gr.Markdown("## Results")
180
+ results_output = gr.Textbox(label="Energy & Forces", lines=15, interactive=False)
181
+ status_output = gr.Textbox(label="Status", interactive=False, max_lines=1)
182
+
183
+ gr.Examples(examples=examples, inputs=[xyz_input, charge_input, spin_input])
184
+
185
  run_btn.click(
186
  predict_molecule,
187
  inputs=[xyz_input, charge_input, spin_input],
188
+ outputs=[results_output, status_output]
 
 
 
 
 
189
  )
190
 
191
  with gr.Tab("Molecular Dynamics"):
 
195
  steps_input = gr.Slider(value=100, minimum=10, maximum=1000, step=10, label="Steps")
196
  temp_input = gr.Slider(value=300, minimum=10, maximum=1000, step=10, label="Temperature (K)")
197
  timestep_input = gr.Slider(value=1.0, minimum=0.1, maximum=5.0, step=0.1, label="Timestep (fs)")
198
+ run_md_btn = gr.Button("Run MD Simulation", variant="primary")
199
  md_status = gr.Textbox(label="MD Status", lines=2)
200
  md_view = gr.HTML()
201
  run_md_btn.click(