Spaces:
Runtime error
Runtime error
Vaishnav14220
commited on
Commit
·
a6b2934
1
Parent(s):
904862a
Fix File component schema bug - use Textbox for SDF content instead
Browse files
app.py
CHANGED
|
@@ -51,7 +51,7 @@ def name_to_smiles(name: str) -> str:
|
|
| 51 |
|
| 52 |
|
| 53 |
def name_to_3d_molecule(name: str) -> tuple:
|
| 54 |
-
"""Convert chemical name to 3D molecule SDF
|
| 55 |
try:
|
| 56 |
# Convert name to SMILES
|
| 57 |
smiles = cirpy.resolve(name, 'smiles')
|
|
@@ -74,13 +74,8 @@ def name_to_3d_molecule(name: str) -> tuple:
|
|
| 74 |
# Optimize geometry
|
| 75 |
AllChem.MMFFOptimizeMolecule(mol)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
import os
|
| 80 |
-
sdf_file = tempfile.NamedTemporaryFile(mode='w', suffix='.sdf', delete=False)
|
| 81 |
-
writer = Chem.SDWriter(sdf_file.name)
|
| 82 |
-
writer.write(mol)
|
| 83 |
-
writer.close()
|
| 84 |
|
| 85 |
# Generate 2D image for preview
|
| 86 |
mol_2d = Chem.MolFromSmiles(smiles) # Get 2D version
|
|
@@ -96,7 +91,7 @@ def name_to_3d_molecule(name: str) -> tuple:
|
|
| 96 |
# Get SMILES string
|
| 97 |
canonical_smiles = Chem.MolToSmiles(mol_2d)
|
| 98 |
|
| 99 |
-
# Create HTML with 2D image and
|
| 100 |
html_content = f"""
|
| 101 |
<div style="text-align: center; padding: 20px;">
|
| 102 |
<h3>{name}</h3>
|
|
@@ -105,16 +100,16 @@ def name_to_3d_molecule(name: str) -> tuple:
|
|
| 105 |
<p style="margin-top: 10px; color: #666;">2D Structure Preview</p>
|
| 106 |
<div style="margin-top: 20px; padding: 15px; background-color: #d4edda; border: 2px solid #28a745; border-radius: 8px;">
|
| 107 |
<p style="color: #155724; font-size: 16px; font-weight: bold; margin: 0;">
|
| 108 |
-
✓ 3D SDF
|
| 109 |
</p>
|
| 110 |
<p style="color: #155724; margin: 5px 0 0 0;">
|
| 111 |
-
|
| 112 |
</p>
|
| 113 |
</div>
|
| 114 |
</div>
|
| 115 |
"""
|
| 116 |
|
| 117 |
-
return html_content,
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
raise gr.Error(f"Error creating molecule: {str(e)}")
|
|
@@ -166,10 +161,10 @@ molecule_3d_interface = gr.Interface(
|
|
| 166 |
inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine, glucose"),
|
| 167 |
outputs=[
|
| 168 |
gr.HTML(label="2D Preview"),
|
| 169 |
-
gr.
|
| 170 |
],
|
| 171 |
api_name="name_to_molecule",
|
| 172 |
-
description="Get 2D structure preview and
|
| 173 |
cache_examples=False,
|
| 174 |
)
|
| 175 |
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
def name_to_3d_molecule(name: str) -> tuple:
|
| 54 |
+
"""Convert chemical name to 3D molecule SDF and 2D visualization"""
|
| 55 |
try:
|
| 56 |
# Convert name to SMILES
|
| 57 |
smiles = cirpy.resolve(name, 'smiles')
|
|
|
|
| 74 |
# Optimize geometry
|
| 75 |
AllChem.MMFFOptimizeMolecule(mol)
|
| 76 |
|
| 77 |
+
# Generate SDF content
|
| 78 |
+
sdf_content = Chem.MolToMolBlock(mol)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# Generate 2D image for preview
|
| 81 |
mol_2d = Chem.MolFromSmiles(smiles) # Get 2D version
|
|
|
|
| 91 |
# Get SMILES string
|
| 92 |
canonical_smiles = Chem.MolToSmiles(mol_2d)
|
| 93 |
|
| 94 |
+
# Create HTML with 2D image and SDF content
|
| 95 |
html_content = f"""
|
| 96 |
<div style="text-align: center; padding: 20px;">
|
| 97 |
<h3>{name}</h3>
|
|
|
|
| 100 |
<p style="margin-top: 10px; color: #666;">2D Structure Preview</p>
|
| 101 |
<div style="margin-top: 20px; padding: 15px; background-color: #d4edda; border: 2px solid #28a745; border-radius: 8px;">
|
| 102 |
<p style="color: #155724; font-size: 16px; font-weight: bold; margin: 0;">
|
| 103 |
+
✓ 3D SDF Content Ready!
|
| 104 |
</p>
|
| 105 |
<p style="color: #155724; margin: 5px 0 0 0;">
|
| 106 |
+
Copy the SDF content below and save as .sdf file
|
| 107 |
</p>
|
| 108 |
</div>
|
| 109 |
</div>
|
| 110 |
"""
|
| 111 |
|
| 112 |
+
return html_content, sdf_content
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
raise gr.Error(f"Error creating molecule: {str(e)}")
|
|
|
|
| 161 |
inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine, glucose"),
|
| 162 |
outputs=[
|
| 163 |
gr.HTML(label="2D Preview"),
|
| 164 |
+
gr.Textbox(label="3D SDF Content (copy and save as .sdf file)", lines=20, max_lines=30)
|
| 165 |
],
|
| 166 |
api_name="name_to_molecule",
|
| 167 |
+
description="Get 2D structure preview and 3D SDF content. Copy the SDF text and save as .sdf file to open in PyMOL, Avogadro, ChimeraX, or other molecular viewers.",
|
| 168 |
cache_examples=False,
|
| 169 |
)
|
| 170 |
|