osbm commited on
Commit
22e50e6
·
1 Parent(s): 7e143cc

Upload gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +204 -0
gradio_app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from trainer import Trainer
3
+ import PIL
4
+ from PIL import Image
5
+ import pandas as pd
6
+ import random
7
+ from rdkit import Chem
8
+ from rdkit.Chem import Draw
9
+ from rdkit.Chem.Draw import IPythonConsole
10
+ import shutil
11
+
12
+ class DrugGENConfig:
13
+ submodel='CrossLoss'
14
+ act='relu'
15
+ z_dim=16
16
+ max_atom=45
17
+ lambda_gp=1
18
+ dim=128
19
+ depth=1
20
+ heads=8
21
+ dec_depth=1
22
+ dec_heads=8
23
+ dec_dim=128
24
+ mlp_ratio=3
25
+ warm_up_steps=0
26
+ dis_select='mlp'
27
+ init_type='normal'
28
+ batch_size=128
29
+ epoch=50
30
+ g_lr=0.00001
31
+ d_lr=0.00001
32
+ g2_lr=0.00001
33
+ d2_lr=0.00001
34
+ dropout=0.
35
+ dec_dropout=0.
36
+ n_critic=1
37
+ beta1=0.9
38
+ beta2=0.999
39
+ resume_iters=None
40
+ clipping_value=2
41
+ features=False
42
+ test_iters=10_000
43
+ num_test_epoch=30_000
44
+ inference_sample_num=1000
45
+ num_workers=1
46
+ mode="inference"
47
+ inference_iterations=100
48
+ inf_batch_size=1
49
+ protein_data_dir='data/akt'
50
+ drug_index='data/drug_smiles.index'
51
+ drug_data_dir='data/akt'
52
+ mol_data_dir='data'
53
+ log_dir='experiments/logs'
54
+ model_save_dir='experiments/models'
55
+ # inference_model=""
56
+ sample_dir='experiments/samples'
57
+ result_dir="experiments/tboard_output"
58
+ dataset_file="chembl45_train.pt"
59
+ drug_dataset_file="akt_train.pt"
60
+ raw_file='data/chembl_train.smi'
61
+ drug_raw_file="data/akt_train.smi"
62
+ inf_dataset_file="chembl45_test.pt"
63
+ inf_drug_dataset_file='akt_test.pt'
64
+ inf_raw_file='data/chembl_test.smi'
65
+ inf_drug_raw_file="data/akt_test.smi"
66
+ log_sample_step=1000
67
+ set_seed=True
68
+ seed=1
69
+ resume=False
70
+ resume_epoch=None
71
+ resume_iter=None
72
+ resume_directory=None
73
+
74
+ class ProtConfig(DrugGENConfig):
75
+ submodel="Prot"
76
+ inference_model="experiments/models/Prot"
77
+
78
+ class CrossLossConfig(DrugGENConfig):
79
+ submodel="CrossLoss"
80
+ inference_model="experiments/models/CrossLoss"
81
+
82
+ class NoTargetConfig(DrugGENConfig):
83
+ submodel="NoTarget"
84
+ inference_model="experiments/models/NoTarget"
85
+
86
+
87
+ model_configs = {
88
+ "Prot": ProtConfig(),
89
+ "CrossLoss": CrossLossConfig(),
90
+ "NoTarget": NoTargetConfig(),
91
+ }
92
+
93
+
94
+
95
+ def function(model_name: str, mol_num: int, seed: int) -> tuple[PIL.Image, pd.DataFrame, str]:
96
+ '''
97
+ Returns:
98
+ image, score_df, file path
99
+ '''
100
+ model_name = model_name.replace("DrugGEN-", "")
101
+
102
+ config = model_configs[model_name]
103
+ config.inference_sample_num = mol_num
104
+ config.seed = seed
105
+
106
+ trainer = Trainer(config)
107
+ scores = trainer.inference() # create scores_df out of this
108
+
109
+ score_df = pd.DataFrame(scores, index=[0])
110
+
111
+ output_file_path = f'experiments/inference/{model_name}/inference_drugs.txt'
112
+ import os
113
+ new_path = f'DrugGEN-{model_name}_denovo_mols.smi'
114
+ os.rename(output_file_path, new_path)
115
+
116
+ with open(new_path) as f:
117
+ inference_drugs = f.read()
118
+
119
+ generated_molecule_list = inference_drugs.split("\n")
120
+
121
+ rng = random.Random(seed)
122
+
123
+ selected_molecules = rng.choices(generated_molecule_list,k=12)
124
+ selected_molecules = [Chem.MolFromSmiles(mol) for mol in selected_molecules]
125
+
126
+ drawOptions = Draw.rdMolDraw2D.MolDrawOptions()
127
+ drawOptions.prepareMolsBeforeDrawing = False
128
+ drawOptions.bondLineWidth = 0.5
129
+
130
+ molecule_image = Draw.MolsToGridImage(
131
+ selected_molecules,
132
+ molsPerRow=3,
133
+ subImgSize=(400, 400),
134
+ maxMols=len(selected_molecules),
135
+ # legends=None,
136
+ returnPNG=False,
137
+ drawOptions=drawOptions,
138
+ highlightAtomLists=None,
139
+ highlightBondLists=None,
140
+ )
141
+
142
+
143
+ return molecule_image, score_df, new_path
144
+
145
+
146
+
147
+ with gr.Blocks() as demo:
148
+ with gr.Row():
149
+ with gr.Column(scale=1):
150
+ gr.Markdown("# DrugGEN: Target Centric De Novo Design of Drug Candidate Molecules with Graph Generative Deep Adversarial Networks")
151
+ with gr.Row():
152
+ gr.Markdown("[![arXiv](https://img.shields.io/badge/arXiv-2302.07868-b31b1b.svg)](https://arxiv.org/abs/2302.07868)")
153
+ gr.Markdown("[![github-repository](https://img.shields.io/badge/GitHub-black?logo=github)](https://github.com/HUBioDataLab/DrugGEN)")
154
+
155
+ with gr.Accordion("Expand to display information about models", open=False):
156
+ gr.Markdown("""
157
+ ### Model Variations
158
+ - **DrugGEN-Prot**: composed of two GANs, incorporates protein features to the transformer decoder module of GAN2 (together with the de novo molecules generated by GAN1) to direct the target centric molecule design.
159
+ - **DrugGEN-CrossLoss**: composed of one GAN, the input of the GAN1 generator is the real molecules dataset and the GAN1 discriminator compares the generated molecules with the real inhibitors of the given target.
160
+ - **DrugGEN-NoTarget**: composed of one GAN, focuses on learning the chemical properties from the ChEMBL training dataset, no target-specific generation.
161
+ """)
162
+ model_name = gr.Radio(
163
+ choices=("DrugGEN-Prot", "DrugGEN-CrossLoss", "DrugGEN-NoTarget"),
164
+ value="DrugGEN-Prot",
165
+ label="Select a model to make inference",
166
+ info=" DrugGEN-Prot and DrugGEN-CrossLoss models design molecules to target the AKT1 protein"
167
+ )
168
+
169
+ num_molecules = gr.Number(
170
+ label="Number of molecules to generate",
171
+ precision=0, # integer input
172
+ minimum=1,
173
+ value=1000,
174
+ )
175
+ seed_num = gr.Number(
176
+ label="RNG seed value (can be used for reproducibility):",
177
+ precision=0, # integer input
178
+ minimum=0,
179
+ value=42,
180
+ )
181
+
182
+ submit_button = gr.Button(
183
+ value="Start Generating"
184
+ )
185
+
186
+ with gr.Column(scale=2):
187
+ scores_df = gr.Dataframe(
188
+ label="Scores",
189
+ )
190
+ file_download = gr.File(
191
+ label="Click to download generated molecules",
192
+ )
193
+ image_output = gr.Image(
194
+ label="Structures of randomly selected 12 de novo molecules from the inference set:"
195
+ )
196
+ # ).style(
197
+ # height=200*4,
198
+ # width=200*3,
199
+ # )
200
+
201
+ submit_button.click(function, inputs=[model_name, num_molecules, seed_num], outputs=[image_output, scores_df, file_download], api_name="inference")
202
+
203
+ demo.queue(concurrency_count=1)
204
+ demo.launch()