Views
No views yet


1from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2import re
3from rdkit.Chem import MolFromSmiles
4import string
5from rdkit import RDLogger
6RDLogger.DisableLog('rdApp.*')
7atoms_tokens = ['Ag','Al','As','Au','B','Ba','Bi','Br','C','Ca',
8 'Cd','Cl','Co','Cr','Cs','Cu','F','Fe','Ga','Gd',
9 'Ge','H','Hg','I','In','K','Li','M','Mg','Mn',
10 'Mo','N','Na','O','P','Pt','Ru','S','Sb','Sc',
11 'Se','Si','Sn','V','W','Z','Zn','c','e','n','o','p','s']
12atoms_tokens = sorted(atoms_tokens, key=lambda s: len(s), reverse=True)
13SMI_REGEX_PATTERN = r"(\[|\]|\(|\)|\.|=|#|-|\+|\\|\/|:|~|@|\?|>>?|\*|\$|\%[0-9]{2}|[0-9]|" + \
14 '|'.join(atoms_tokens) + ")"
15regex = re.compile(SMI_REGEX_PATTERN)
16def clean_output_sequence(output_sequence):
17 return output_sequence.replace('</s>', '').replace('<sm_', '').replace(' sm_', '').replace('>', '').strip()
18def add_special_symbols(text):
19 output = []
20 for word in text.split():
21 tokens = [token for token in regex.findall(word)]
22 if len(tokens) > 4 and (word == ''.join(tokens)) and MolFromSmiles(word):
23 output.append(''.join(['<sm_'+t+'>' for t in tokens]))
24 else:
25 output.append(word)
26 return ' '.join(output)
27PROMPT = """Given the following reactants and reagents, please provide a possible product.
28 CCN(CC)CC.CCN=C=NCCCN(C)C.CN(C)C=O.Cl.NC1=CC=C(Cl)C=C1N.O.O=C(O)CCCCCNC(=O)C=C1C2=CC=CC=C2C2=CC=CC=C12.OC1=CC=CC2=C1N=NN2.[Cl-].[Na+]"""
29PROMPT = add_special_symbols(PROMPT)1 model = AutoModelForSeq2SeqLM.from_pretrained('insilicomedicine/nach0_base')
2 tokenizer = AutoTokenizer.from_pretrained('insilicomedicine/nach0_base')1input_text_ids = tokenizer(PROMPT, padding="longest", max_length=512, truncation=True, return_tensors="pt")
2generated_text_ids = model.generate(**input_text_ids, do_sample=True, top_k=100, top_p=0.95, max_length=512)
3generated_text = tokenizer.batch_decode(generated_text_ids, skip_special_tokens=True)[0]
4generated_text = clean_output_sequence(generated_text)# NC1=CC=C(Cl)C=C1NC(=O)CCCCCNC(=O)C=C1C2=CC=CC=C2C2=CC=CC=C12add_special_symbols function mentioned above.src_file_name and tgt_file_name fields to the files where the input (prompts) and target (responses) data are stored.restore_from_path field to the NeMo checkpoint path.write_predictions_to_file to True.output_file_path_prefix field to set the output file prefix.@article{D4SC00966E,
author ="Livne, Micha and Miftahutdinov, Zulfat and Tutubalina, Elena and Kuznetsov, Maksim and Polykovskiy, Daniil and Brundyn, Annika and Jhunjhunwala, Aastha and Costa, Anthony and Aliper, Alex and Aspuru-Guzik, Alán and Zhavoronkov, Alex",
title ="nach0: multimodal natural and chemical languages foundation model",
journal ="Chem. Sci.",
year ="2024",
volume ="15",
issue ="22",
pages ="8380-8389",
publisher ="The Royal Society of Chemistry",
doi ="10.1039/D4SC00966E",
url ="http://dx.doi.org/10.1039/D4SC00966E",
}