Spaces:
Build error
Build error
| import gradio as gr | |
| from gradio_molecule2d import molecule2d | |
| from rdkit import Chem | |
| from rdkit.Chem.inchi import MolToInchi, InchiToInchiKey | |
| def transform_molecule(smiles_input): | |
| """Convert drawn molecule SMILES to canonical SMILES and InChIKey.""" | |
| if not smiles_input: | |
| return "", "" | |
| mol = Chem.MolFromSmiles(smiles_input) | |
| if mol is None: | |
| return "Invalid molecule", "Invalid molecule" | |
| canonical_smiles = Chem.MolToSmiles(mol) | |
| inchi = MolToInchi(mol) | |
| inchi_key = InchiToInchiKey(inchi) if inchi else "Could not generate InChIKey" | |
| return canonical_smiles, inchi_key | |
| with gr.Blocks(title="Chemical Structure Transformer") as demo: | |
| gr.Markdown( | |
| """ | |
| # 🧪 Chemical Structure Transformer | |
| Draw a chemical structure using the editor below, then click **Transform** to get the canonical SMILES and InChIKey. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| mol_input = molecule2d(label="Draw Chemical Structure") | |
| transform_btn = gr.Button("🔄 Transform", variant="primary", size="lg") | |
| with gr.Column(): | |
| smiles_output = gr.Textbox( | |
| label="Canonical SMILES", | |
| placeholder="SMILES string will appear here...", | |
| interactive=False, | |
| ) | |
| inchi_key_output = gr.Textbox( | |
| label="InChIKey", | |
| placeholder="InChIKey will appear here...", | |
| interactive=False, | |
| ) | |
| transform_btn.click( | |
| fn=transform_molecule, | |
| inputs=[mol_input], | |
| outputs=[smiles_output, inchi_key_output], | |
| ) | |
| gr.Examples( | |
| examples=[ | |
| ["c1ccccc1"], # Benzene | |
| ["CC(=O)Oc1ccccc1C(=O)O"], # Aspirin | |
| ["CC12CCC3C(C1CCC2O)CCC4=CC(=O)CCC34C"], # Testosterone | |
| ], | |
| inputs=[mol_input], | |
| label="Example Molecules (SMILES)", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |