Views
No views yet
tiiuae/falcon-7b-instruct using the QLoRA technique on the TruthfulQA dataset.1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3
4# Load the base model
5base_model_name = "tiiuae/falcon-7b-instruct"
6base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
7tokenizer = AutoTokenizer.from_pretrained(base_model_name)
8
9# Load the adapter and apply it to the base model
10adapter_repo_name = "MohammadOthman/falcon-7b-qlora-truthfulqa"
11model = PeftModel.from_pretrained(base_model, adapter_repo_name)
12
13# Move model to GPU if available
14device = "cuda" if torch.cuda.is_available() else "cpu"
15model.to(device)
16
17# Function to generate text
18def generate_text(prompt, max_length=100, num_return_sequences=1):
19 # Tokenize the input prompt
20 inputs = tokenizer(prompt, return_tensors="pt").to(device)
21
22 # Generate text
23 outputs = model.generate(
24 input_ids=inputs["input_ids"],
25 attention_mask=inputs["attention_mask"],
26 max_length=max_length,
27 num_return_sequences=num_return_sequences,
28 do_sample=True,
29 temperature=0.7
30 )
31
32 # Decode and print the output
33 for i, output in enumerate(outputs):
34 print(f"Generated Text {i+1}: {tokenizer.decode(output, skip_special_tokens=True)}")
35
36# Example usage
37prompt = "Once upon a time in a land far, far away"
38generate_text(prompt)