File size: 1,481 Bytes
d93a43e 0d8eac0 6e75c39 1b279a5 6e75c39 1b279a5 6e75c39 0d8eac0 6e75c39 0d8eac0 1b279a5 a6fee7b 1b279a5 0029d81 0d8eac0 1ad2f1c 1b279a5 4c70bd8 1b279a5 |
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 |
import gradio as gr
from rdkit.Chem import Draw
from rdkit import Chem
import selfies as sf
sf_input
def greet1(name):
tokenizer = AutoTokenizer.from_pretrained("zjunlp/MolGen")
model = AutoModelForSeq2SeqLM.from_pretrained("zjunlp/MolGen")
sf_input = tokenizer(name, return_tensors="pt")
# beam search
molecules = model.generate(input_ids=sf_input["input_ids"],
attention_mask=sf_input["attention_mask"],
max_length=15,
min_length=5,
num_return_sequences=5,
num_beams=5)
sf_output = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True).replace(" ","") for g in molecules]
return sf_output
def greet2(name):
smis = [sf.decoder(i) for i in sf_output]
mols = []
for smi in smis:
mol = Chem.MolFromSmiles(smi)
mols.append(mol)
img = Draw.MolsToGridImage(
mols,
molsPerRow=4,
subImgSize=(200,200),
legends=['' for x in mols]
)
return img
greeter_1 = gr.Interface(greet1, inputs="text", outputs="text")
greeter_2 = gr.Interface(greet2 , inputs="text", outputs="image")
demo = gr.Parallel(greeter_1, greeter_2)
demo.launch()
#iface = gr.Interface(fn=greet, inputs="text", outputs="image", title="Molecular Language Model as Multi-task Generator",
# )
#iface.launch() |