File size: 895 Bytes
44d180e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c23fe
44d180e
b9e79ed
44d180e
18784a2
44d180e
 
 
 
 
 
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
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers
import torch


class Model():
    def __init__(self, model="tiiuae/falcon-7b-instruct") -> None:
        pass
        self.tokenizer = AutoTokenizer.from_pretrained(model)
        self.pipeline = transformers.pipeline(
            "text-generation",
            model=model,
            tokenizer=self.tokenizer,
            torch_dtype=torch.bfloat16,
            trust_remote_code=True,
            device_map="auto",
        )

    def gen(self, content, temp=0.0, max_length=500):
        sequences = self.pipeline(
            content,
            max_length=max_length,
            do_sample=False,
            temperature=temp,
            num_return_sequences=1,
            eos_token_id=self.tokenizer.eos_token_id,
        )
        
        return '\n'.join([seq['generated_text'] for seq in sequences])