mgutierrez commited on
Commit
0caaa58
·
verified ·
1 Parent(s): 4a2c3cb

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +36 -0
handler.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from peft import PeftModel
4
+ import torch
5
+
6
+ class EndpointHandler():
7
+ def __init__(self, path=""):
8
+ model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
9
+ model = PeftModel.from_pretrained(model, "srmorfi/phi3-mini-med-adapter")
10
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
11
+ self.model = model
12
+ self.tokenizer = tokenizer
13
+ self.model.eval()
14
+
15
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
16
+ """
17
+ Process input data and generate predictions using the model.
18
+
19
+ Args:
20
+ data (Dict[str, Any]): Input data containing either an "inputs" key
21
+ or the input directly in the data dictionary.
22
+
23
+ Returns:
24
+ List[Dict[str, Any]]: Processed model predictions that will be serialized and returned.
25
+ """
26
+ inputs = data.pop("inputs", data)
27
+ inputs = self.tokenizer(inputs, return_tensors="pt")
28
+
29
+ print(inputs)
30
+
31
+ with torch.no_grad():
32
+ outputs = self.model.generate(input_ids=inputs["input_ids"], max_new_tokens=20)
33
+ output = self.tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0]
34
+ predictions = [{"generated_text": output}]
35
+
36
+ return predictions