Nanny7 commited on
Commit
26cfee5
·
1 Parent(s): 360448f

Add PubChem fallback for SMILES name lookup

Browse files
Files changed (2) hide show
  1. app.py +23 -9
  2. requirements.txt +2 -1
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
  from urllib.error import HTTPError, URLError
6
 
7
 
@@ -54,19 +55,32 @@ def name_to_smiles(name: str) -> str:
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:
 
2
  from rdkit import Chem
3
  from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
5
+ import pubchempy as pcp
6
  from urllib.error import HTTPError, URLError
7
 
8
 
 
55
  def smiles_to_name(smiles: str) -> str:
56
  """Convert SMILES string to chemical name using Chemical Identifier Resolver (CIR)."""
57
  mol = _mol_from_smiles(smiles)
58
+ canonical_smiles = Chem.MolToSmiles(mol)
59
 
60
  try:
61
  name = cirpy.resolve(smiles, "name")
62
+ if name:
63
+ return name
64
+ except (HTTPError, URLError):
65
+ # Ignore network failures and fall back to other resolvers.
66
+ pass
67
+ except Exception:
68
+ # Ignore unexpected CIR errors and fall back to other resolvers.
69
+ pass
70
 
71
+ try:
72
+ # Try PubChem as a secondary resolver in case CIR fails.
73
+ compounds = pcp.get_compounds(canonical_smiles, namespace="smiles")
74
+ for compound in compounds:
75
+ if compound.iupac_name:
76
+ return compound.iupac_name
77
+ if compound.synonyms:
78
+ return compound.synonyms[0]
79
+ except Exception:
80
+ # Ignore PubChem issues and fall back to canonical SMILES output.
81
+ pass
82
+
83
+ return f"No name available. Canonical SMILES: {canonical_smiles}"
84
 
85
 
86
  def name_to_3d_molecule(name: str) -> str:
requirements.txt CHANGED
@@ -2,4 +2,5 @@
2
  rdkit
3
  gradio==4.44.1
4
  huggingface_hub==0.19.4
5
- cirpy
 
 
2
  rdkit
3
  gradio==4.44.1
4
  huggingface_hub==0.19.4
5
+ cirpy
6
+ pubchempy