File size: 2,063 Bytes
c20f071
 
 
 
44c8341
 
 
 
bdac097
44c8341
5e62770
11a696d
0fbae15
 
06b7537
0fbae15
 
06b7537
0fbae15
44c8341
 
8b25912
44c8341
 
 
 
 
 
 
 
 
8b25912
 
 
 
44c8341
 
 
 
 
 
f1fe251
44c8341
 
 
0fbae15
 
44c8341
 
0fbae15
 
44c8341
 
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 os
os.system("pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu")
os.system("pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.12.0+cpu.html")

import gradio as gr
from glycowork.ml.processing import dataset_to_dataloader
import numpy as np
import torch
from glycowork.glycan_data.loader import lib


def fn(class_list):
  def f(glycan, model):
    if model == "No data augmentation":
      model = torch.load("model1.pt", map_location=torch.device('cpu'))
      model.eval()
    else:
      model = torch.load("model2.pt", map_location=torch.device('cpu'))
      model.eval()
    glycan = [glycan]
    label = [0]
    data = next(iter(dataset_to_dataloader(glycan, label, batch_size=1)))
    device = "cpu"
    if torch.cuda.is_available():
      device = "cuda:0"
    x = data.labels
    edge_index = data.edge_index
    batch = data.batch
    x = x.to(device)
    edge_index = edge_index.to(device)
    batch = batch.to(device) 
    pred = model(x,edge_index, batch).cpu().detach().numpy()[0]
    pred = np.exp(pred)/sum(np.exp(pred)) # Softmax 
    pred = [float(x) for x in pred]
    pred = {class_list[i]:pred[i] for i in range(15)}
    return pred
  return f

class_list=['Amoebozoa', 'Animalia', 'Bacteria', 'Bamfordvirae', 'Chromista', 'Euryarchaeota', 'Excavata', 'Fungi', 'Heunggongvirae', 
            'Orthornavirae', 'Pararnavirae', 'Plantae', 'Proteoarchaeota', 'Protista', 'Riboviria']

f = fn(class_list)  

demo = gr.Interface(
    fn=f,
    inputs=[gr.Textbox(label="Glycan sequence"), gr.Radio(label="Model",choices=["No data augmentation", "Random node deletion"])],
    outputs=[gr.Label(num_top_classes=15, label="Prediction")],
    allow_flagging=False,
    title="SweetNet demo",
    examples=[["GlcOSN(a1-4)GlcA(b1-4)GlcOSN(a1-4)GlcAOS(b1-4)GlcOSN(a1-4)GlcOSN", "No data augmentation"],
    ["Man(a1-2)Man(a1-3)[Man(a1-3)Man(a1-6)]Man(b1-4)GlcNAc(b1-4)GlcNAc", "Random node deletion"]]
)
demo.launch(debug=True)