Heykal Sayid commited on
Commit
316afbb
·
1 Parent(s): fbee0d9

delete cache

Browse files
Files changed (1) hide show
  1. functions.py +46 -0
functions.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Load model directly
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Model Info
6
+ # model_path = '/Users/heykalsayid/Desktop/skill-academy/projects/ai-porto/deployment/app/model/eleutherai-finetuned'
7
+ model_path_hf = 'paacamo/EleutherAI-pythia-1b-finetuned-nvidia-faq'
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(model_path_hf)
10
+ model = AutoModelForCausalLM.from_pretrained(model_path_hf)
11
+
12
+ def text_generation(text, model=model, tokenizer=tokenizer, max_input_token=300, max_output_token=100):
13
+ # Tokenize
14
+ tokenizer.truncation_side = 'left'
15
+ input_encoded = tokenizer(
16
+ text,
17
+ return_tensors='pt',
18
+ padding=True,
19
+ truncation=True,
20
+ max_length=max_input_token
21
+ )
22
+
23
+ # set attention mask to the output
24
+ input_ids = input_encoded['input_ids']
25
+ attention_mask = input_encoded['attention_mask']
26
+
27
+ # generate
28
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29
+ model.to(device)
30
+
31
+ output_ids = model.generate(
32
+ input_ids=input_ids.to(device),
33
+ attention_mask=attention_mask.to(device),
34
+ max_new_tokens=max_output_token,
35
+ pad_token_id=tokenizer.eos_token_id,
36
+ do_sample=True,
37
+ top_p=0.95,
38
+ temperature=0.7
39
+ )
40
+
41
+ # decode
42
+ generated_text_answer = tokenizer.decode(
43
+ output_ids[0][input_ids.shape[-1]:], skip_special_tokens=True
44
+ )
45
+ return generated_text_answer
46
+