Create handler.py
Browse files- handler.py +48 -0
handler.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
|
5 |
+
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
6 |
+
|
7 |
+
### Instruction:
|
8 |
+
{}
|
9 |
+
|
10 |
+
### Input:
|
11 |
+
{}
|
12 |
+
|
13 |
+
### Response:
|
14 |
+
{}"""
|
15 |
+
|
16 |
+
|
17 |
+
class EndpointHandler:
|
18 |
+
def __init__(self, path=""):
|
19 |
+
# load model and processor from path
|
20 |
+
self.model = AutoModelForCausalLM.from_pretrained(path, device_map="auto")
|
21 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
22 |
+
|
23 |
+
with open(f"{path}/zero_shot_cot_prompt.txt", 'r') as file:
|
24 |
+
self.instruction_prompt = file.read()
|
25 |
+
|
26 |
+
|
27 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
28 |
+
sentence = data.pop("inputs",data)
|
29 |
+
|
30 |
+
inputs = self.tokenizer(
|
31 |
+
[
|
32 |
+
alpaca_prompt.format(
|
33 |
+
self.instruction_prompt, # instruction
|
34 |
+
sentence, # input
|
35 |
+
"", # output - leave this blank for generation!
|
36 |
+
)
|
37 |
+
], return_tensors="pt")
|
38 |
+
|
39 |
+
outputs = self.model.generate(**inputs,
|
40 |
+
max_new_tokens=1048,
|
41 |
+
use_cache=True,
|
42 |
+
top_p=0.1,
|
43 |
+
temperature=0.001)
|
44 |
+
|
45 |
+
outputs = self.tokenizer.batch_decode(outputs)[0]
|
46 |
+
response = outputs.split("### Response:")[1].split("<|end_of_text|>")[0]
|
47 |
+
|
48 |
+
return [{"generated_text": response}]
|