Views
No views yet
1import time
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM
4
5finetuned_model = AutoModelForCausalLM.from_pretrained("Mr-Vicky-01/GPT-QnA")
6tokenizer = AutoTokenizer.from_pretrained("Mr-Vicky-01/GPT-QnA")
7
8alpaca_prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
9
10### Instruction:
11what is depresssion how to overcome
12
13### Response:
14"""
15
16s = time.time()
17prompt = alpaca_prompt
18encodeds = tokenizer(prompt, return_tensors="pt",truncation=True).input_ids
19
20device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
21finetuned_model.to(device)
22inputs = encodeds.to(device)
23
24# Increase max_new_tokens if needed
25generated_ids = finetuned_model.generate(inputs, max_new_tokens=256, temperature=0.1, top_p=0.90, do_sample=False,pad_token_id=50259,eos_token_id=50259,num_return_sequences=1)
26print(tokenizer.decode(generated_ids[0]).split('### Response:')[1].split('<eos>')[0].strip())
27e = time.time()
28print(f'time taken:{e-s}')