Views
No views yet
unsloth/Llama-3.2-1B-InstructSFTTrainer (supervised fine tuning)pip install transformers accelerate peft torch huggingface_hub1from huggingface_hub import notebook_login
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM
4from peft import PeftModel
5
6# notebook_login() # Required only if model is private
7
8# Base Llama 3.2 model
9base_model = "unsloth/Llama-3.2-1B-Instruct"
10
11# Your fine tuned LoRA adapter
12ft_model = "arif-butt/llmshield-1b-finetuned"
13
14# Load tokenizer
15tokenizer = AutoTokenizer.from_pretrained(base_model)
16
17# Load base model
18base_model = AutoModelForCausalLM.from_pretrained(
19 base_model,
20 torch_dtype=torch.float16,
21 device_map="auto"
22)
23
24# Apply LoRA adapter
25model = PeftModel.from_pretrained(base_model, ft_model)
26
27# Evaluation mode
28model.eval()1def chat(user_message, max_new_tokens=80):
2
3 # Construct structured messages required by Llama chat template
4 messages = [
5 {
6 "role": "system",
7 "content": (
8 "Answer the user's question directly and concisely. "
9 "Do NOT ask new questions or create a Q&A list."
10 )
11 },
12 {"role": "user", "content": user_message},
13 ]
14
15 # Convert messages → tokens using built-in template
16 inputs = tokenizer.apply_chat_template(
17 messages,
18 add_generation_prompt=True,
19 return_tensors="pt",
20 ).to(model.device)
21
22 # Generate continuation
23 outputs = model.generate(
24 inputs,
25 max_new_tokens=max_new_tokens,
26 temperature=0.7,
27 top_p=0.9,
28 do_sample=True,
29 )
30
31 # Remove prompt → decode assistant reply
32 generated_tokens = outputs[0, inputs.shape[-1]:]
33 answer = tokenizer.decode(generated_tokens,
34 skip_special_tokens=True).strip()
35
36 return answerprint(chat("What is LLMShield?"))LLMShield is a fine-tuned Llama 3.2 1B instruction model designed
to answer questions concisely and follow user instructions.adapter_model.safetensors or pytorch_lora_weights.bintokenizer.jsonconfig.jsonspecial_tokens_map.jsonMIT LicenseApache-2.0LLMShield: Fine-tuned Llama 3.2 1B model
Author: Dr. Muhammad Arif Butt, Um E Abeeha, Khalood Sami, and Alisha Shahid.
HuggingFace: arif-butt/llmshield-1b-finetuned