Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,29 +41,48 @@ if st.button('Generate Molecules'):
|
|
41 |
st.info("Generating molecules... Please wait.")
|
42 |
# Generate molecules
|
43 |
core_smiles = "C1C(=O)N(C)C(=O)C1" # Beta-lactam core structure
|
|
|
44 |
output_ids = model.generate(
|
45 |
-
|
46 |
max_length=128,
|
47 |
temperature=creativity,
|
48 |
do_sample=True,
|
49 |
top_k=50,
|
50 |
-
num_return_sequences=num_molecules
|
|
|
51 |
)
|
52 |
generated_smiles = [tokenizer.decode(ids, skip_special_tokens=True) for ids in output_ids]
|
53 |
-
molecule_names = [f"Mol{str(i).zfill(2)}" for i in range(1,
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# ADMET Predictions
|
57 |
-
preds = admet_model.predict(smiles=
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
# Display Molecules
|
62 |
st.subheader('Generated Molecules')
|
63 |
-
cols_per_row = min(5,
|
64 |
cols = st.columns(cols_per_row)
|
65 |
-
for idx, mol_name in enumerate(
|
66 |
-
smiles =
|
67 |
img = generate_molecule_image(smiles, use_safe_visualization=(string_format == 'SAFE'))
|
68 |
with cols[idx % cols_per_row]:
|
69 |
if isinstance(img, Image.Image):
|
@@ -77,10 +96,11 @@ if st.button('Generate Molecules'):
|
|
77 |
st_copy_button(string_to_display, key=f'copy_{mol_name}')
|
78 |
# Display ADMET properties
|
79 |
st.write("**ADMET Properties:**")
|
80 |
-
st.write(
|
81 |
else:
|
82 |
st.write("Click the 'Generate Molecules' button to generate beta-lactam molecules.")
|
83 |
|
|
|
84 |
# Function Definitions
|
85 |
def generate_molecule_image(input_string, use_safe_visualization=True):
|
86 |
try:
|
|
|
41 |
st.info("Generating molecules... Please wait.")
|
42 |
# Generate molecules
|
43 |
core_smiles = "C1C(=O)N(C)C(=O)C1" # Beta-lactam core structure
|
44 |
+
input_ids = tokenizer(core_smiles, return_tensors='pt').input_ids
|
45 |
output_ids = model.generate(
|
46 |
+
input_ids=input_ids,
|
47 |
max_length=128,
|
48 |
temperature=creativity,
|
49 |
do_sample=True,
|
50 |
top_k=50,
|
51 |
+
num_return_sequences=num_molecules,
|
52 |
+
num_beams=max(num_molecules, 5) # Ensure num_beams >= num_return_sequences
|
53 |
)
|
54 |
generated_smiles = [tokenizer.decode(ids, skip_special_tokens=True) for ids in output_ids]
|
55 |
+
molecule_names = [f"Mol{str(i).zfill(2)}" for i in range(1, len(generated_smiles) + 1)]
|
56 |
+
|
57 |
+
# Create DataFrame for generated molecules
|
58 |
+
df_molecules = pd.DataFrame({
|
59 |
+
'Molecule Name': molecule_names,
|
60 |
+
'SMILES': generated_smiles
|
61 |
+
})
|
62 |
+
|
63 |
+
# Display generated SMILES for debugging
|
64 |
+
st.write("Generated SMILES:")
|
65 |
+
st.write(df_molecules)
|
66 |
|
67 |
# ADMET Predictions
|
68 |
+
preds = admet_model.predict(smiles=df_molecules['SMILES'].tolist())
|
69 |
+
|
70 |
+
# Ensure 'SMILES' is a column in preds
|
71 |
+
if 'SMILES' not in preds.columns:
|
72 |
+
preds['SMILES'] = df_molecules['SMILES']
|
73 |
+
|
74 |
+
# Merge predictions with generated molecules
|
75 |
+
df_results = pd.merge(df_molecules, preds, on='SMILES', how='inner')
|
76 |
+
|
77 |
+
# Set 'Molecule Name' as index
|
78 |
+
df_results.set_index('Molecule Name', inplace=True)
|
79 |
|
80 |
# Display Molecules
|
81 |
st.subheader('Generated Molecules')
|
82 |
+
cols_per_row = min(5, len(df_results))
|
83 |
cols = st.columns(cols_per_row)
|
84 |
+
for idx, (mol_name, row) in enumerate(df_results.iterrows()):
|
85 |
+
smiles = row['SMILES']
|
86 |
img = generate_molecule_image(smiles, use_safe_visualization=(string_format == 'SAFE'))
|
87 |
with cols[idx % cols_per_row]:
|
88 |
if isinstance(img, Image.Image):
|
|
|
96 |
st_copy_button(string_to_display, key=f'copy_{mol_name}')
|
97 |
# Display ADMET properties
|
98 |
st.write("**ADMET Properties:**")
|
99 |
+
st.write(row.drop(['SMILES']))
|
100 |
else:
|
101 |
st.write("Click the 'Generate Molecules' button to generate beta-lactam molecules.")
|
102 |
|
103 |
+
|
104 |
# Function Definitions
|
105 |
def generate_molecule_image(input_string, use_safe_visualization=True):
|
106 |
try:
|