philschmid's picture
philschmid HF staff
added pipeline
17e379b
from typing import Dict, List, Any
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
class PreTrainedPipeline:
def __init__(self, path=""):
# load the optimized model
self.model = AutoModelForCausalLM.from_pretrained(
path, torch_dtype=torch.float16, device_map="auto", load_in_8bit=True
)
self.tokenizer = AutoTokenizer.from_pretrained(path)
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.get("inputs", data)
parameters = data.get("parameters", {})
# tokenize the input
input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids.to(self.model.device)
# run the model
logits = self.model.generate(input_ids, **parameters)
# Perform pooling
# postprocess the prediction
return {"generated_text": self.tokenizer.decode(logits[0].tolist())}