brocks1234 commited on
Commit
95c104f
·
verified ·
1 Parent(s): 4fd68aa

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +28 -0
handler.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict, List
2
+ from transformers import AutoTokenizer, AutoModel
3
+ import torch
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ # We point directly to the original weights
8
+ self.model_id = "zhihan1996/DNABERT-2-117M"
9
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_id, trust_remote_code=True)
10
+ self.model = AutoModel.from_pretrained(self.model_id, trust_remote_code=True)
11
+ if torch.cuda.is_available():
12
+ self.model = self.model.to("cuda")
13
+
14
+ def __call__(self, data: Dict[str, Any]) -> List[float]:
15
+ inputs = data.pop("inputs", data)
16
+
17
+ # DNA Tokenization
18
+ encoded_input = self.tokenizer(inputs, return_tensors='pt')
19
+ if torch.cuda.is_available():
20
+ encoded_input = {k: v.to("cuda") for k, v in encoded_input.items()}
21
+
22
+ with torch.no_grad():
23
+ outputs = self.model(**encoded_input)
24
+
25
+ # Returns a 768-dimensional vector representing the DNA sequence
26
+ embeddings = outputs[0][0].mean(dim=0).cpu().numpy().tolist()
27
+
28
+ return embeddings