File size: 1,618 Bytes
f288596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c3dade
f288596
 
 
8c3dade
f288596
 
 
 
8c3dade
f288596
 
 
8c3dade
f288596
 
 
 
8c3dade
f288596
 
8c3dade
f288596
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from typing import Dict, Any

class EndpointHandler:
    def __init__(self, path=""):
        quantization_config = BitsAndBytesConfig(
            load_in_4bit=True,
            bnb_4bit_compute_dtype=torch.float16,
            bnb_4bit_quant_type="nf4",
        )
        self.tokenizer = AutoTokenizer.from_pretrained(path)
        self.model = AutoModelForCausalLM.from_pretrained(
            path,
            quantization_config=quantization_config,
            device_map="auto",
            torch_dtype=torch.float16,
        )

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        inputs = data.get("inputs", "")
        parameters = data.get("parameters", {})

        if isinstance(inputs, list):
            text = self.tokenizer.apply_chat_template(inputs, tokenize=False, add_generation_prompt=True)
        else:
            text = inputs

        encoded = self.tokenizer(text, return_tensors="pt").to(self.model.device)
        max_new_tokens = parameters.get("max_new_tokens", 512)
        temperature = parameters.get("temperature", 0.3)

        with torch.no_grad():
            outputs = self.model.generate(
                **encoded,
                max_new_tokens=max_new_tokens,
                temperature=max(temperature, 0.01),
                do_sample=temperature > 0,
            )

        new_tokens = outputs[0][encoded["input_ids"].shape[1]:]
        response = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
        return [{"generated_text": response}]