Views
No views yet
1
2from unsloth import FastLanguageModel
3from peft import PeftModel
4import torch
5
6# Load the base model
7base_model_name = "unsloth/Phi-4-unsloth-bnb-4bit",
8model, tokenizer = FastLanguageModel.from_pretrained(
9 model_name=base_model_name,
10 max_seq_length=2048, # Must match fine-tuning
11 load_in_4bit=True,
12)
13
14# Load the fine-tuned LoRA adapter
15lora_model_name = "Machlovi/Phi4_Safe"
16model = PeftModel.from_pretrained(model, lora_model_name)
17
18# Run inference
19input_text = "Why do we need to go to see something?"
20inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
21
22with torch.no_grad():
23 outputs = model.generate(**inputs, max_new_tokens=4)
24
25# Decode and print response
26response = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
28
291FastLanguageModel.for_inference(model) # Enable native 2x faster inference
2
3messages=[
4 {'content': 'why even let a tranny in they arent really even human',
5 'role': 'user'},
6]
7
8tokenizer = get_chat_template(
9 tokenizer,
10 chat_template = "Phi-4", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
11)
12
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")
21
22from transformers import TextStreamer
23text_streamer = TextStreamer(tokenizer)
24_ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 10, use_cache = True)
25
26Hate speech, personal attacks, and discrimination1@InProceedings{10.1007/978-3-032-13184-3_24,
2author="Machlovi, Naseem
3and Saleki, Maryam
4and Ababio, Innocent
5and Amin, Ruhul",
6editor="Degen, Helmut
7and Ntoa, Stavroula",
8title="Towards Safer AI Moderation: Evaluating LLM Moderators Through a Unified Benchmark Dataset and Advocating a Human-First Approach",
9booktitle="HCI International 2025 -- Late Breaking Papers",
10year="2026",
11publisher="Springer Nature Switzerland",
12address="Cham",
13pages="386--403",
14abstract="As AI systems become more integrated into daily life, the need for safer and more reliable moderation has never been greater. Large Language Models (LLMs) have demonstrated remarkable capabilities, surpassing earlier models in complexity and performance. Their evaluation across diverse tasks has consistently showcased their potential, enabling the development of adaptive and personalized agents. However, despite these advancements, LLMs remain prone to errors, particularly in areas requiring nuanced moral reasoning. They struggle with detecting implicit hate, offensive language, and gender biases due to the subjective and context-dependent nature of these issues. Moreover, their reliance on training data can inadvertently reinforce societal biases, leading to inconsistencies and ethical concerns in their outputs. To explore the limitations of LLMs in this role, we developed an experimental framework based on state-of-the-art (SOTA) models to assess human emotions and offensive behaviors. The framework introduces a unified benchmark dataset encompassing 49 distinct categories spanning the wide spectrum of human emotions, offensive and hateful text, and gender and racial biases. Furthermore, we introduced SafePhi, a QLoRA fine-tuned version of Phi-4, adapting diverse ethical contexts and outperforming benchmark moderators by achieving a Macro F1 score of 0.89, where OpenAI Moderator and Llama Guard score 0.77 and 0.74, respectively. This research also highlights the critical domains where LLM moderators consistently underperformed, pressing the need to incorporate more heterogeneous and representative data with human-in-the-loop, for better model robustness and explainability.",
15isbn="978-3-032-13184-3"
16}
17