-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.py
238 lines (190 loc) · 8.13 KB
/
launch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Copyright 2024 Llamole Team
#
# This code is inspired by the HuggingFace's transformers library.
# https://github.com/huggingface/transformers/blob/v4.40.0/examples/pytorch/summarization/run_summarization.py
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import yaml
import numpy as np
import gradio as gr
import random
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import AllChem
from src.webui.workflow import load_model_and_tokenizer, process_input, generate
from src.webui.elements import create_input_components
# Load candidates
with open('data/molqa_material_examples.json', 'r') as f:
material_examples = json.load(f)
with open('data/molqa_drug_examples.json', 'r') as f:
drug_examples = json.load(f)
# Add type to each example
for example in material_examples:
example['type'] = 'Material'
for example in drug_examples:
example['type'] = 'Drug'
# Function to process property values
def process_property(value):
return 1e-8 if value == 0 else value
# Add type to each example and process property values
for example in material_examples:
example['type'] = 'Material'
for prop in ['CO2', 'N2', 'O2', 'FFV']:
if prop in example['property']:
example['property'][prop] = process_property(example['property'][prop])
# Combine examples
all_examples = material_examples + drug_examples
# Get default values from the first material example
default_values = drug_examples[0]
# Load property ranges and arguments
with open('data/property_ranges.json', 'r') as f:
property_ranges = json.load(f)
# with open('config/generate/qwen_material.yaml', 'r') as file:
with open('config/generate/llama_material.yaml', 'r') as file:
args_dict = yaml.safe_load(file)
# Load model and tokenizer outside the function
model, tokenizer, generating_args = load_model_and_tokenizer(args_dict)
def format_example(example):
formatted = [example['instruction']]
# Determine if it's a drug or material example based on properties
is_drug = any(prop in example.get('property', {}) for prop in ["HIV", "BBBP", "BACE"])
formatted.append("Drug" if is_drug else "Material")
# Handle drug properties
for prop in ["HIV", "BBBP", "BACE"]:
value = example.get('property', {}).get(prop, float('nan'))
formatted.append(value if not np.isnan(value) else "NAN")
# Handle material properties
for prop in ["CO2", "N2", "O2", "FFV", "TC"]:
value = example.get('property', {}).get(prop, float('nan'))
formatted.append(value if not np.isnan(value) else 0) # 0 represents NAN for material properties
# Handle synthetic properties
for prop in ["SC", "SA"]:
value = example.get('property', {}).get(prop, float('nan'))
formatted.append(value if not np.isnan(value) else float('nan'))
return formatted
# Prepare examples
formatted_examples = [format_example(example) for example in all_examples]
def random_example(examples):
example = random.choice(examples)
property_type = example['type']
outputs = [example['instruction'], property_type]
for prop in ["HIV", "BBBP", "BACE"]:
outputs.append(example['property'].get(prop, "NAN"))
for prop in ["CO2", "N2", "O2", "FFV", "TC"]:
outputs.append(example['property'].get(prop, 0))
for prop in ["SC", "SA"]:
outputs.append(example['property'].get(prop, float('nan')))
return outputs
def generate_and_visualize(instruction, property_type, HIV, BBBP, BACE, CO2, N2, O2, FFV, TC, SC, SA):
properties = {
"HIV": float('nan') if HIV == "NAN" else HIV,
"BBBP": float('nan') if BBBP == "NAN" else BBBP,
"BACE": float('nan') if BACE == "NAN" else BACE,
"CO2": float('nan') if CO2 == 0 else CO2,
"N2": float('nan') if N2 == 0 else N2,
"O2": float('nan') if O2 == 0 else O2,
"FFV": float('nan') if FFV == 0 else FFV,
"TC": float('nan') if TC == 0 else TC,
"SC": SC,
"SA": SA
}
# Filter out NaN values
properties = {k: v for k, v in properties.items() if not np.isnan(v)}
print('instruction', instruction)
print('properties', properties)
results = run_molqa(instruction, **properties)
llm_response = results.get('llm_response', 'No response generated')
llm_smiles = results.get('llm_smiles')
llm_reactions = results['llm_reactions']
molecule_img = visualize_molecule(llm_smiles) if llm_smiles else None
reaction_steps = []
reaction_imgs = []
if llm_reactions:
for i, reaction_dict in enumerate(llm_reactions):
reaction = reaction_dict.get('reaction')
if reaction:
reaction_steps.append(f"Step {i+1}: {reaction}")
reaction_imgs.append(visualize_reaction(reaction))
return (
llm_response,
llm_smiles if llm_smiles else "No SMILES generated",
molecule_img,
gr.JSON(value=reaction_steps, visible=bool(reaction_steps)),
gr.Gallery(value=reaction_imgs, visible=bool(reaction_imgs))
)
def run_molqa(instruction: str, **properties) -> dict:
# Filter out properties with NaN values
filtered_properties = {k: v for k, v in properties.items() if not np.isnan(v)}
input_data = {
"instruction": instruction,
"input": "",
"property": filtered_properties
}
dataloader, gen_kwargs = process_input(input_data, model, tokenizer, generating_args)
generated_results = generate(model, dataloader, gen_kwargs)
return generated_results
def visualize_molecule(smiles: str) -> np.ndarray:
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
img = Draw.MolToImage(mol)
return np.array(img)
return np.zeros((300, 300, 3), dtype=np.uint8)
def visualize_reaction(reaction: str) -> np.ndarray:
rxn = AllChem.ReactionFromSmarts(reaction, useSmiles=True)
if rxn is not None:
img = Draw.ReactionToImage(rxn)
return np.array(img)
return np.zeros((300, 300, 3), dtype=np.uint8)
# Define property names and their full descriptions
property_names = {
"HIV": "HIV virus replication inhibition",
"BBBP": "Blood-brain barrier permeability",
"BACE": "Human β-secretase 1 inhibition",
"CO2": "CO2 Perm",
"N2": "N2 Perm",
"O2": "O2 Perm",
"FFV": "Fractional free volume",
"TC": "Thermal conductivity",
"SC": "Heuristic Synthetic Scores (SCScore)",
"SA": "Synthetic Synthetic Scores (SAScore)"
}
# Define outputs
outputs = [
gr.Textbox(label="Overall LLM Response"),
gr.Textbox(label="Generated SMILES"),
gr.Image(label="Generated Molecule"),
gr.JSON(label="Reaction Steps"),
gr.Gallery(label="Reaction Visualizations")
]
with gr.Blocks() as iface:
gr.Markdown("# Llamole Demo Interface")
gr.Markdown("Enter an instruction and property values to generate a molecule design.")
interface, instruction, property_type, drug_properties, material_properties, synthetic_properties = create_input_components(default_values, property_names, property_ranges)
random_btn = gr.Button("Random Example")
generate_btn = gr.Button("Generate")
for output in outputs:
output.render()
# Update the inputs for the generate button
all_inputs = [instruction, property_type]
all_inputs.extend(drug_properties.values())
all_inputs.extend(material_properties.values())
all_inputs.extend(synthetic_properties.values())
generate_btn.click(generate_and_visualize, inputs=all_inputs, outputs=outputs)
random_btn.click(
random_example,
inputs=gr.State(all_examples),
outputs=all_inputs
)
if __name__ == "__main__":
iface.launch(share=True)