Refactor app.py to improve user experience by updating SMILES input display, enhancing error handling for invalid inputs, and refining the layout for displaying DCM layers. Adjusted slider label for clarity and ensured consistent rendering dimensions for 3D models.
Browse files
app.py
CHANGED
|
@@ -15,14 +15,14 @@ import streamlit.components.v1 as components
|
|
| 15 |
|
| 16 |
import streamlit as st
|
| 17 |
from streamlit_ketcher import st_ketcher
|
| 18 |
-
|
| 19 |
st.title("DCM-Net")
|
| 20 |
smiles = "OC(=O)[C@@H]1CCCN1"
|
| 21 |
# Create a molecule editor
|
| 22 |
smiles = st_ketcher(smiles)
|
| 23 |
|
| 24 |
# Output the SMILES string of the drawn molecule
|
| 25 |
-
st.write(f"SMILES: {smiles}")
|
| 26 |
|
| 27 |
|
| 28 |
# color charges by magnitude
|
|
@@ -83,58 +83,60 @@ def render_3d(atoms, dcm_charges=None, dcmcolors=None):
|
|
| 83 |
return view
|
| 84 |
|
| 85 |
# create a slider for n_dcm
|
| 86 |
-
n_dcm = st.slider("Number of
|
| 87 |
|
| 88 |
# create a button to run the DCM-Net
|
| 89 |
if st.button("Run DCM-Net"):
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
with col1:
|
| 101 |
-
showmol(render_3d(ase_atoms), height=300, width=1000)
|
| 102 |
-
with col2:
|
| 103 |
-
st.image(results["smiles_image"])
|
| 104 |
-
|
| 105 |
-
st.subheader("DCM-1")
|
| 106 |
-
dcmcolors1 = color_charges(results["mono_dc1"], 1, ase_atoms)
|
| 107 |
-
# two columns showing combined and just the dcmol
|
| 108 |
-
col1, col2 = st.columns(2)
|
| 109 |
-
with col1:
|
| 110 |
-
showmol(render_3d(ase_atoms, results["dcmol"]), height=300, width=1000)
|
| 111 |
-
with col2:
|
| 112 |
-
showmol(render_3d(None, results["dcmol"], dcmcolors1), height=300, width=1000)
|
| 113 |
-
|
| 114 |
-
if n_dcm >= 2:
|
| 115 |
-
st.subheader("DCM-2")
|
| 116 |
-
dcmcolors2 = color_charges(results["mono_dc2"], 2, ase_atoms)
|
| 117 |
col1, col2 = st.columns(2)
|
| 118 |
with col1:
|
| 119 |
-
showmol(render_3d(ase_atoms
|
| 120 |
with col2:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
if n_dcm >=
|
| 124 |
-
st.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
with col1:
|
| 128 |
-
showmol(render_3d(ase_atoms, results["dcmol3"]), height=300, width=1000)
|
| 129 |
-
with col2:
|
| 130 |
-
showmol(render_3d(None, results["dcmol3"], dcmcolors3), height=300, width=1000)
|
| 131 |
-
|
| 132 |
-
if n_dcm >= 4:
|
| 133 |
-
st.subheader("DCM-4")
|
| 134 |
-
dcmcolors4 = color_charges(results["mono_dc4"], 4, ase_atoms)
|
| 135 |
col1, col2 = st.columns(2)
|
| 136 |
with col1:
|
| 137 |
-
showmol(render_3d(ase_atoms, results["
|
| 138 |
with col2:
|
| 139 |
-
showmol(render_3d(None, results["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
|
|
|
| 15 |
|
| 16 |
import streamlit as st
|
| 17 |
from streamlit_ketcher import st_ketcher
|
| 18 |
+
WIDTH = 500
|
| 19 |
st.title("DCM-Net")
|
| 20 |
smiles = "OC(=O)[C@@H]1CCCN1"
|
| 21 |
# Create a molecule editor
|
| 22 |
smiles = st_ketcher(smiles)
|
| 23 |
|
| 24 |
# Output the SMILES string of the drawn molecule
|
| 25 |
+
st.write(f"SMILES input: {smiles}")
|
| 26 |
|
| 27 |
|
| 28 |
# color charges by magnitude
|
|
|
|
| 83 |
return view
|
| 84 |
|
| 85 |
# create a slider for n_dcm
|
| 86 |
+
n_dcm = st.slider("Number of Distributed Charges per Atom", min_value=1, max_value=4, value=4)
|
| 87 |
|
| 88 |
# create a button to run the DCM-Net
|
| 89 |
if st.button("Run DCM-Net"):
|
| 90 |
+
# bail if smiles is not valid
|
| 91 |
+
if smiles is None or smiles == "":
|
| 92 |
+
st.error("Please enter a valid SMILES string")
|
| 93 |
+
st.stop()
|
| 94 |
+
|
| 95 |
+
# start a spinner
|
| 96 |
+
with st.spinner("Running DCM-Net..."):
|
| 97 |
+
results = run_dcm(smiles, n_dcm)
|
| 98 |
+
print(results)
|
| 99 |
+
ase_atoms = results["atoms"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
col1, col2 = st.columns(2)
|
| 101 |
with col1:
|
| 102 |
+
showmol(render_3d(ase_atoms), height=300, width=WIDTH)
|
| 103 |
with col2:
|
| 104 |
+
st.image(results["smiles_image"])
|
| 105 |
+
st.subheader("Model Output")
|
| 106 |
+
if n_dcm >= 1:
|
| 107 |
+
st.markdown("### DCM-1")
|
| 108 |
+
dcmcolors1 = color_charges(results["mono_dc1"], 1, ase_atoms)
|
| 109 |
+
# two columns showing combined and just the dcmol
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
col1, col2 = st.columns(2)
|
| 111 |
with col1:
|
| 112 |
+
showmol(render_3d(ase_atoms, results["dcmol"]), height=300, width=WIDTH)
|
| 113 |
with col2:
|
| 114 |
+
showmol(render_3d(None, results["dcmol"], dcmcolors1), height=300, width=WIDTH)
|
| 115 |
+
|
| 116 |
+
if n_dcm >= 2:
|
| 117 |
+
st.markdown("### DCM-2")
|
| 118 |
+
dcmcolors2 = color_charges(results["mono_dc2"], 2, ase_atoms)
|
| 119 |
+
col1, col2 = st.columns(2)
|
| 120 |
+
with col1:
|
| 121 |
+
showmol(render_3d(ase_atoms, results["dcmol2"]), height=300, width=WIDTH)
|
| 122 |
+
with col2:
|
| 123 |
+
showmol(render_3d(None, results["dcmol2"], dcmcolors2), height=300, width=WIDTH)
|
| 124 |
+
|
| 125 |
+
if n_dcm >= 3:
|
| 126 |
+
st.markdown("### DCM-3")
|
| 127 |
+
dcmcolors3 = color_charges(results["mono_dc3"], 3, ase_atoms)
|
| 128 |
+
col1, col2 = st.columns(2)
|
| 129 |
+
with col1:
|
| 130 |
+
showmol(render_3d(ase_atoms, results["dcmol3"]), height=300, width=WIDTH)
|
| 131 |
+
with col2:
|
| 132 |
+
showmol(render_3d(None, results["dcmol3"], dcmcolors3), height=300, width=WIDTH)
|
| 133 |
+
|
| 134 |
+
if n_dcm >= 4:
|
| 135 |
+
st.markdown("### DCM-4")
|
| 136 |
+
dcmcolors4 = color_charges(results["mono_dc4"], 4, ase_atoms)
|
| 137 |
+
col1, col2 = st.columns(2)
|
| 138 |
+
with col1:
|
| 139 |
+
showmol(render_3d(ase_atoms, results["dcmol4"]), height=300, width=WIDTH)
|
| 140 |
+
with col2:
|
| 141 |
+
showmol(render_3d(None, results["dcmol4"], dcmcolors4), height=300, width=WIDTH)
|
| 142 |
|