import streamlit as st from PIL import Image import numpy as np import pubchempy as pcp from rdkit import Chem from rdkit.Chem import Draw import time import os import random @st.dialog("Select From Library") def lib_modal(): files = os.listdir("lib") files = random.choices(files, k=50) cols = st.columns(2) for i, file in enumerate(files): with cols[i % 2]: st.image("lib/{}".format(file), use_container_width=True) if st.button("Select", key=i): st.session_state.uploaded_file = file st.rerun() st.markdown("---") st.title("3D2SMILES") st.markdown(""" This app generates SMILES strings from images of molecules ball-and-stick models. [Version 1 Paper](https://chemrxiv.org/engage/chemrxiv/article-details/673a9d62f9980725cf89abe1) | [Version 2 Paper]() | [Synthetic Dataset](https://huggingface.co/datasets/weathon/3d2smiles_synthetic) | [Real Dataset](https://huggingface.co/datasets/weathon/3d2smiles_real) | [Author Github](https://github.com/weathon) | [Feedback](mailto:wguo6358@gmail.com) | [Deploy](https://huggingface.co/spaces/weathon/3d2smiles?docker=true) """) col1, col2 = st.columns(2) gen_strategy = col1.selectbox("Select a generative strategy", ("Beam Search", "Sampling", "Greedy Search")) temp = col2.slider("Temperature", 0.0, 2.0, 1.0) uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg", "webp", "heic"]) lib = st.columns(2) lib[0].markdown("Do not have an image? You can select a molecule from the library below.") from_library = lib[1].button("Select from Library") if from_library: lib_modal() col1, col2 = st.columns(2) if not "uploaded_file" in st.session_state: col1.checkbox("Contribute To Public Library", value=True, help="If checked, images will be included in the PUBLIC library, and the image will be reviewed by our team and used for model training. When checked, do not upload any sensitive or personal data.") else: col1.checkbox("Contribute To Public Library", value=False, disabled=True, help="When using the library, this option is disabled. If you want to contribute to the library, please upload an image.") button = col2.button("Submit") if button: if uploaded_file: start_time = time.time() image = Image.open(uploaded_file) options = ["CC(=O)OC1=CC=CC=C1C(=C)C(=O)O", "CC(=O)", "CC(=O)O", "CC(=O)C", "CC(=O)C1=CC=CC=C1"] grid = [st.columns(2) for _ in range(len(options) // 3 + 1)] cols = [col for row in grid for col in row] for i, (smiles, col) in enumerate(zip(options, cols)): cid = pcp.get_compounds(smiles, 'smiles') name = cid[0].synonyms[0] col.markdown(name) m = Chem.MolFromSmiles(smiles) img = Draw.MolToImage(m) col.image(img, use_container_width=False) pubchem_url = "https://pubchem.ncbi.nlm.nih.gov/compound/{}".format(cid[0].cid) col.markdown("[PubChem]({})".format(pubchem_url)) st.markdown("---") st.markdown("Taken {} seconds".format(round(time.time() - start_time, 2)))