Upload handler.py
Browse files- handler.py +47 -0
handler.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline, set_seed
|
4 |
+
|
5 |
+
class EndpointHandler:
|
6 |
+
def __init__(self, path=""):
|
7 |
+
self.pipeline = pipeline(
|
8 |
+
"text-generation",
|
9 |
+
model="openai-community/gpt2",
|
10 |
+
device_map='auto',
|
11 |
+
#trust_remote_code=True,
|
12 |
+
model_kwargs={
|
13 |
+
"load_in_4bit": True
|
14 |
+
},
|
15 |
+
# batch_size=1,
|
16 |
+
)
|
17 |
+
# model.generation_config
|
18 |
+
|
19 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
20 |
+
"""
|
21 |
+
data args:
|
22 |
+
inputs (:obj: `str`)
|
23 |
+
parameters (:obj: `Dict`)
|
24 |
+
Return:
|
25 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
26 |
+
"""
|
27 |
+
# get inputs
|
28 |
+
inputs = data.pop("inputs", "")
|
29 |
+
# get additional date field
|
30 |
+
params = data.pop("parameters", ())
|
31 |
+
if not params:
|
32 |
+
params = dict()
|
33 |
+
set_seed(42)
|
34 |
+
# run normal prediction
|
35 |
+
generation = self.pipeline(inputs, **params)
|
36 |
+
# **generate_kwargs https://huggingface.co/docs/transformers/generation_strategies#customize-text-generation,
|
37 |
+
# https://huggingface.co/docs/transformers/generation_strategies#customize-text-generation
|
38 |
+
return generation
|
39 |
+
|
40 |
+
# Returns
|
41 |
+
|
42 |
+
# A list or a list of list of dict
|
43 |
+
|
44 |
+
# Returns one of the following dictionaries (cannot return a combination of both generated_text and generated_token_ids):
|
45 |
+
|
46 |
+
# generated_text (str, present when return_text=True) — The generated text.
|
47 |
+
# generated_token_ids (torch.Tensor or tf.Tensor, present when return_tensors=True) — The token ids of the generated text.
|