Spaces:
Paused
Paused
Download services/gnn.py from noranisa/Sentimen-Analysis: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/spaces/noranisa/Sentimen-Analysis/resolve/main/services/gnn.py
- Command line
-
hf download hf://spaces/noranisa/Sentimen-Analysis/services/gnn.py
-
curl -L -o gnn.py https://huggingface.co/spaces/noranisa/Sentimen-Analysis/resolve/main/services/gnn.py
1.3 kB
| # ========================= | |
| # 🔥 SAFE GNN (NO CRASH) | |
| # ========================= | |
| def run_gnn(nodes, edges): | |
| try: | |
| import torch | |
| from torch_geometric.nn import GCNConv | |
| from torch_geometric.data import Data | |
| if len(nodes) == 0 or len(edges) == 0: | |
| return [] | |
| edge_index = [] | |
| for e in edges: | |
| edge_index.append([e["source"], e["target"]]) | |
| edge_index = torch.tensor(edge_index).t().contiguous() | |
| x = torch.rand((len(nodes), 16)) | |
| class GNN(torch.nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.conv1 = GCNConv(16, 16) | |
| self.conv2 = GCNConv(16, 2) | |
| def forward(self, x, edge_index): | |
| x = self.conv1(x, edge_index) | |
| x = torch.relu(x) | |
| x = self.conv2(x, edge_index) | |
| return x | |
| model = GNN() | |
| out = model(x, edge_index) | |
| return [ | |
| {"node": i, "score": float(out[i][0])} | |
| for i in range(len(nodes)) | |
| ] | |
| except Exception as e: | |
| print("⚠️ GNN fallback aktif:", e) | |
| # 🔥 fallback TANPA torch | |
| return [ | |
| {"node": i, "score": 0.5} | |
| for i in range(len(nodes)) | |
| ] |