File size: 2,709 Bytes
d34a682
611a628
74a3db1
1de48e6
d34a682
a95ce8e
b2c5b3d
 
 
 
 
 
 
611a628
d34a682
 
a018e24
 
 
 
 
 
 
 
d34a682
f6c1dd0
4e460f3
a95ce8e
 
 
 
 
 
 
 
 
 
 
 
 
d34a682
4e460f3
f1b97af
 
 
4e460f3
f1b97af
 
 
 
 
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

import transformers
from transformers import GraphormerForGraphClassification
from transformers.models.graphormer.collating_graphormer import preprocess_item
import gradio as gr
import json

import os 
try:
    import toml
except ImportError:
    os.system('pip install toml')
    import toml
print('todo en orden')
model = GraphormerForGraphClassification.from_pretrained("PedroLancharesSanchez/graph-regression")

example= '''{
    "node_feat": [[0],[0],[0],[0],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[2],[0],[0],[0],[0],[0],[0],[3],[0],[0]],
    "edge_index": [[0,1,1,1,1,2,3,4,4,4,5,5,6,6,7,7,7,8,8,9,9,10,10,10,11,11,12,12,12,13,14,14,15,15,15,16,16,17,17,18,18,19,19,20,20,20,21,22,22,22,23,23],[1,0,2,3,4,1,1,1,5,23,4,6,5,7,6,8,22,7,9,8,10,9,11,22,10,12,11,13,14,12,12,15,14,16,20,15,17,16,18,17,19,18,20,15,19,21,20,7,10,23,4,22]],
    "edge_attr": [[1],[1],[1],[1],[1],[1],[1],[1],[2],[1],[2],[1],[1],[2],[2],[1],[1],[1],[1],[1],[2],[2],[1],[1],[1],[1],[1],[2],[1],[2],[1],[1],[1],[2],[1],[2],[1],[1],[2],[2],[1],[1],[2],[1],[2],[1],[1],[1],[1],[2],[1],[2]],
    "y": [3.1381945610046387],
    "num_nodes": 24
}'''

def predict(instancia):
    print(instancia)
    instancia=json.loads(instancia)
    instancia_preprocesada=preprocess_item(instancia)
    inputs={}
    inputs['input_nodes'] = torch.tensor([instancia_preprocesada['input_nodes']])
    inputs['input_edges'] = torch.tensor([instancia_preprocesada['input_edges']])
    inputs['attn_bias'] = torch.tensor([instancia_preprocesada['attn_bias']])
    inputs['in_degree'] = torch.tensor([instancia_preprocesada['in_degree']])
    inputs['out_degree'] = torch.tensor([instancia_preprocesada['out_degree']])
    inputs['spatial_pos'] = torch.tensor([instancia_preprocesada['spatial_pos']])
    inputs['attn_edge_type'] = torch.tensor([instancia_preprocesada['attn_edge_type']])
    with torch.no_grad():
        logits = model(**inputs).logits
    predicted_class_id = logits.argmax().item()
    return str(logits.item())

ejemplos = ['''{"node_feat": [[0],[0],[0],[0],[0],[0],[0],[0],[1],[0],[0],[0],[0],[1],[2],[0],[0],[0],[0],[0],[0],[3],[0],[0]],
     "edge_index": [[0,1,1,1,1,2,3,4,4,4,5,5,6,6,7,7,7,8,8,9,9,10,10,10,11,11,12,12,12,13,14,14,15,15,15,16,16,17,17,18,18,19,19,20,20,20,21,22,22,22,23,23]],
     "edge_attr": [[1],[1],[1],[1],[1],[1],[1],[1],[2],[1],[2],[1],[1],[2],[2],[1],[1],[1],[1],[1],[2],[2],[1],[1],[1],[1],[1],[2],[1],[2],[1],[1],[1],[2],[1],[2],[1],[1],[2],[2],[1],[1],[2],[1],[2],[1],[1],[1],[1],[2],[1],[2]],
     "y": [3.1381945610046387],
     "num_nodes": 24}'''
]

# Crear la interfaz Gradio
interfaz = gr.Interface(fn=predict, inputs="text", outputs='text', examples=ejemplos)
interfaz.launch(share=False)