add requirement, and handler.py
Browse files- handler-3.py +32 -0
- requirements.txt +12 -0
handler-3.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM,AutoTokenizer,BitsAndBytesConfig
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class EndpointHandler():
|
| 6 |
+
|
| 7 |
+
def __init__(self, model_id="",HF_TOKEN=""):
|
| 8 |
+
self.bnb_config = BitsAndBytesConfig(
|
| 9 |
+
load_in_4bit=True,
|
| 10 |
+
bnb_4bit_quant_type="nf4",
|
| 11 |
+
bnb_4bit_compute_dtype=torch.bfloat16,)
|
| 12 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_id,
|
| 14 |
+
device_map={"":0},
|
| 15 |
+
quantization_config=self.bnb_config,
|
| 16 |
+
token=HF_TOKEN)
|
| 17 |
+
self.device = "cuda:0"
|
| 18 |
+
|
| 19 |
+
def __call__(self, input:str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
data args:
|
| 22 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 23 |
+
kwargs
|
| 24 |
+
Return:
|
| 25 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 26 |
+
"""
|
| 27 |
+
inputs = self.tokenizer(text, return_tensors="pt").to(self.device)
|
| 28 |
+
outputs = self.model.generate(**inputs, max_new_tokens=20)
|
| 29 |
+
result = (self.tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 30 |
+
return result
|
| 31 |
+
|
| 32 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bitsandbytes==0.42.0
|
| 2 |
+
accelerate==0.27.1
|
| 3 |
+
peft==0.8.2
|
| 4 |
+
trl==0.7.10
|
| 5 |
+
datasets==2.17.0
|
| 6 |
+
transformers==4.38.0
|
| 7 |
+
bitsandbytes==0.42.0
|
| 8 |
+
accelerate==0.27.1
|
| 9 |
+
peft==0.8.2
|
| 10 |
+
trl==0.7.10
|
| 11 |
+
datasets==2.17.0
|
| 12 |
+
transformers==4.38.0
|