Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torch_geometric.data import Data
|
4 |
+
from torch_geometric.utils import from_networkx
|
5 |
+
import networkx as nx
|
6 |
+
|
7 |
+
# Usamos el modelo GCN previamente entrenado (podrías cambiar por GAT si lo prefieres)
|
8 |
+
model.eval()
|
9 |
+
|
10 |
+
def predict_mutagenicity():
|
11 |
+
# Creamos un grafo de prueba simple (3 nodos conectados)
|
12 |
+
G = nx.Graph()
|
13 |
+
G.add_edges_from([(0, 1), (1, 2)])
|
14 |
+
nx.set_node_attributes(G, {i: [1, 0, 0, 1, 0, 1, 0] for i in G.nodes}, "x") # vector ficticio
|
15 |
+
|
16 |
+
# Convertimos a objeto PyG
|
17 |
+
pyg_data = from_networkx(G)
|
18 |
+
pyg_data.x = torch.tensor(list(nx.get_node_attributes(G, 'x').values()), dtype=torch.float)
|
19 |
+
pyg_data.edge_index = pyg_data.edge_index
|
20 |
+
pyg_data.batch = torch.tensor([0] * pyg_data.num_nodes)
|
21 |
+
|
22 |
+
pyg_data = pyg_data.to(device)
|
23 |
+
with torch.no_grad():
|
24 |
+
out = model(pyg_data.x, pyg_data.edge_index, pyg_data.batch)
|
25 |
+
pred = out.argmax(dim=1).item()
|
26 |
+
|
27 |
+
return "Mutagénico" if pred == 1 else "No mutagénico"
|
28 |
+
|
29 |
+
gr.Interface(fn=predict_mutagenicity, inputs=[], outputs="text",
|
30 |
+
title="Clasificador de Moléculas con GNN",
|
31 |
+
description="Demo simple de GCN sobre grafos moleculares (MUTAG)").launch()
|