|
from typing import Any, Dict, List |
|
import torch |
|
import transformers |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] ==8 else torch.float16 |
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
self.tokenizer = AutoTokenizer.from_pretrained(path) |
|
self.model = AutoModelForCausalLM.from_pretrained(path, trust_remote_code=True, revision="main") |
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
self.model = self.model.to(self.device) |
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[str]: |
|
prompt = data["inputs"] |
|
input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device) |
|
generated_ids = self.model.generate(input_ids) |
|
return [self.tokenizer.decode(generated_ids[0], skip_special_tokens=True)] |
|
|
|
|
|
|