Views
No views yet
1from peft import PeftModel
2import torch
3
4from unsloth import FastModel
5
6
7# Load the base model
8base_model_name = "unsloth/gemma-3-12b-it-unsloth-bnb-4bit",",
9model, tokenizer = FastModel.from_pretrained(
10 model_name=base_model_name,
11 max_seq_length=2048, # Must match fine-tuning
12 load_in_4bit=True,
13)
14
15# Load the fine-tuned LoRA adapter
16lora_model_name = "Machlovi/GGuard"
17model = PeftModel.from_pretrained(model, lora_model_name)
18
19model.eval()1policy = """You are a safety moderation assistant. Check if there is unsafe content in the user message according to the following categories:
2
3<BEGIN UNSAFE CONTENT CATEGORIES>
4S1: Violence
5S2: Sexual.
6S3: Criminal Planning/Confessions.
7S4: Guns and Illegal Weapons.
8S5: Controlled/Regulated Substances.
9S6: Suicide and Self Harm.
10S7: Sexual (minor).
11S8: Hate/Identity Hate.
12S9: PII/Privacy.
13S10: Harassment.
14S11: Threat.
15S12: Profanity.
16S13: Needs Caution.
17S14: Immoral Medical.
18S15: Manipulation.
19S16: Fraud/Deception.
20S17: Malware.
21S18: High Risk Gov Decision Making.
22S19: Political/Misinformation/Conspiracy.
23S20: Copyright/Trademark/Plagiarism.
24S21: Unauthorized Advice.
25S22: Illegal Activity.
26S23: Immoral/Unethical
27S24: Other.
28<END UNSAFE CONTENT CATEGORIES>
29Respond with only this JSON format:
30
31Provide your safety assessment for the above conversation in the below output JSON format:
32"User Safety": safe or unsafe rating for the user message,
33"Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present.
34"Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. Omit if all safe.
35
36Do not include anything other than the output JSON in your response.
37Output JSON:
38
39"""1from unsloth.chat_templates import get_chat_template
2
3chat_template='gemma-3'
4tokenizer = get_chat_template(
5 tokenizer,
6 chat_template = chat_template,
7)
8
9messages=[{"content":policy,
10 'role':"system"},
11 {'content':'why even let a tranny in they arent really even human',
12 'role': 'user'},
13
14]
15inputs = tokenizer.apply_chat_template(
16 messages,
17 tokenize = True,
18 add_generation_prompt = True, # Must add for generation
19 return_tensors = "pt",
20).to("cuda")
211from transformers import TextStreamer
2text_streamer = TextStreamer(tokenizer)
3_= model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 50, use_cache = True, temperature = 0.2, top_p = 0.95, top_k = 64,)
4
5
6Hate speech, personal attacks, and discrimination