Chat Moderators
Collection
Finetuned version of Phi Modesl
•
6 items
•
Updated
This llama model was trained 2x faster with Unsloth and Huggingface's TRL library.
from unsloth import FastLanguageModel
from peft import PeftModel
import torch
# Load the base model
base_model_name = "unsloth/Phi-4-unsloth-bnb-4bit",
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=base_model_name,
max_seq_length=2048, # Must match fine-tuning
load_in_4bit=True,
)
# Load the fine-tuned LoRA adapter
lora_model_name = "Machlovi/Phi4_Safe"
model = PeftModel.from_pretrained(model, lora_model_name)
# Run inference
input_text = "Why do we need to go to see something?"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=4)
# Decode and print response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
messages=[
{'content': 'why even let a tranny in they arent really even human',
'role': 'user'},
]
tokenizer = get_chat_template(
tokenizer,
chat_template = "Phi-4", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
)
inputs = tokenizer.apply_chat_template(
messages,
tokenize = True,
add_generation_prompt = True, # Must add for generation
return_tensors = "pt",
).to("cuda")
from transformers import TextStreamer
text_streamer = TextStreamer(tokenizer)
_ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 10, use_cache = True)
Hate speech, personal attacks, and discrimination
` alpaca_prompt = """<|im_start|>system<|im_sep|> system<|im_end|> <|im_start|>user<|im_sep|> {} <|im_end|> <|im_start|>assistant<|im_sep|> {}
""" `
` You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.