joaojanini commited on
Commit
3b7c54f
·
1 Parent(s): 4c37e72

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +8 -8
handler.py CHANGED
@@ -3,13 +3,13 @@ from transformers import AutoTokenizer, AutoModelForMaskedLM
3
  import torch
4
 
5
  class EndpointHandler:
6
- def __init__(self, model_name: str = "naver/splade-cocondenser-ensembledistil",
7
- max_seq_length: int = 256, device: str = "cpu"):
 
 
8
  # Load tokenizer and model
9
- self.tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- self.model = AutoModelForMaskedLM.from_pretrained(model_name).to(device)
11
- self.max_seq_length = max_seq_length
12
- self.device = device
13
 
14
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
15
  """
@@ -29,7 +29,7 @@ class EndpointHandler:
29
  return_tensors="pt",
30
  padding=True,
31
  truncation=True,
32
- max_length=self.max_seq_length,
33
  ).to(self.device)
34
  with torch.no_grad():
35
  logits = self.model(**inputs).logits
@@ -48,4 +48,4 @@ class EndpointHandler:
48
  {"indices": nz_tokens.tolist(), "values": nz_weights.tolist()}
49
  )
50
 
51
- return output[0] if isinstance(inputs, str) else output
 
3
  import torch
4
 
5
  class EndpointHandler:
6
+ def __init__(self, path: str = ""):
7
+ # Device to use for inference. "cuda" if CUDA is available, else "cpu"
8
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
  # Load tokenizer and model
11
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
12
+ self.model = AutoModelForMaskedLM.from_pretrained(path).to(self.device)
 
 
13
 
14
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
15
  """
 
29
  return_tensors="pt",
30
  padding=True,
31
  truncation=True,
32
+ max_length=self.model.config.max_position_embeddings, # Max length according to the model configuration
33
  ).to(self.device)
34
  with torch.no_grad():
35
  logits = self.model(**inputs).logits
 
48
  {"indices": nz_tokens.tolist(), "values": nz_weights.tolist()}
49
  )
50
 
51
+ return output[0] if isinstance(inputs, str) else output