Views
No views yet
!pip install transformers
!pip install peft
!pip install torch
!pip install datasets
!pip install bitsandbytes1import transformers
2from peft import LoraConfig, get_peft_model
3import torch
4from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
5
6login() # Need access to the gated model.
7
8# Load LLAMA 2 model
9model_name = "meta-llama/Llama-2-7b-chat-hf"
10
11# Quantization configuration
12bnb_config = BitsAndBytesConfig(
13 load_in_4bit=True,
14 bnb_4bit_quant_type="nf4",
15 bnb_4bit_compute_dtype=torch.float16,
16)
17
18# Load model
19model = AutoModelForCausalLM.from_pretrained(
20 model_name,
21 quantization_config=bnb_config,
22 trust_remote_code=True
23)
24
25# Load LoRA configuration
26lora_config = LoraConfig.from_pretrained('harpyerr/archimedes-300s-7b-chat')
27model = get_peft_model(model, lora_config)
28
29# Load tokenizer
30tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
31tokenizer.pad_token = tokenizer.eos_token
32
33# Define prompt
34text = "Can you tell me who made Space-X?"
35prompt = "You are a helpful assistant. Please provide an informative response. \n\n" + text
36
37# Generate response
38device = "cuda:0"
39inputs = tokenizer(prompt, return_tensors="pt").to(device)
40outputs = model.generate(**inputs, max_new_tokens=100)
41print(tokenizer.decode(outputs[0], skip_special_tokens=True))