Nanny7 commited on
Commit
291f90f
·
1 Parent(s): 526a32d

Allow orbital tab to resolve names

Browse files
Files changed (1) hide show
  1. app.py +33 -3
app.py CHANGED
@@ -86,9 +86,28 @@ def smiles_to_name(smiles: str) -> str:
86
  return f"No name available. Canonical SMILES: {canonical_smiles}"
87
 
88
 
89
- def smiles_to_molecular_orbitals(smiles: str) -> str:
90
  """Generate HOMO/LUMO isosurfaces using Psikit, when available."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  mol = _mol_from_smiles(smiles)
 
92
  if mol.GetNumAtoms() > 30:
93
  raise gr.Error("Please provide a molecule with 30 atoms or fewer for orbital visualization.")
94
 
@@ -127,6 +146,11 @@ def smiles_to_molecular_orbitals(smiles: str) -> str:
127
  mol_block = Chem.MolToMolBlock(pk.mol)
128
 
129
  html_sections: list[str] = []
 
 
 
 
 
130
  labels = ["HOMO", "LUMO"]
131
  for cube_file, label in zip(cube_candidates[:2], labels):
132
  cube_data = cube_file.read_text()
@@ -151,6 +175,9 @@ def smiles_to_molecular_orbitals(smiles: str) -> str:
151
  if not html_sections:
152
  return "<p>Could not prepare HOMO/LUMO visualizations.</p>"
153
 
 
 
 
154
  return "".join(html_sections)
155
  except Exception as exc: # pragma: no cover - runtime heavy
156
  return f"<p>Unable to compute molecular orbitals: {exc}</p>"
@@ -230,10 +257,13 @@ smiles_to_name_interface = gr.Interface(
230
 
231
  orbital_interface = gr.Interface(
232
  fn=smiles_to_molecular_orbitals,
233
- inputs=gr.Textbox(label="SMILES", placeholder="e.g., CC(=O)O"),
 
 
 
234
  outputs=gr.HTML(label="Molecular Orbitals"),
235
  api_name="smiles_to_mo",
236
- description="Generate HOMO/LUMO isosurfaces using Psikit (CPU-intensive).",
237
  )
238
 
239
  name_interface = gr.Interface(
 
86
  return f"No name available. Canonical SMILES: {canonical_smiles}"
87
 
88
 
89
+ def smiles_to_molecular_orbitals(smiles_input: str, name_input: str) -> str:
90
  """Generate HOMO/LUMO isosurfaces using Psikit, when available."""
91
+ smiles = smiles_input.strip()
92
+ name = name_input.strip()
93
+
94
+ if not smiles and not name:
95
+ raise gr.Error("Enter a SMILES string or a chemical name to compute orbitals.")
96
+
97
+ if name:
98
+ try:
99
+ resolved = cirpy.resolve(name, "smiles")
100
+ except Exception as exc:
101
+ return f"<p>Could not resolve '{name}' to SMILES: {exc}</p>"
102
+ if not resolved:
103
+ return f"<p>No SMILES found for '{name}'. Try a different name or supply a SMILES directly.</p>"
104
+ smiles = resolved
105
+
106
+ if not smiles:
107
+ raise gr.Error("Unable to determine SMILES for orbital calculation.")
108
+
109
  mol = _mol_from_smiles(smiles)
110
+ canonical_smiles = Chem.MolToSmiles(mol)
111
  if mol.GetNumAtoms() > 30:
112
  raise gr.Error("Please provide a molecule with 30 atoms or fewer for orbital visualization.")
113
 
 
146
  mol_block = Chem.MolToMolBlock(pk.mol)
147
 
148
  html_sections: list[str] = []
149
+ if name_input.strip():
150
+ html_sections.append(
151
+ f"<p><strong>Resolved '{name_input.strip()}' to SMILES:</strong> {canonical_smiles}</p>"
152
+ )
153
+
154
  labels = ["HOMO", "LUMO"]
155
  for cube_file, label in zip(cube_candidates[:2], labels):
156
  cube_data = cube_file.read_text()
 
175
  if not html_sections:
176
  return "<p>Could not prepare HOMO/LUMO visualizations.</p>"
177
 
178
+ if not html_sections:
179
+ return "<p>Could not prepare HOMO/LUMO visualizations.</p>"
180
+
181
  return "".join(html_sections)
182
  except Exception as exc: # pragma: no cover - runtime heavy
183
  return f"<p>Unable to compute molecular orbitals: {exc}</p>"
 
257
 
258
  orbital_interface = gr.Interface(
259
  fn=smiles_to_molecular_orbitals,
260
+ inputs=[
261
+ gr.Textbox(label="SMILES", placeholder="e.g., CC(=O)O"),
262
+ gr.Textbox(label="Chemical Name", placeholder="Optional, e.g., benzene"),
263
+ ],
264
  outputs=gr.HTML(label="Molecular Orbitals"),
265
  api_name="smiles_to_mo",
266
+ description="Generate HOMO/LUMO isosurfaces using Psikit (CPU-intensive). Provide SMILES or a name.",
267
  )
268
 
269
  name_interface = gr.Interface(