Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "meta-llama/Llama-2-7b-hf",
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11
12# Load tokenizer
13tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
14tokenizer.pad_token = tokenizer.eos_token
15
16# Load LoRA adapter and apply to base model
17model = PeftModel.from_pretrained(base_model, "derain30/llama2-7b-backward-instruction")
18
19# Example: Generate an instruction from a response
20response = """Machine learning is a subfield of artificial intelligence that focuses on developing algorithms and models that enable computers to learn from data without being explicitly programmed. It identifies patterns in data and makes decisions with minimal human intervention."""
21
22prompt = f"### Output:\n{response}\n\n### Instruction:"
23inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
24outputs = model.generate(
25 inputs["input_ids"],
26 max_length=256,
27 temperature=0.7,
28 top_p=0.9,
29 num_return_sequences=1
30)
31
32generated_instruction = tokenizer.decode(outputs[0], skip_special_tokens=True).split("### Instruction:")[1].strip()
33print("Generated instruction:", generated_instruction)