File size: 1,705 Bytes
57009b3
03eeab0
 
57009b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03eeab0
 
57009b3
 
 
80ec456
5e4cc00
 
 
 
57009b3
 
 
 
5e4cc00
57009b3
 
 
 
ff3c462
 
57009b3
 
5e4cc00
 
 
 
57009b3
 
 
 
 
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
45
46
47
48
49
50
51
52
53
from typing import Dict, List, Any
from transformers import AutoTokenizer
from transformers import AutoModelForCausalLM, BitsAndBytesConfig


alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""

 
class EndpointHandler:
    def __init__(self, path=""):
        # load model and processor from path

        self.model = AutoModelForCausalLM.from_pretrained(path, load_in_4bit=True)
        self.tokenizer = AutoTokenizer.from_pretrained(path)

 
    def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
        sentence = data.pop("inputs",data).lower()
        instruction_prompt = data.pop('prompt', data)
        max_new_tokens = data.pop('max_new_tokens', data)
        top_p = data.pop('top_p', data)
        temperature = data.pop('temperature', data)

        inputs = self.tokenizer(
            [
                alpaca_prompt.format(
                    instruction_prompt,  # instruction
                    sentence,  # input
                    "",  # output - leave this blank for generation!
                )
            ], return_tensors="pt")

        inputs = inputs.to('cuda')
        
        outputs = self.model.generate(**inputs,
                    max_new_tokens=max_new_tokens,
                    # use_cache=True,
                    top_p=top_p,
                    temperature=temperature)
        
        outputs = self.tokenizer.batch_decode(outputs)[0]
        response = outputs.split("### Response:")[1].split("<|end_of_text|>")[0]
 
        return [{"generated_text": response}]