Commit
·
a050cb0
1
Parent(s):
ecba4f9
Upload 2 files
Browse files- handler.py +41 -0
- requirements.txt +7 -0
handler.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import transformers
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16
|
| 7 |
+
|
| 8 |
+
class EndpointHandler:
|
| 9 |
+
def __init__(self, path=""):
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
| 12 |
+
tokenizer.padding_side = "left"
|
| 13 |
+
tokenizer.pad_token = tokenizer.unk_token
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
path,
|
| 16 |
+
return_dict=True,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
load_in_8bit=True,
|
| 19 |
+
torch_dtype=torch.bfloat16,
|
| 20 |
+
trust_remote_code=True
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
generation_config = model.generation_config
|
| 24 |
+
generation_config.max_new_tokens = 200
|
| 25 |
+
generation_config.temperature = 0.7
|
| 26 |
+
generation_config.top_p = 0.7
|
| 27 |
+
generation_config.num_return_sequences = 1
|
| 28 |
+
generation_config.pad_token_id = tokenizer.pad_token_id
|
| 29 |
+
generation_config.eos_token_id = tokenizer.eos_token_id
|
| 30 |
+
self.generation_config = generation_config
|
| 31 |
+
|
| 32 |
+
self.pipeline = transformers.pipeline(
|
| 33 |
+
"text-generation",
|
| 34 |
+
model=model,
|
| 35 |
+
tokenizer=tokenizer
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 39 |
+
prompt = data.pop("inputs", data)
|
| 40 |
+
result = self.pipeline(prompt, generation_config=self.generation_config)
|
| 41 |
+
return result
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
bitsandbytes
|
| 4 |
+
accelerate
|
| 5 |
+
loralib
|
| 6 |
+
einops
|
| 7 |
+
datasets
|