A19grey commited on
Commit
7216f39
·
1 Parent(s): 0f5f6d3

updated to working molgallery3D but still need to fix bugs for H2 and H2O, app crashing now for unknown reasons

Browse files
Files changed (2) hide show
  1. app.py +30 -14
  2. visualization.py +16 -15
app.py CHANGED
@@ -53,6 +53,15 @@ import sys
53
  import json
54
  import os
55
 
 
 
 
 
 
 
 
 
 
56
  # Load molecule definitions
57
  def load_molecules():
58
  """Load molecule definitions from JSON file."""
@@ -77,6 +86,7 @@ import gradio as gr
77
  import numpy as np
78
  from quantum_utils import run_vqe_simulation
79
  from visualization import plot_convergence, create_molecule_viewer, format_molecule_params
 
80
  import spaces
81
 
82
  # Add immediate logging
@@ -162,7 +172,9 @@ def create_interface():
162
  molecule_choice = gr.Dropdown(
163
  choices=list(molecules.keys()),
164
  value=list(molecules.keys())[0],
165
- label="Select Molecule"
 
 
166
  )
167
 
168
  # Get scale range from selected molecule
@@ -195,17 +207,20 @@ def create_interface():
195
 
196
  # Plots and visualization
197
  with gr.Row():
198
- convergence_plot = gr.Plot(
199
- label="VQE Convergence",
200
- show_label=True
201
- )
202
- # Create MolGallery3D component
203
- molecule_viz = MolGallery3D(
204
- columns=1,
205
- height=400,
206
- label="3D Molecule Viewer",
207
- automatic_rotation=True
208
- )
 
 
 
209
 
210
  def update_molecule_info(molecule_choice):
211
  """Update description and scale range when molecule is selected."""
@@ -223,8 +238,9 @@ def create_interface():
223
 
224
  # Create molecule visualization
225
  mol_list = create_molecule_viewer(molecule_choice, default_scale)
226
- if mol_list is None:
227
- mol_list = [] # Empty list if molecule creation failed
 
228
 
229
  return [
230
  params_html, # Update params display
 
53
  import json
54
  import os
55
 
56
+ def install_dependencies():
57
+ """Install required packages from requirements.txt."""
58
+ try:
59
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
60
+ print("Dependencies installed successfully")
61
+ except subprocess.CalledProcessError as e:
62
+ print(f"Error installing dependencies: {e}")
63
+ sys.exit(1)
64
+
65
  # Load molecule definitions
66
  def load_molecules():
67
  """Load molecule definitions from JSON file."""
 
86
  import numpy as np
87
  from quantum_utils import run_vqe_simulation
88
  from visualization import plot_convergence, create_molecule_viewer, format_molecule_params
89
+ from gradio_molgallery3d import MolGallery3D
90
  import spaces
91
 
92
  # Add immediate logging
 
172
  molecule_choice = gr.Dropdown(
173
  choices=list(molecules.keys()),
174
  value=list(molecules.keys())[0],
175
+ label="Select Molecule",
176
+ allow_custom_value=False, # Prevent text input
177
+ type="value" # Ensure it stays as a dropdown
178
  )
179
 
180
  # Get scale range from selected molecule
 
207
 
208
  # Plots and visualization
209
  with gr.Row():
210
+ with gr.Column(scale=1):
211
+ convergence_plot = gr.Plot(
212
+ label="VQE Convergence",
213
+ show_label=True
214
+ )
215
+ with gr.Column(scale=1):
216
+ # Create MolGallery3D component with more space
217
+ molecule_viz = MolGallery3D(
218
+ columns=1,
219
+ height=600, # Increased height
220
+ width=600, # Added explicit width
221
+ label="3D Molecule Viewer",
222
+ automatic_rotation=True
223
+ )
224
 
225
  def update_molecule_info(molecule_choice):
226
  """Update description and scale range when molecule is selected."""
 
238
 
239
  # Create molecule visualization
240
  mol_list = create_molecule_viewer(molecule_choice, default_scale)
241
+ if mol_list is None or not mol_list:
242
+ print(f"No molecule visualization created for {molecule_choice}", file=sys.stderr)
243
+ mol_list = []
244
 
245
  return [
246
  params_html, # Update params display
visualization.py CHANGED
@@ -8,7 +8,7 @@ import numpy as np
8
  import logging
9
  from rdkit import Chem
10
  from rdkit.Chem import AllChem
11
- from gradio_molgallery import MolGallery3D
12
  import json
13
 
14
  # Configure logging
@@ -154,14 +154,22 @@ def create_molecule_viewer(molecule_id: str, scale_factor: float) -> MolGallery3
154
 
155
  molecule_data = molecules[molecule_id]
156
 
157
- # Create molecule using SMILES
158
- mol = Chem.MolFromSmiles(molecule_data['smiles'])
159
- if mol is None:
160
- logger.error(f"Could not create molecule from SMILES: {molecule_data['smiles']}")
161
- return None
 
 
 
 
 
 
 
 
162
 
163
- mol = AllChem.AddHs(mol)
164
- AllChem.EmbedMolecule(mol, randomSeed=42) # Use fixed seed for consistency
165
 
166
  # Scale the molecule coordinates
167
  conf = mol.GetConformer()
@@ -174,13 +182,6 @@ def create_molecule_viewer(molecule_id: str, scale_factor: float) -> MolGallery3
174
  conf.SetAtomPosition(i, scaled_pos)
175
 
176
  # Create the MolGallery3D component
177
- gallery = MolGallery3D(
178
- columns=1,
179
- height=400,
180
- label=f"{molecule_data.get('name', molecule_id)} (Scale: {scale_factor:.2f})",
181
- automatic_rotation=True
182
- )
183
-
184
  logger.info("Created MolGallery3D component")
185
  return [mol] # Return the molecule as a list as expected by MolGallery3D
186
 
 
8
  import logging
9
  from rdkit import Chem
10
  from rdkit.Chem import AllChem
11
+ from gradio_molgallery3d import MolGallery3D
12
  import json
13
 
14
  # Configure logging
 
154
 
155
  molecule_data = molecules[molecule_id]
156
 
157
+ # Special handling for H2 and H2O
158
+ if molecule_id == "H2":
159
+ mol = Chem.MolFromSmiles("[H][H]")
160
+ elif molecule_id == "H2O":
161
+ mol = Chem.MolFromSmiles("O")
162
+ mol = Chem.AddHs(mol)
163
+ else:
164
+ # Create molecule using SMILES
165
+ mol = Chem.MolFromSmiles(molecule_data['smiles'])
166
+ if mol is None:
167
+ logger.error(f"Could not create molecule from SMILES: {molecule_data['smiles']}")
168
+ return None
169
+ mol = Chem.AddHs(mol)
170
 
171
+ # Generate 3D coordinates with distance matrix
172
+ AllChem.EmbedMolecule(mol, randomSeed=42)
173
 
174
  # Scale the molecule coordinates
175
  conf = mol.GetConformer()
 
182
  conf.SetAtomPosition(i, scaled_pos)
183
 
184
  # Create the MolGallery3D component
 
 
 
 
 
 
 
185
  logger.info("Created MolGallery3D component")
186
  return [mol] # Return the molecule as a list as expected by MolGallery3D
187