Nanny7 commited on
Commit
360448f
·
1 Parent(s): 4954e62

Handle resolver outages in SMILES to Name tab

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from rdkit import Chem
3
  from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
 
5
 
6
 
7
  # RDKit API with multiple endpoints
@@ -51,19 +52,21 @@ def name_to_smiles(name: str) -> str:
51
 
52
 
53
  def smiles_to_name(smiles: str) -> str:
54
- """Convert SMILES string to chemical name using Chemical Identifier Resolver (CIR)"""
 
 
55
  try:
56
- # First validate the SMILES
57
- mol = _mol_from_smiles(smiles)
58
-
59
- # Try to resolve SMILES to a chemical name
60
- name = cirpy.resolve(smiles, 'name')
61
- if name is None:
62
- # If no name found, return the canonical SMILES
63
- return Chem.MolToSmiles(mol)
64
- return name
65
  except Exception as e:
66
- raise gr.Error(f"Error converting SMILES to name: {str(e)}")
 
 
 
 
 
67
 
68
 
69
  def name_to_3d_molecule(name: str) -> str:
 
2
  from rdkit import Chem
3
  from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
5
+ from urllib.error import HTTPError, URLError
6
 
7
 
8
  # RDKit API with multiple endpoints
 
52
 
53
 
54
  def smiles_to_name(smiles: str) -> str:
55
+ """Convert SMILES string to chemical name using Chemical Identifier Resolver (CIR)."""
56
+ mol = _mol_from_smiles(smiles)
57
+
58
  try:
59
+ name = cirpy.resolve(smiles, "name")
60
+ except (HTTPError, URLError) as network_error:
61
+ # Gracefully degrade when the resolver service is unavailable.
62
+ return f"Resolver unavailable ({network_error}). Canonical SMILES: {Chem.MolToSmiles(mol)}"
 
 
 
 
 
63
  except Exception as e:
64
+ return f"Unable to resolve to name ({e}). Canonical SMILES: {Chem.MolToSmiles(mol)}"
65
+
66
+ if not name:
67
+ return f"No name found. Canonical SMILES: {Chem.MolToSmiles(mol)}"
68
+
69
+ return name
70
 
71
 
72
  def name_to_3d_molecule(name: str) -> str: