smitathkr1 commited on
Commit
52c62bb
·
verified ·
1 Parent(s): c7d84e0

Update app.py with 3D molecule viewer

Browse files
Files changed (1) hide show
  1. app.py +56 -2
app.py CHANGED
@@ -1,7 +1,11 @@
1
  import gradio as gr
2
  from rdkit import Chem
3
- from rdkit.Chem import Descriptors, Draw
4
  import cirpy
 
 
 
 
5
 
6
 
7
  def _mol_from_smiles(smiles: str):
@@ -47,6 +51,46 @@ def name_to_smiles(name: str) -> str:
47
  raise gr.Error(f"Error converting name to SMILES: {str(e)}")
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  smiles_interface = gr.Interface(
51
  fn=smiles_to_canonical,
52
  inputs=gr.Textbox(label="SMILES"),
@@ -88,11 +132,21 @@ tpsa_interface = gr.Interface(
88
  description="Calculate the topological polar surface area (TPSA).",
89
  )
90
 
 
 
 
 
 
 
 
 
 
91
 
92
  demo = gr.TabbedInterface(
93
- [name_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
94
  [
95
  "Name to SMILES",
 
96
  "SMILES to Canonical",
97
  "Molecular Weight",
98
  "LogP",
 
1
  import gradio as gr
2
  from rdkit import Chem
3
+ from rdkit.Chem import Descriptors, Draw, AllChem
4
  import cirpy
5
+ import py3Dmol
6
+
7
+
8
+ # RDKit API with multiple endpoints
9
 
10
 
11
  def _mol_from_smiles(smiles: str):
 
51
  raise gr.Error(f"Error converting name to SMILES: {str(e)}")
52
 
53
 
54
+ def name_to_3d_molecule(name: str) -> str:
55
+ """Convert chemical name to 3D molecule visualization"""
56
+ try:
57
+ # Convert name to SMILES
58
+ smiles = cirpy.resolve(name, 'smiles')
59
+ if smiles is None:
60
+ raise gr.Error(f"Could not find SMILES for chemical name: {name}")
61
+
62
+ # Create molecule from SMILES
63
+ mol = Chem.MolFromSmiles(smiles)
64
+ if mol is None:
65
+ raise gr.Error(f"Could not create molecule from SMILES: {smiles}")
66
+
67
+ # Add hydrogens for better 3D structure
68
+ mol = Chem.AddHs(mol)
69
+
70
+ # Generate 3D coordinates
71
+ success = AllChem.EmbedMolecule(mol, AllChem.ETKDG())
72
+ if success == -1:
73
+ raise gr.Error(f"Could not generate 3D coordinates for: {name}")
74
+
75
+ # Optimize geometry
76
+ AllChem.MMFFOptimizeMolecule(mol)
77
+
78
+ # Convert to PDB format for py3Dmol
79
+ pdb_block = Chem.MolToPDBBlock(mol)
80
+
81
+ # Create 3D viewer
82
+ viewer = py3Dmol.view(width=400, height=400)
83
+ viewer.addModel(pdb_block, 'pdb')
84
+ viewer.setStyle({'stick': {}})
85
+ viewer.zoomTo()
86
+
87
+ # Return HTML representation
88
+ return viewer._repr_html_()
89
+
90
+ except Exception as e:
91
+ raise gr.Error(f"Error creating 3D molecule: {str(e)}")
92
+
93
+
94
  smiles_interface = gr.Interface(
95
  fn=smiles_to_canonical,
96
  inputs=gr.Textbox(label="SMILES"),
 
132
  description="Calculate the topological polar surface area (TPSA).",
133
  )
134
 
135
+ molecule_3d_interface = gr.Interface(
136
+ fn=name_to_3d_molecule,
137
+ inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
138
+ outputs=gr.HTML(label="3D Molecule Viewer"),
139
+ api_name="name_to_3d_molecule",
140
+ description="Convert a chemical name to an interactive 3D molecule visualization.",
141
+ examples=[["benzene"], ["aspirin"], ["caffeine"], ["ethanol"]],
142
+ )
143
+
144
 
145
  demo = gr.TabbedInterface(
146
+ [name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
147
  [
148
  "Name to SMILES",
149
+ "3D Molecule Viewer",
150
  "SMILES to Canonical",
151
  "Molecular Weight",
152
  "LogP",