Spaces:
Sleeping
Sleeping
Delete model.py
Browse files
model.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
SERAPH model architecture definition and weight-loading helper.
|
| 3 |
-
"""
|
| 4 |
-
import torch
|
| 5 |
-
import torch.nn as nn
|
| 6 |
-
from huggingface_hub import hf_hub_download
|
| 7 |
-
from transformers import EsmModel, EsmTokenizer
|
| 8 |
-
|
| 9 |
-
from config import HF_REPO_ID, WEIGHTS_FILE, ESM_MODEL_ID
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
class SERAPH(nn.Module):
|
| 13 |
-
"""ESM-2 backbone + Conv1D + BiLSTM head for 3-state secondary
|
| 14 |
-
structure prediction (Helix / Sheet / Coil)."""
|
| 15 |
-
|
| 16 |
-
def __init__(self, esm_model, conv_channels=256, kernel_size=7,
|
| 17 |
-
lstm_hidden=256, num_classes=3, dropout=0.3, freeze_esm=True):
|
| 18 |
-
super().__init__()
|
| 19 |
-
self.esm = esm_model
|
| 20 |
-
if freeze_esm:
|
| 21 |
-
for param in self.esm.encoder.layer[:-2].parameters():
|
| 22 |
-
param.requires_grad = False
|
| 23 |
-
esm_embed_dim = self.esm.config.hidden_size
|
| 24 |
-
self.conv = nn.Conv1d(esm_embed_dim, conv_channels, kernel_size=kernel_size, padding=kernel_size // 2)
|
| 25 |
-
self.bn = nn.BatchNorm1d(conv_channels)
|
| 26 |
-
self.dropout = nn.Dropout(dropout)
|
| 27 |
-
self.bilstm = nn.LSTM(conv_channels, lstm_hidden, num_layers=2, batch_first=True, bidirectional=True)
|
| 28 |
-
self.fc = nn.Linear(lstm_hidden * 2, num_classes)
|
| 29 |
-
|
| 30 |
-
def forward(self, input_ids, attention_mask=None):
|
| 31 |
-
x = self.esm(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
|
| 32 |
-
x = x.transpose(1, 2)
|
| 33 |
-
x = torch.relu(self.bn(self.conv(x)))
|
| 34 |
-
x = self.dropout(x)
|
| 35 |
-
x = x.transpose(1, 2)
|
| 36 |
-
x, _ = self.bilstm(x)
|
| 37 |
-
x = self.dropout(x)
|
| 38 |
-
return self.fc(x)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def load_model():
|
| 42 |
-
"""Downloads the ESM-2 backbone + tokenizer and the fine-tuned SERAPH
|
| 43 |
-
checkpoint, and returns a ready-to-use (model, tokenizer) pair."""
|
| 44 |
-
print("Loading ESM2 backbone...")
|
| 45 |
-
esm = EsmModel.from_pretrained(ESM_MODEL_ID)
|
| 46 |
-
tokenizer = EsmTokenizer.from_pretrained(ESM_MODEL_ID)
|
| 47 |
-
|
| 48 |
-
print("Downloading SERAPH weights...")
|
| 49 |
-
weights_path = hf_hub_download(repo_id=HF_REPO_ID, filename=WEIGHTS_FILE)
|
| 50 |
-
checkpoint = torch.load(weights_path, map_location="cpu")
|
| 51 |
-
|
| 52 |
-
model = SERAPH(esm_model=esm)
|
| 53 |
-
model.load_state_dict(checkpoint["model_state_dict"])
|
| 54 |
-
model.eval()
|
| 55 |
-
print("SERAPH model ready.")
|
| 56 |
-
|
| 57 |
-
return model, tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|