File size: 922 Bytes
5677d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f0551a
5677d77
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)]