Spaces:
Sleeping
Sleeping
fix: update model architecture, features, config to match trained model
Browse files- Dockerfile +1 -2
- config.py +4 -5
- models/loader.py +2 -2
- models/molecule_graph.py +53 -67
- models/multitask_gnn.py +45 -19
- requirements.txt +0 -2
- routes/models.py +1 -1
Dockerfile
CHANGED
|
@@ -18,9 +18,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 18 |
COPY requirements.txt .
|
| 19 |
RUN pip install --upgrade pip && \
|
| 20 |
pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cpu && \
|
| 21 |
-
pip install torch-scatter torch-sparse -f https://data.pyg.org/whl/torch-2.5.1+cpu.html && \
|
| 22 |
pip install torch-geometric && \
|
| 23 |
-
pip install rdkit
|
| 24 |
pip install -r requirements.txt
|
| 25 |
|
| 26 |
RUN mkdir -p /model
|
|
|
|
| 18 |
COPY requirements.txt .
|
| 19 |
RUN pip install --upgrade pip && \
|
| 20 |
pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cpu && \
|
|
|
|
| 21 |
pip install torch-geometric && \
|
| 22 |
+
pip install rdkit && \
|
| 23 |
pip install -r requirements.txt
|
| 24 |
|
| 25 |
RUN mkdir -p /model
|
config.py
CHANGED
|
@@ -29,11 +29,11 @@ CORS_ORIGIN_REGEX = "|".join(
|
|
| 29 |
if "*" in o
|
| 30 |
) or None
|
| 31 |
|
| 32 |
-
NUM_TASKS =
|
| 33 |
TASK_NAMES = [
|
| 34 |
"NR-AR", "NR-AhR", "NR-Aromatase", "NR-ER",
|
| 35 |
"NR-PPAR-gamma", "SR-ARE", "SR-ATAD5", "SR-HSE",
|
| 36 |
-
"SR-MMP", "SR-p53",
|
| 37 |
]
|
| 38 |
|
| 39 |
TASK_CLASSES = {
|
|
@@ -42,10 +42,9 @@ TASK_CLASSES = {
|
|
| 42 |
"NR-PPAR-gamma": "Nuclear Receptor", "SR-ARE": "Stress Response",
|
| 43 |
"SR-ATAD5": "Stress Response", "SR-HSE": "Stress Response",
|
| 44 |
"SR-MMP": "Stress Response", "SR-p53": "Stress Response",
|
| 45 |
-
"MUV-1": "MUV Benchmark", "MUV-2": "MUV Benchmark",
|
| 46 |
}
|
| 47 |
|
| 48 |
-
EDGE_DIM =
|
| 49 |
-
NODE_DIM =
|
| 50 |
HIDDEN_DIM = 128
|
| 51 |
DROPOUT = 0.15
|
|
|
|
| 29 |
if "*" in o
|
| 30 |
) or None
|
| 31 |
|
| 32 |
+
NUM_TASKS = 10
|
| 33 |
TASK_NAMES = [
|
| 34 |
"NR-AR", "NR-AhR", "NR-Aromatase", "NR-ER",
|
| 35 |
"NR-PPAR-gamma", "SR-ARE", "SR-ATAD5", "SR-HSE",
|
| 36 |
+
"SR-MMP", "SR-p53",
|
| 37 |
]
|
| 38 |
|
| 39 |
TASK_CLASSES = {
|
|
|
|
| 42 |
"NR-PPAR-gamma": "Nuclear Receptor", "SR-ARE": "Stress Response",
|
| 43 |
"SR-ATAD5": "Stress Response", "SR-HSE": "Stress Response",
|
| 44 |
"SR-MMP": "Stress Response", "SR-p53": "Stress Response",
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
+
EDGE_DIM = 11
|
| 48 |
+
NODE_DIM = 45
|
| 49 |
HIDDEN_DIM = 128
|
| 50 |
DROPOUT = 0.15
|
models/loader.py
CHANGED
|
@@ -4,7 +4,7 @@ import torch
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from safetensors.torch import load_file
|
| 6 |
from config import HF_MODEL_REPO, HF_TOKEN, MODEL_CACHE_DIR, NODE_DIM, EDGE_DIM, HIDDEN_DIM, NUM_TASKS, DROPOUT
|
| 7 |
-
from .multitask_gnn import
|
| 8 |
|
| 9 |
|
| 10 |
class ModelLoader:
|
|
@@ -40,7 +40,7 @@ class ModelLoader:
|
|
| 40 |
|
| 41 |
config = self.load_config()
|
| 42 |
|
| 43 |
-
model =
|
| 44 |
in_channels=config.get("node_dim", NODE_DIM),
|
| 45 |
edge_dim=config.get("edge_dim", EDGE_DIM),
|
| 46 |
hidden_dim=config.get("hidden_dim", HIDDEN_DIM),
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from safetensors.torch import load_file
|
| 6 |
from config import HF_MODEL_REPO, HF_TOKEN, MODEL_CACHE_DIR, NODE_DIM, EDGE_DIM, HIDDEN_DIM, NUM_TASKS, DROPOUT
|
| 7 |
+
from .multitask_gnn import MultiTaskGNN_ResGATv2_JK_VN
|
| 8 |
|
| 9 |
|
| 10 |
class ModelLoader:
|
|
|
|
| 40 |
|
| 41 |
config = self.load_config()
|
| 42 |
|
| 43 |
+
model = MultiTaskGNN_ResGATv2_JK_VN(
|
| 44 |
in_channels=config.get("node_dim", NODE_DIM),
|
| 45 |
edge_dim=config.get("edge_dim", EDGE_DIM),
|
| 46 |
hidden_dim=config.get("hidden_dim", HIDDEN_DIM),
|
models/molecule_graph.py
CHANGED
|
@@ -1,80 +1,66 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import torch
|
| 3 |
from rdkit import Chem
|
| 4 |
from rdkit.Chem import AllChem
|
| 5 |
from torch_geometric.data import Data
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
Chem.rdchem.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
Chem.rdchem.
|
| 25 |
-
Chem.rdchem.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
Chem.rdchem.BondType.SINGLE: [1, 0, 0, 0],
|
| 30 |
-
Chem.rdchem.BondType.DOUBLE: [0, 1, 0, 0],
|
| 31 |
-
Chem.rdchem.BondType.TRIPLE: [0, 0, 1, 0],
|
| 32 |
-
Chem.rdchem.BondType.AROMATIC: [0, 0, 0, 1],
|
| 33 |
-
}
|
| 34 |
|
| 35 |
|
| 36 |
def one_hot(val, choices):
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def get_atom_features(atom):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def get_bond_features(bond):
|
| 66 |
-
bond_type = BOND_ENCODER.get(bond.GetBondType(), [0, 0, 0, 0])
|
| 67 |
-
conjugated = 1.0 if bond.GetIsConjugated() else 0.0
|
| 68 |
-
|
| 69 |
-
stereo = bond.GetStereo()
|
| 70 |
-
stereo_vec = one_hot(stereo, [
|
| 71 |
-
Chem.rdchem.BondStereo.STEREONONE,
|
| 72 |
-
Chem.rdchem.BondStereo.STEREOANY,
|
| 73 |
-
Chem.rdchem.BondStereo.STEREOZ,
|
| 74 |
-
Chem.rdchem.BondStereo.STEREOE,
|
| 75 |
-
])
|
| 76 |
-
|
| 77 |
-
return bond_type + [conjugated] + stereo_vec
|
| 78 |
|
| 79 |
|
| 80 |
def smiles_to_graph(smiles: str) -> Data:
|
|
@@ -101,13 +87,13 @@ def smiles_to_graph(smiles: str) -> Data:
|
|
| 101 |
j = bond.GetEndAtomIdx()
|
| 102 |
edge_indices.append([i, j])
|
| 103 |
edge_indices.append([j, i])
|
| 104 |
-
bf = get_bond_features(bond)
|
| 105 |
edge_features.append(bf)
|
| 106 |
edge_features.append(bf)
|
| 107 |
|
| 108 |
if len(edge_indices) == 0:
|
| 109 |
edge_index = torch.zeros((2, 0), dtype=torch.long)
|
| 110 |
-
edge_attr = torch.zeros((0,
|
| 111 |
else:
|
| 112 |
edge_index = torch.tensor(edge_indices, dtype=torch.long).t().contiguous()
|
| 113 |
edge_attr = torch.tensor(edge_features, dtype=torch.float32)
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from rdkit import Chem
|
| 3 |
from rdkit.Chem import AllChem
|
| 4 |
from torch_geometric.data import Data
|
| 5 |
|
| 6 |
|
| 7 |
+
ATOM_TYPES = [6, 7, 8, 16, 15, 9, 17, 35, 53]
|
| 8 |
+
HYBRIDIZATION_TYPES = [
|
| 9 |
+
Chem.rdchem.HybridizationType.SP,
|
| 10 |
+
Chem.rdchem.HybridizationType.SP2,
|
| 11 |
+
Chem.rdchem.HybridizationType.SP3,
|
| 12 |
+
Chem.rdchem.HybridizationType.SP3D,
|
| 13 |
+
Chem.rdchem.HybridizationType.SP3D2,
|
| 14 |
+
Chem.rdchem.HybridizationType.UNSPECIFIED,
|
| 15 |
+
]
|
| 16 |
+
BOND_TYPES = [
|
| 17 |
+
Chem.rdchem.BondType.SINGLE,
|
| 18 |
+
Chem.rdchem.BondType.DOUBLE,
|
| 19 |
+
Chem.rdchem.BondType.TRIPLE,
|
| 20 |
+
Chem.rdchem.BondType.AROMATIC,
|
| 21 |
+
]
|
| 22 |
+
STEREO_TYPES = [
|
| 23 |
+
Chem.rdchem.BondStereo.STEREONONE,
|
| 24 |
+
Chem.rdchem.BondStereo.STEREOANY,
|
| 25 |
+
Chem.rdchem.BondStereo.STEREOZ,
|
| 26 |
+
Chem.rdchem.BondStereo.STEREOE,
|
| 27 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def one_hot(val, choices):
|
| 31 |
+
encoding = [0] * (len(choices) + 1)
|
| 32 |
+
for i, c in enumerate(choices):
|
| 33 |
+
if val == c:
|
| 34 |
+
encoding[i] = 1
|
| 35 |
+
return encoding
|
| 36 |
+
encoding[-1] = 1
|
| 37 |
+
return encoding
|
| 38 |
|
| 39 |
|
| 40 |
def get_atom_features(atom):
|
| 41 |
+
feat = []
|
| 42 |
+
feat += one_hot(atom.GetAtomicNum(), ATOM_TYPES)
|
| 43 |
+
feat += one_hot(atom.GetDegree(), list(range(7)))
|
| 44 |
+
feat += one_hot(atom.GetTotalValence(), list(range(7)))
|
| 45 |
+
feat += one_hot(atom.GetFormalCharge(), list(range(-3, 4)))
|
| 46 |
+
feat += one_hot(atom.GetHybridization(), HYBRIDIZATION_TYPES)
|
| 47 |
+
feat.append(1.0 if atom.GetIsAromatic() else 0.0)
|
| 48 |
+
feat += one_hot(atom.GetTotalNumHs(), list(range(5)))
|
| 49 |
+
feat.append(float(atom.GetNumRadicalElectrons()))
|
| 50 |
+
feat.append(1.0 if atom.IsInRing() else 0.0)
|
| 51 |
+
return feat
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def get_bond_features(bond, mol):
|
| 55 |
+
feat = [1 if bond.GetBondType() == bt else 0 for bt in BOND_TYPES]
|
| 56 |
+
feat.append(1.0 if bond.GetIsConjugated() else 0.0)
|
| 57 |
+
feat.append(1.0 if bond.IsInRing() else 0.0)
|
| 58 |
+
feat += [1 if bond.GetStereo() == s else 0 for s in STEREO_TYPES]
|
| 59 |
+
conf = mol.GetConformer()
|
| 60 |
+
i, j = bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()
|
| 61 |
+
dist = conf.GetAtomPosition(i).Distance(conf.GetAtomPosition(j))
|
| 62 |
+
feat.append(min(dist / 2.0, 1.0))
|
| 63 |
+
return feat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
def smiles_to_graph(smiles: str) -> Data:
|
|
|
|
| 87 |
j = bond.GetEndAtomIdx()
|
| 88 |
edge_indices.append([i, j])
|
| 89 |
edge_indices.append([j, i])
|
| 90 |
+
bf = get_bond_features(bond, mol)
|
| 91 |
edge_features.append(bf)
|
| 92 |
edge_features.append(bf)
|
| 93 |
|
| 94 |
if len(edge_indices) == 0:
|
| 95 |
edge_index = torch.zeros((2, 0), dtype=torch.long)
|
| 96 |
+
edge_attr = torch.zeros((0, 11), dtype=torch.float32)
|
| 97 |
else:
|
| 98 |
edge_index = torch.tensor(edge_indices, dtype=torch.long).t().contiguous()
|
| 99 |
edge_attr = torch.tensor(edge_features, dtype=torch.float32)
|
models/multitask_gnn.py
CHANGED
|
@@ -1,25 +1,45 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import torch.nn.functional as F
|
| 4 |
-
from torch_geometric.nn import
|
| 5 |
|
| 6 |
|
| 7 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def __init__(self, in_channels: int, edge_dim: int, hidden_dim: int,
|
| 9 |
num_tasks: int, dropout: float = 0.15):
|
| 10 |
-
super(
|
| 11 |
self.num_tasks = num_tasks
|
| 12 |
|
| 13 |
-
self.
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
self.
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
self.
|
| 22 |
-
nn.Linear(hidden_dim, hidden_dim),
|
| 23 |
nn.ReLU(),
|
| 24 |
nn.Dropout(dropout),
|
| 25 |
)
|
|
@@ -36,16 +56,22 @@ class MultiTaskGNN(nn.Module):
|
|
| 36 |
nn.init.xavier_uniform_(m.weight, gain=1.0)
|
| 37 |
if m.bias is not None:
|
| 38 |
nn.init.zeros_(m.bias)
|
| 39 |
-
|
| 40 |
def forward(self, x, edge_index, edge_attr, batch):
|
| 41 |
-
h = self.
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
logits = torch.cat([head(
|
| 51 |
return logits
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
import torch.nn.functional as F
|
| 4 |
+
from torch_geometric.nn import GATv2Conv, global_mean_pool, global_max_pool
|
| 5 |
|
| 6 |
|
| 7 |
+
class ResidualGATv2Layer(nn.Module):
|
| 8 |
+
def __init__(self, hidden_dim: int, edge_dim: int, dropout: float = 0.15):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.conv = GATv2Conv(hidden_dim, hidden_dim, heads=4, concat=False,
|
| 11 |
+
edge_dim=edge_dim, dropout=dropout)
|
| 12 |
+
self.norm = nn.LayerNorm(hidden_dim)
|
| 13 |
+
|
| 14 |
+
def forward(self, x, edge_index, edge_attr):
|
| 15 |
+
h = self.conv(x, edge_index, edge_attr)
|
| 16 |
+
h = F.relu(self.norm(h))
|
| 17 |
+
return h + x
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class MultiTaskGNN_ResGATv2_JK_VN(nn.Module):
|
| 21 |
def __init__(self, in_channels: int, edge_dim: int, hidden_dim: int,
|
| 22 |
num_tasks: int, dropout: float = 0.15):
|
| 23 |
+
super().__init__()
|
| 24 |
self.num_tasks = num_tasks
|
| 25 |
|
| 26 |
+
self.input_proj = nn.Sequential(
|
| 27 |
+
nn.Linear(in_channels, hidden_dim),
|
| 28 |
+
nn.LayerNorm(hidden_dim),
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
self.convs = nn.ModuleList([
|
| 32 |
+
ResidualGATv2Layer(hidden_dim, edge_dim, dropout)
|
| 33 |
+
for _ in range(3)
|
| 34 |
+
])
|
| 35 |
|
| 36 |
+
self.jk_proj = nn.Sequential(
|
| 37 |
+
nn.Linear(hidden_dim * 4, hidden_dim),
|
| 38 |
+
nn.LayerNorm(hidden_dim),
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
self.fc = nn.Sequential(
|
| 42 |
+
nn.Linear(hidden_dim * 2, hidden_dim),
|
| 43 |
nn.ReLU(),
|
| 44 |
nn.Dropout(dropout),
|
| 45 |
)
|
|
|
|
| 56 |
nn.init.xavier_uniform_(m.weight, gain=1.0)
|
| 57 |
if m.bias is not None:
|
| 58 |
nn.init.zeros_(m.bias)
|
|
|
|
| 59 |
def forward(self, x, edge_index, edge_attr, batch):
|
| 60 |
+
h = self.input_proj(x)
|
| 61 |
+
|
| 62 |
+
layer_outputs = [h]
|
| 63 |
+
for conv in self.convs:
|
| 64 |
+
h = conv(h, edge_index, edge_attr)
|
| 65 |
+
layer_outputs.append(h)
|
| 66 |
+
|
| 67 |
+
jk_cat = torch.cat(layer_outputs, dim=-1)
|
| 68 |
+
h_node = self.jk_proj(jk_cat)
|
| 69 |
|
| 70 |
+
h_mean = global_mean_pool(h_node, batch)
|
| 71 |
+
h_max = global_max_pool(h_node, batch)
|
| 72 |
|
| 73 |
+
h_cat = torch.cat([h_mean, h_max], dim=-1)
|
| 74 |
+
h_out = self.fc(h_cat)
|
| 75 |
|
| 76 |
+
logits = torch.cat([head(h_out) for head in self.heads], dim=1)
|
| 77 |
return logits
|
requirements.txt
CHANGED
|
@@ -22,5 +22,3 @@ httpx>=0.27
|
|
| 22 |
shap>=0.45
|
| 23 |
matplotlib>=3.8
|
| 24 |
Pillow>=10
|
| 25 |
-
|
| 26 |
-
Pillow>=10
|
|
|
|
| 22 |
shap>=0.45
|
| 23 |
matplotlib>=3.8
|
| 24 |
Pillow>=10
|
|
|
|
|
|
routes/models.py
CHANGED
|
@@ -10,7 +10,7 @@ async def list_models():
|
|
| 10 |
"models": [
|
| 11 |
{
|
| 12 |
"name": "toxipredict-gnn-v1",
|
| 13 |
-
"architecture": "
|
| 14 |
"num_tasks": NUM_TASKS,
|
| 15 |
"tasks": [
|
| 16 |
{"assay": name, "target_class": TASK_CLASSES.get(name, "Unknown")}
|
|
|
|
| 10 |
"models": [
|
| 11 |
{
|
| 12 |
"name": "toxipredict-gnn-v1",
|
| 13 |
+
"architecture": "MultiTaskGNN_ResGATv2_JK_VN (3×ResGATv2, 4 heads, JK concat, mean+max pool, 10 task heads)",
|
| 14 |
"num_tasks": NUM_TASKS,
|
| 15 |
"tasks": [
|
| 16 |
{"assay": name, "target_class": TASK_CLASSES.get(name, "Unknown")}
|