Handler
Browse files- handler.py +98 -0
- output_evaluate.txt +0 -0
- requirements.txt +4 -0
handler.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
3 |
+
from peft import PeftModel, PeftConfig
|
4 |
+
import torch
|
5 |
+
import time
|
6 |
+
|
7 |
+
class EndpointHandler:
|
8 |
+
def __init__(self, path="5iveDesignStudio/autotrain-TenderGPT-Festive-v2-0"):
|
9 |
+
# load the model
|
10 |
+
config = PeftConfig.from_pretrained(path)
|
11 |
+
|
12 |
+
bnb_config = BitsAndBytesConfig(
|
13 |
+
load_in_4bit=True,
|
14 |
+
bnb_4bit_use_double_quant=True,
|
15 |
+
bnb_4bit_quant_type="nf4",
|
16 |
+
bnb_4bit_compute_dtype=torch.float16,
|
17 |
+
)
|
18 |
+
|
19 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
20 |
+
config.base_model_name_or_path,
|
21 |
+
return_dict=True,
|
22 |
+
load_in_4bit=True,
|
23 |
+
device_map={"":0},
|
24 |
+
trust_remote_code=True,
|
25 |
+
quantization_config=bnb_config,
|
26 |
+
)
|
27 |
+
|
28 |
+
self.tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
29 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
30 |
+
|
31 |
+
self.model = PeftModel.from_pretrained(self.model, path)
|
32 |
+
|
33 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
34 |
+
|
35 |
+
#def __call__(self, data: Any) -> Dict[str, Any]:
|
36 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
|
37 |
+
"""
|
38 |
+
Args:
|
39 |
+
inputs :obj:`list`:. The object should be like {"context": "some word", "question": "some word"} containing:
|
40 |
+
- "context":
|
41 |
+
- "question":
|
42 |
+
Return:
|
43 |
+
A :obj:`list`:. The object returned should be like {"answer": "some word", time: "..."} containing:
|
44 |
+
- "answer": answer the question based on the context
|
45 |
+
- "time": the time run predict
|
46 |
+
"""
|
47 |
+
|
48 |
+
# process input
|
49 |
+
inputs = data.pop("inputs", data)
|
50 |
+
parameters = data.pop("parameters", None)
|
51 |
+
|
52 |
+
prompt = f"""Below is an instruction that describes a task. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
53 |
+
>>TITLE<<: Tender Response.
|
54 |
+
>>CONTEXT<<: You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe in a conversational tone. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
55 |
+
>>QUESTION<<: {inputs}
|
56 |
+
>>ANSWER<<:
|
57 |
+
""".strip()
|
58 |
+
|
59 |
+
# preprocess
|
60 |
+
batch = self.tokenizer(
|
61 |
+
prompt,
|
62 |
+
padding=True,
|
63 |
+
truncation=True,
|
64 |
+
return_tensors="pt"
|
65 |
+
).to(self.device)
|
66 |
+
|
67 |
+
# pass inputs with all kwargs in data
|
68 |
+
#if parameters is not None:
|
69 |
+
# outputs = self.model.generate(**inputs, **parameters)
|
70 |
+
#else:
|
71 |
+
# outputs = self.model.generate(**inputs)
|
72 |
+
|
73 |
+
# postprocess the prediction
|
74 |
+
#prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
75 |
+
|
76 |
+
generation_config = self.model.generation_config
|
77 |
+
generation_config.top_p = 0.75
|
78 |
+
generation_config.temperature = 0.7
|
79 |
+
generation_config.max_new_tokens = 140
|
80 |
+
generation_config.num_return_sequences = 1
|
81 |
+
generation_config.pad_token_id = self.tokenizer.eos_token_id
|
82 |
+
generation_config.eos_token_id = self.tokenizer.eos_token_id
|
83 |
+
|
84 |
+
start = time.time()
|
85 |
+
with torch.cuda.amp.autocast():
|
86 |
+
output_tokens = self.model.generate(
|
87 |
+
input_ids = batch.input_ids,
|
88 |
+
generation_config=generation_config,
|
89 |
+
)
|
90 |
+
end = time.time()
|
91 |
+
|
92 |
+
generated_text = self.tokenizer.decode(
|
93 |
+
output_tokens[0]
|
94 |
+
)
|
95 |
+
|
96 |
+
prediction = {'answer': generated_text.split('>>END<<')[0].split('>>ANSWER<<:')[1].strip(), 'time': f"{(end-start):.2f} s"}
|
97 |
+
|
98 |
+
return prediction
|
output_evaluate.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.0.0
|
2 |
+
git+https://github.com/huggingface/peft.git
|
3 |
+
bitsandbytes
|
4 |
+
einops
|