import torch import torch.nn as nn import dgl from dgl.nn.pytorch import GraphConv, AvgPooling, EdgeWeightNorm import gradio as gr class ModifiedGCN(nn.Module): def __init__(self, input_dim, edge_dim, output_dim, dropout_rate=0.5): super(ModifiedGCN, self).__init__() hidden_dims = [16, 32, 32, 16] self.conv1 = GraphConv(input_dim, hidden_dims[0], allow_zero_in_degree=True) self.bn1 = nn.BatchNorm1d(hidden_dims[0]) self.conv2 = GraphConv(hidden_dims[0], hidden_dims[1], allow_zero_in_degree=True) self.bn2 = nn.BatchNorm1d(hidden_dims[1]) self.conv3 = GraphConv(hidden_dims[1], hidden_dims[2], allow_zero_in_degree=True) self.bn3 = nn.BatchNorm1d(hidden_dims[2]) self.conv4 = GraphConv(hidden_dims[2], hidden_dims[3], allow_zero_in_degree=True) self.bn4 = nn.BatchNorm1d(hidden_dims[3]) self.pool = AvgPooling() self.classify = nn.Linear(hidden_dims[3], output_dim) self.elu1 = nn.ELU() self.elu2 = nn.ELU() self.elu3 = nn.ELU() self.elu4 = nn.ELU() self.edge_weight_norm = EdgeWeightNorm() def forward(self, g, features, edge_weights): edge_weights = self.edge_weight_norm(g, edge_weights) x = self.elu1(self.bn1(self.conv1(g, features, edge_weight=edge_weights))) x = self.elu2(self.bn2(self.conv2(g, x, edge_weight=edge_weights))) x = self.elu3(self.bn3(self.conv3(g, x, edge_weight=edge_weights))) x = self.elu4(self.bn4(self.conv4(g, x, edge_weight=edge_weights))) x = self.pool(g, x) return self.classify(x) # Load the complete model model_path = 'complete_model.pth' # Path relative to the Space model = torch.load(model_path, map_location=torch.device('cpu')) model.eval() # Define the inference function def predict(graph_data, features, edge_weights): src, dst = zip(*graph_data) g = dgl.graph((src, dst)) features = torch.tensor(features, dtype=torch.float32) edge_weights = torch.tensor(edge_weights, dtype=torch.float32) with torch.no_grad(): output = model(g, features, edge_weights) return output.numpy().tolist() # Create the Gradio interface iface = gr.Interface( fn=predict, inputs=[ gr.Dataframe(headers=["src", "dst"], type="array"), # Graph data gr.Dataframe(headers=["features"], type="array"), # Node features gr.Dataframe(headers=["edge_weights"], type="array") # Edge weights ], outputs="json" ) # Launch the app iface.launch()