Views
No views yet
1from unsloth import FastLanguageModel
2from peft import PeftModel
3import torch
4
5# Load the base model
6base_model_name = "unsloth/gemma-3-4b-it-unsloth-bnb-4bit",
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name=base_model_name,
9 max_seq_length=2048, # Must match fine-tuning
10 load_in_4bit=True,
11)
12
13# Load the fine-tuned LoRA adapter
14lora_model_name = "Machlovi/Gemma3-4"
15model = PeftModel.from_pretrained(model, lora_model_name)
16
17model.eval()1policy=You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.
2* "No Harassment": The input shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
3* "No Dangerous Content": The input shall not contain or seek generation of content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide, violent protest).
4* "No Hate Speech": The input shall not contain or seek generation of content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status.
5* "No Sexually Explicit Information": The input shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually arousal descriptions, content aimed at causing arousal, sexual assault (ex: rape),sexual harassment (physical, verbal, or visual)). Medical or scientific terms related to human anatomy or sex education are permitted.
6Does the human question violate the above principle? Your answer must start with 'Safe' or 'Unsafe'.
7- First line must read 'safe' or 'unsafe'.
8- If unsafe, a second line must include a comma-separated list of violated categories.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 = 10, use_cache = True)
4
5
6Hate speech, personal attacks, and discrimination