Update handler.py
Browse files- handler.py +19 -14
handler.py
CHANGED
|
@@ -6,19 +6,27 @@ class EndpointHandler:
|
|
| 6 |
def __init__(self, path=""):
|
| 7 |
self.model_id = "zhihan1996/DNABERT-2-117M"
|
| 8 |
|
| 9 |
-
# 1.
|
| 10 |
-
#
|
| 11 |
-
config = BertConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# 2. Load the tokenizer normally
|
| 14 |
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, trust_remote_code=True)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# We use
|
| 18 |
self.model = BertModel.from_pretrained(
|
| 19 |
self.model_id,
|
| 20 |
config=config,
|
| 21 |
-
trust_remote_code=False,
|
| 22 |
ignore_mismatched_sizes=True
|
| 23 |
)
|
| 24 |
|
|
@@ -27,12 +35,9 @@ class EndpointHandler:
|
|
| 27 |
self.model.eval()
|
| 28 |
|
| 29 |
def __call__(self, data: Dict[str, Any]) -> List[float]:
|
| 30 |
-
# Handle input strings or dictionaries
|
| 31 |
inputs = data.pop("inputs", data)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Standard tokenization
|
| 36 |
encoded_input = self.tokenizer(
|
| 37 |
inputs,
|
| 38 |
return_tensors='pt',
|
|
@@ -45,10 +50,10 @@ class EndpointHandler:
|
|
| 45 |
encoded_input = {k: v.to("cuda") for k, v in encoded_input.items()}
|
| 46 |
|
| 47 |
with torch.no_grad():
|
|
|
|
| 48 |
outputs = self.model(**encoded_input)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
# index 0 is the last_hidden_state
|
| 52 |
embeddings = outputs[0][0].mean(dim=0).cpu().numpy().tolist()
|
| 53 |
|
| 54 |
return embeddings
|
|
|
|
| 6 |
def __init__(self, path=""):
|
| 7 |
self.model_id = "zhihan1996/DNABERT-2-117M"
|
| 8 |
|
| 9 |
+
# 1. MANUALLY build the configuration.
|
| 10 |
+
# By NOT using .from_pretrained(), we avoid the custom modeling code entirely.
|
| 11 |
+
config = BertConfig(
|
| 12 |
+
vocab_size=32000,
|
| 13 |
+
hidden_size=768,
|
| 14 |
+
num_hidden_layers=12,
|
| 15 |
+
num_attention_heads=12,
|
| 16 |
+
intermediate_size=3072,
|
| 17 |
+
max_position_embeddings=512,
|
| 18 |
+
type_vocab_size=2,
|
| 19 |
+
is_decoder=False
|
| 20 |
+
)
|
| 21 |
|
|
|
|
| 22 |
self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, trust_remote_code=True)
|
| 23 |
|
| 24 |
+
# 2. Load the weights into a standard, safe BertModel
|
| 25 |
+
# We use the original weights but our 'clean' config
|
| 26 |
self.model = BertModel.from_pretrained(
|
| 27 |
self.model_id,
|
| 28 |
config=config,
|
| 29 |
+
trust_remote_code=False,
|
| 30 |
ignore_mismatched_sizes=True
|
| 31 |
)
|
| 32 |
|
|
|
|
| 35 |
self.model.eval()
|
| 36 |
|
| 37 |
def __call__(self, data: Dict[str, Any]) -> List[float]:
|
|
|
|
| 38 |
inputs = data.pop("inputs", data)
|
| 39 |
+
|
| 40 |
+
# Tokenization
|
|
|
|
|
|
|
| 41 |
encoded_input = self.tokenizer(
|
| 42 |
inputs,
|
| 43 |
return_tensors='pt',
|
|
|
|
| 50 |
encoded_input = {k: v.to("cuda") for k, v in encoded_input.items()}
|
| 51 |
|
| 52 |
with torch.no_grad():
|
| 53 |
+
# This now runs standard PyTorch Attention layers
|
| 54 |
outputs = self.model(**encoded_input)
|
| 55 |
|
| 56 |
+
# Sequence embedding via mean pooling
|
|
|
|
| 57 |
embeddings = outputs[0][0].mean(dim=0).cpu().numpy().tolist()
|
| 58 |
|
| 59 |
return embeddings
|