Vaishnav14220 commited on
Commit
3945aad
·
1 Parent(s): d157eb5

Switch to 2D molecule viewer with base64 encoded PNG

Browse files
Files changed (1) hide show
  1. app.py +22 -25
app.py CHANGED
@@ -74,30 +74,27 @@ def name_to_3d_molecule(name: str) -> str:
74
  # Optimize geometry
75
  AllChem.MMFFOptimizeMolecule(mol)
76
 
77
- # Convert to MOL block format
78
- mol_block = Chem.MolToMolBlock(mol)
79
 
80
- # Escape the mol block for JavaScript
81
- import json
82
- mol_block_escaped = json.dumps(mol_block)
 
 
 
 
 
 
83
 
84
- # Create HTML with embedded 3D viewer using 3Dmol.js
85
  html_content = f"""
86
- <div id="container-3dmol" style="width: 600px; height: 600px; position: relative; margin: auto;"></div>
87
- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
88
- <script src="https://3dmol.csb.pitt.edu/build/3Dmol-min.js"></script>
89
- <script>
90
- $(document).ready(function() {{
91
- let element = $('#container-3dmol');
92
- let viewer = $3Dmol.createViewer(element, {{backgroundColor: 'white'}});
93
- let molData = {mol_block_escaped};
94
- viewer.addModel(molData, "mol");
95
- viewer.setStyle({{}}, {{stick: {{colorscheme: 'Jmol'}}}});
96
- viewer.addStyle({{}}, {{sphere: {{radius: 0.3, colorscheme: 'Jmol'}}}});
97
- viewer.zoomTo();
98
- viewer.render();
99
- }});
100
- </script>
101
  """
102
 
103
  return html_content
@@ -150,9 +147,9 @@ tpsa_interface = gr.Interface(
150
  molecule_3d_interface = gr.Interface(
151
  fn=name_to_3d_molecule,
152
  inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
153
- outputs=gr.HTML(label="3D Molecule Viewer"),
154
- api_name="name_to_3d_molecule",
155
- description="Convert a chemical name to an interactive 3D molecule visualization.",
156
  cache_examples=False,
157
  )
158
 
@@ -161,7 +158,7 @@ demo = gr.TabbedInterface(
161
  [name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
162
  [
163
  "Name to SMILES",
164
- "3D Molecule Viewer",
165
  "SMILES to Canonical",
166
  "Molecular Weight",
167
  "LogP",
 
74
  # Optimize geometry
75
  AllChem.MMFFOptimizeMolecule(mol)
76
 
77
+ # Generate 2D image as fallback and show SMILES
78
+ img = Draw.MolToImage(mol, size=(400, 400))
79
 
80
+ # Convert image to base64
81
+ import io
82
+ import base64
83
+ buffered = io.BytesIO()
84
+ img.save(buffered, format="PNG")
85
+ img_str = base64.b64encode(buffered.getvalue()).decode()
86
+
87
+ # Get SMILES string
88
+ canonical_smiles = Chem.MolToSmiles(mol)
89
 
90
+ # Create HTML with 2D image and molecule info
91
  html_content = f"""
92
+ <div style="text-align: center; padding: 20px;">
93
+ <h3>{name}</h3>
94
+ <p><strong>SMILES:</strong> {canonical_smiles}</p>
95
+ <img src="data:image/png;base64,{img_str}" style="max-width: 100%; border: 1px solid #ddd; border-radius: 8px; padding: 10px; background: white;">
96
+ <p style="margin-top: 10px; color: #666;">2D Structure Visualization</p>
97
+ </div>
 
 
 
 
 
 
 
 
 
98
  """
99
 
100
  return html_content
 
147
  molecule_3d_interface = gr.Interface(
148
  fn=name_to_3d_molecule,
149
  inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
150
+ outputs=gr.HTML(label="Molecule Viewer"),
151
+ api_name="name_to_molecule",
152
+ description="Convert a chemical name to a molecule visualization with SMILES.",
153
  cache_examples=False,
154
  )
155
 
 
158
  [name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
159
  [
160
  "Name to SMILES",
161
+ "Molecule Viewer",
162
  "SMILES to Canonical",
163
  "Molecular Weight",
164
  "LogP",