Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_molecule2d import molecule2d
|
| 3 |
+
from rdkit import Chem
|
| 4 |
+
from rdkit.Chem.inchi import MolToInchi, InchiToInchiKey
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def transform_molecule(smiles_input):
|
| 8 |
+
"""Convert drawn molecule SMILES to canonical SMILES and InChIKey."""
|
| 9 |
+
if not smiles_input:
|
| 10 |
+
return "", ""
|
| 11 |
+
|
| 12 |
+
mol = Chem.MolFromSmiles(smiles_input)
|
| 13 |
+
if mol is None:
|
| 14 |
+
return "Invalid molecule", "Invalid molecule"
|
| 15 |
+
|
| 16 |
+
canonical_smiles = Chem.MolToSmiles(mol)
|
| 17 |
+
|
| 18 |
+
inchi = MolToInchi(mol)
|
| 19 |
+
inchi_key = InchiToInchiKey(inchi) if inchi else "Could not generate InChIKey"
|
| 20 |
+
|
| 21 |
+
return canonical_smiles, inchi_key
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
with gr.Blocks(title="Chemical Structure Transformer") as demo:
|
| 25 |
+
gr.Markdown(
|
| 26 |
+
"""
|
| 27 |
+
# 🧪 Chemical Structure Transformer
|
| 28 |
+
Draw a chemical structure using the editor below, then click **Transform** to get the canonical SMILES and InChIKey.
|
| 29 |
+
"""
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
with gr.Column():
|
| 34 |
+
mol_input = molecule2d(label="Draw Chemical Structure")
|
| 35 |
+
transform_btn = gr.Button("🔄 Transform", variant="primary", size="lg")
|
| 36 |
+
|
| 37 |
+
with gr.Column():
|
| 38 |
+
smiles_output = gr.Textbox(
|
| 39 |
+
label="Canonical SMILES",
|
| 40 |
+
placeholder="SMILES string will appear here...",
|
| 41 |
+
interactive=False,
|
| 42 |
+
)
|
| 43 |
+
inchi_key_output = gr.Textbox(
|
| 44 |
+
label="InChIKey",
|
| 45 |
+
placeholder="InChIKey will appear here...",
|
| 46 |
+
interactive=False,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
transform_btn.click(
|
| 50 |
+
fn=transform_molecule,
|
| 51 |
+
inputs=[mol_input],
|
| 52 |
+
outputs=[smiles_output, inchi_key_output],
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
gr.Examples(
|
| 56 |
+
examples=[
|
| 57 |
+
["c1ccccc1"], # Benzene
|
| 58 |
+
["CC(=O)Oc1ccccc1C(=O)O"], # Aspirin
|
| 59 |
+
["CC12CCC3C(C1CCC2O)CCC4=CC(=O)CCC34C"], # Testosterone
|
| 60 |
+
],
|
| 61 |
+
inputs=[mol_input],
|
| 62 |
+
label="Example Molecules (SMILES)",
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|