File size: 1,355 Bytes
7302c2d
 
 
 
 
 
 
 
81c1119
7302c2d
 
 
 
 
 
 
3d6aa6f
 
7302c2d
 
 
3d6aa6f
abc55db
7302c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import transformers
import quant
from typing import Dict, Any
from gptq import GPTQ
from utils import find_layers, DEV
from transformers import AutoTokenizer, LlamaConfig, LlamaForCausalLM
import os
import pickle

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

class EndpointHandler:
    def __init__(self, path=""):
        model_bin_path = os.path.join(path, "model.bin")
        model_folder_path = model_name = os.path.join(path, "Wizard-Vicuna-13B-Uncensored-GPTQ")
        
        with open(model_bin_path, "rb") as f: # "rb" because we want to read in binary mode
            self.model = pickle.load(f)

        self.tokenizer = AutoTokenizer.from_pretrained(model_folder_path, use_fast=False)
        self.model.to(DEV)

        

    def __call__(self, data: Any) -> Dict[str, str]:
        input_text = data.pop("inputs", data)
        input_ids = self.tokenizer.encode(input_text, return_tensors="pt").to(DEV)

        with torch.no_grad():
            generated_ids = self.model.generate(
                input_ids,
                do_sample=True,
                min_length=50,
                max_length=200,
                top_p=0.95,
                temperature=0.8,
            )
        generated_text = self.tokenizer.decode([el.item() for el in generated_ids[0]])

        return {'generated_text': generated_text}