import torch from typing import Dict, List, Any from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig, TextGenerationPipeline from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig # check for GPU device = 0 if torch.cuda.is_available() else -1 class EndpointHandler(): def __init__(self, path=""): # load the optimized model model = AutoGPTQForCausalLM.from_quantized(path, use_safetensors=True) #file_name="model-quantized.onnx") tokenizer = AutoTokenizer.from_pretrained(path) # or you can also use pipeline self.pipeline = TextGenerationPipeline(model=model, tokenizer=tokenizer) def __call__(self, data: Any) -> List[List[Dict[str, float]]]: """ Args: data (:obj:): includes the input data and the parameters for the inference. Return: A :obj:`list`:. The list contains the embeddings of the inference inputs """ inputs = data.pop("inputs", data) parameters = data.pop("parameters", None) # pass inputs with all kwargs in data if parameters is not None: prediction = self.pipeline(inputs, **parameters) else: prediction = self.pipeline(inputs) return prediction