|
import torch |
|
from typing import Dict, List, Any |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
bnb_config = BitsAndBytesConfig( |
|
load_in_4bit=True, |
|
bnb_4bit_use_double_quant=True, |
|
bnb_4bit_quant_type="nf4", |
|
bnb_4bit_compute_dtype=torch.bfloat16 |
|
) |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(path) |
|
model = AutoModelForCausalLM.from_pretrained(path, device_map="auto", quantization_config=bnb_config) |
|
|
|
self.pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, torch_dtype=torch.bfloat16) |
|
|
|
def __call__(self, data: Any): |
|
inputs = data.pop("inputs", data) |
|
prediction = self.pipeline(inputs) |
|
|
|
|
|
return prediction |
|
|