Update handler.py
Browse files- handler.py +35 -35
handler.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
2 |
from transformers_stream_generator import init_stream_support
|
3 |
import re
|
4 |
init_stream_support()
|
@@ -22,45 +22,45 @@ Alice Gate: *Alice strides into the room with a smile, her eyes lighting up when
|
|
22 |
|
23 |
class EndpointHandler():
|
24 |
|
25 |
-
def __init__(self, path = "
|
|
|
|
|
26 |
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
27 |
self.model = AutoModelForCausalLM.from_pretrained(
|
28 |
path,
|
29 |
device_map = "auto",
|
30 |
-
load_in_8bit = True
|
|
|
|
|
31 |
)
|
32 |
|
33 |
def __call__(self, data):
|
34 |
inputs = data.pop("inputs", data)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
except Exception as e:
|
64 |
-
return {
|
65 |
-
"error": str(e)
|
66 |
-
}
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
2 |
from transformers_stream_generator import init_stream_support
|
3 |
import re
|
4 |
init_stream_support()
|
|
|
22 |
|
23 |
class EndpointHandler():
|
24 |
|
25 |
+
def __init__(self, path = ""):
|
26 |
+
path = ""
|
27 |
+
# quantization_config = BitsAndBytesConfig(llm_int8_enable_fp32_cpu_offload = True)
|
28 |
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
29 |
self.model = AutoModelForCausalLM.from_pretrained(
|
30 |
path,
|
31 |
device_map = "auto",
|
32 |
+
load_in_8bit = True,
|
33 |
+
torch_dtype = "auto",
|
34 |
+
low_cpu_mem_usage = True
|
35 |
)
|
36 |
|
37 |
def __call__(self, data):
|
38 |
inputs = data.pop("inputs", data)
|
39 |
+
prompt = template.format(
|
40 |
+
user_name = inputs["user_name"],
|
41 |
+
user_input = "\n".join(inputs["user_input"])
|
42 |
+
)
|
43 |
+
input_ids = self.tokenizer(
|
44 |
+
prompt,
|
45 |
+
return_tensors = "pt"
|
46 |
+
).input_ids
|
47 |
+
stream_generator = self.model.generate(
|
48 |
+
input_ids,
|
49 |
+
max_new_tokens = 50,
|
50 |
+
do_sample = True,
|
51 |
+
do_stream = True,
|
52 |
+
temperature = 0.5,
|
53 |
+
top_p = 0.9,
|
54 |
+
top_k = 0,
|
55 |
+
repetition_penalty = 1.1,
|
56 |
+
pad_token_id = 50256,
|
57 |
+
num_return_sequences = 1
|
58 |
+
)
|
59 |
+
result = []
|
60 |
+
for token in stream_generator:
|
61 |
+
result.append(self.tokenizer.decode(token))
|
62 |
+
response = "".join(result).strip()
|
63 |
+
if len(response) != 0 and result[-1] == "\n":
|
64 |
+
return {
|
65 |
+
"message": " ".join(filter(None, re.sub("\*.*?\*", "", response).split()))
|
66 |
+
}
|
|
|
|
|
|
|
|