Views
No views yet
transformers:1import torch
2from peft import PeftModel, PeftConfig
3from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
5# Load peft config for pre-trained checkpoint etc.
6peft_model_id = "rsonavane/flan-t5-xl-alpaca-dolly-lora-peft"
7config = PeftConfig.from_pretrained(peft_model_id)
8
9# load base LLM model and tokenizer
10model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, load_in_8bit=True, device_map={"":0})
11tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
12
13# Load the Lora model
14model = PeftModel.from_pretrained(model, peft_model_id, device_map={"":0})1def generate_prompt(instruction: str, input_ctxt: str = "") -> str:
2 if input_ctxt:
3 return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
4
5### Instruction:
6{instruction}
7
8### Input:
9{input_ctxt}
10
11### Response:"""
12 else:
13 return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
14
15### Instruction:
16{instruction}
17
18### Response:"""1
2input_ctxt = ""
3instruction = ""
4
5input_text = generate_prompt(instruction, input_ctxt)
6input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
7outputs = model.generate(input_ids)
8print(tokenizer.decode(outputs[0]))