zakerytclarke commited on
Commit
8a4e9e9
·
verified ·
1 Parent(s): 4e1ab77

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +9 -10
handler.py CHANGED
@@ -1,14 +1,15 @@
1
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import torch
3
 
 
4
  MAX_INPUT_TOKENS = 512
5
 
 
6
  class EndpointHandler:
7
  def __init__(self, path: str = ""):
8
- # IMPORTANT: on HF Endpoints, `path` is the mounted model directory (e.g. "/repository")
9
- # Use it exactly, otherwise transformers can't find config.json/model files.
10
- self.tokenizer = AutoTokenizer.from_pretrained(path)
11
- self.model = AutoModelForSeq2SeqLM.from_pretrained(path)
12
 
13
  self.model.eval()
14
  self.device = torch.device("cpu")
@@ -29,7 +30,7 @@ class EndpointHandler:
29
  raise ValueError("Missing required field 'inputs'.")
30
 
31
  # Support:
32
- # 1) inputs is a full prompt string (closest to your code)
33
  # 2) inputs is {"context": ..., "question": ...}
34
  if isinstance(inputs, str):
35
  prompt = inputs
@@ -40,12 +41,12 @@ class EndpointHandler:
40
  else:
41
  raise ValueError("inputs must be a string OR an object with 'context' and 'question'.")
42
 
43
- # Match your code: tokenizer(prompt, return_tensors="pt")
44
  enc = self.tokenizer(prompt, return_tensors="pt")
45
  input_ids = enc["input_ids"]
46
  attention_mask = enc["attention_mask"]
47
 
48
- # Keep only most recent tokens (left-truncate)
49
  if input_ids.shape[1] > MAX_INPUT_TOKENS:
50
  input_ids = input_ids[:, -MAX_INPUT_TOKENS:]
51
  attention_mask = attention_mask[:, -MAX_INPUT_TOKENS:]
@@ -53,13 +54,11 @@ class EndpointHandler:
53
  input_ids = input_ids.to(self.device)
54
  attention_mask = attention_mask.to(self.device)
55
 
56
- # Match your code: generate(..., do_sample=False)
57
  outputs = self.model.generate(
58
  input_ids=input_ids,
59
  attention_mask=attention_mask,
60
- do_sample=False
61
  )
62
 
63
- # Match your code: decode(..., skip_special_tokens=True)
64
  answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
65
  return {"generated_text": answer}
 
1
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
  import torch
3
 
4
+ MODEL_NAME = "teapotai/tinyteapot"
5
  MAX_INPUT_TOKENS = 512
6
 
7
+
8
  class EndpointHandler:
9
  def __init__(self, path: str = ""):
10
+ # EXACTLY like your snippet: load by repo id from Hugging Face Hub
11
+ self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
12
+ self.model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
 
13
 
14
  self.model.eval()
15
  self.device = torch.device("cpu")
 
30
  raise ValueError("Missing required field 'inputs'.")
31
 
32
  # Support:
33
+ # 1) inputs is a full prompt string (closest to your snippet)
34
  # 2) inputs is {"context": ..., "question": ...}
35
  if isinstance(inputs, str):
36
  prompt = inputs
 
41
  else:
42
  raise ValueError("inputs must be a string OR an object with 'context' and 'question'.")
43
 
44
+ # Match your code path
45
  enc = self.tokenizer(prompt, return_tensors="pt")
46
  input_ids = enc["input_ids"]
47
  attention_mask = enc["attention_mask"]
48
 
49
+ # Keep only the most recent tokens if too long
50
  if input_ids.shape[1] > MAX_INPUT_TOKENS:
51
  input_ids = input_ids[:, -MAX_INPUT_TOKENS:]
52
  attention_mask = attention_mask[:, -MAX_INPUT_TOKENS:]
 
54
  input_ids = input_ids.to(self.device)
55
  attention_mask = attention_mask.to(self.device)
56
 
 
57
  outputs = self.model.generate(
58
  input_ids=input_ids,
59
  attention_mask=attention_mask,
60
+ do_sample=False,
61
  )
62
 
 
63
  answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
64
  return {"generated_text": answer}