File size: 1,095 Bytes
fb2b996
 
 
 
 
 
 
 
 
 
 
 
 
05c8b63
fb2b996
 
 
 
 
 
 
 
 
 
 
 
 
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
import time

from transformers import AutoModelForCausalLM, AutoTokenizer

class HFModel:
    def __init__(self, model_name):
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModelForCausalLM.from_pretrained(model_name)

    def __call__(self, request):
        prompt = request.get("prompt")
        input_ids = self.tokenizer.encode(prompt, return_tensors='pt')
        choices = self.model.generate(input_ids, max_length=50, do_sample=True)
        choices = self.tokenizer.decode(choices, skip_special_tokens=True)
        completion = {
            'id': None,  # fill in
            'model': 'codegen',
            'object': 'text_completion',
            'created': int(time.time()),
            'choices': None,  # fill in
            'usage': {
                'completion_tokens': int(sum([len(c.split()) for c in choices])),
                'prompt_tokens': int(len(prompt.split())),
                'total_tokens': int(sum([len(c.split()) for c in choices]) + len(prompt.split())),
            }
        }
        return completion, choices