|
import logging |
|
import pathlib |
|
|
|
import gradio as gr |
|
import pandas as pd |
|
from gt4sd.algorithms.generation.torchdrug import ( |
|
TorchDrugGenerator, |
|
TorchDrugGCPN, |
|
TorchDrugGraphAF, |
|
) |
|
|
|
from gt4sd.algorithms.registry import ApplicationsRegistry |
|
from utils import draw_grid_generate |
|
|
|
logger = logging.getLogger(__name__) |
|
logger.addHandler(logging.NullHandler()) |
|
|
|
TITLE = "MoLeR" |
|
|
|
|
|
def run_inference(algorithm: str, algorithm_version: str, number_of_samples: int): |
|
|
|
if algorithm == "GCPN": |
|
config = TorchDrugGCPN(algorithm_version=algorithm_version) |
|
elif algorithm == "GraphAF": |
|
config = TorchDrugGraphAF(algorithm_version=algorithm_version) |
|
else: |
|
raise ValueError(f"Unsupported model {algorithm}.") |
|
|
|
model = TorchDrugGenerator(configuration=config) |
|
samples = list(model.sample(number_of_samples)) |
|
|
|
return draw_grid_generate(samples=samples, n_cols=5) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
all_algos = ApplicationsRegistry.list_available() |
|
algos = [ |
|
x["algorithm_version"] |
|
for x in list(filter(lambda x: "TorchDrug" in x["algorithm_name"], all_algos)) |
|
] |
|
|
|
|
|
metadata_root = pathlib.Path(__file__).parent.joinpath("model_cards") |
|
|
|
examples = pd.read_csv(metadata_root.joinpath("examples.csv"), header=None).fillna( |
|
"" |
|
) |
|
|
|
with open(metadata_root.joinpath("article.md"), "r") as f: |
|
article = f.read() |
|
with open(metadata_root.joinpath("description.md"), "r") as f: |
|
description = f.read() |
|
|
|
demo = gr.Interface( |
|
fn=run_inference, |
|
title="TorchDrug (GCPN and GraphAF)", |
|
inputs=[ |
|
gr.Dropdown(["GCPN", "GraphAF"], label="Algorithm", value="GCPN"), |
|
gr.Dropdown( |
|
list(set(algos)), label="Algorithm version", value="zinc250k_v0" |
|
), |
|
gr.Slider( |
|
minimum=1, maximum=50, value=10, label="Number of samples", step=1 |
|
), |
|
], |
|
outputs=gr.HTML(label="Output"), |
|
article=article, |
|
description=description, |
|
examples=examples.values.tolist(), |
|
) |
|
demo.launch(debug=True, show_error=True) |
|
|