This is a
LoRA (Low-Rank Adaptation) adapter fine-tuned on top of
google/paligemma2-3b-pt-224 for
multi-label hateful content detection on paired
text + image data using the MMHS150K dataset.
Given an image and its associated text, the model outputs a JSON array containing zero or more labels from a fixed label set. The model is trained to classify hateful memes and social media content into multiple hate speech categories.
This model is intended for detecting and classifying hateful content in multimodal (text + image) social media posts, memes, and similar content. It can be used for:
1from transformers import AutoModelForImageTextToText, AutoProcessor
2from peft import PeftModel
3import torch
4
5# Model identifiers
6BASE_MODEL = "google/paligemma2-3b-pt-224"
7LORA_ADAPTER = "Amirhossein75/paligemma2-3b-mmhs150k-lora"
8
9# Load the base model
10base_model = AutoModelForImageTextToText.from_pretrained(
11 BASE_MODEL,
12 torch_dtype=torch.float16,
13 device_map="auto" # or "cpu" for CPU-only inference
14)
15
16# Load the LoRA adapter
17model = PeftModel.from_pretrained(base_model, LORA_ADAPTER)
18
19# Load the processor
20processor = AutoProcessor.from_pretrained(BASE_MODEL)
21
22print("✅ Model loaded successfully!")
1import torch
2from PIL import Image
3from transformers import AutoModelForImageTextToText, AutoProcessor
4from peft import PeftModel
5
6# Load base model and adapter
7BASE_MODEL = "google/paligemma2-3b-pt-224"
8LORA_ADAPTER = "Amirhossein75/paligemma2-3b-mmhs150k-lora"
9
10processor = AutoProcessor.from_pretrained(BASE_MODEL)
11base_model = AutoModelForImageTextToText.from_pretrained(
12 BASE_MODEL,
13 torch_dtype=torch.float16,
14 device_map="auto",
15)
16model = PeftModel.from_pretrained(base_model, LORA_ADAPTER)
17
18# Prepare input
19image = Image.open("path/to/image.jpg").convert("RGB")
20text = "Some text to analyze"
21
22# Create prompt
23class_names = ["racist", "sexist", "homophobe", "religion", "otherhate"]
24prompt = f"Classify the following text and image into zero or more of these labels: {class_names}. Return ONLY a JSON array of applicable labels. Text: {text}"
25
26# Generate
27inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
28outputs = model.generate(**inputs, max_new_tokens=64)
29result = processor.decode(outputs[0], skip_special_tokens=True)
30print(result) # e.g., ["racist", "sexist"]
1import json
2import re
3
4def parse_json_labels(response: str) -> list:
5 """Extract JSON array from model response with fallback."""
6 try:
7 # Try to find JSON array in response
8 match = re.search(r'\[.*?\]', response)
9 if match:
10 return json.loads(match.group())
11 except json.JSONDecodeError:
12 pass
13 return []
14
15def classify_batch(model, processor, images, texts, class_names):
16 """Classify a batch of image-text pairs."""
17 results = []
18 for image, text in zip(images, texts):
19 prompt = f"Classify the following text and image into zero or more of these labels: {class_names}. Return ONLY a JSON array of applicable labels. Text: {text}"
20 inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
21 outputs = model.generate(**inputs, max_new_tokens=64)
22 response = processor.decode(outputs[0], skip_special_tokens=True)
23 labels = parse_json_labels(response)
24 results.append(labels)
25 return results
1@misc{yousefi2024paligemma-hatespeech,
2 author = {Yousefi, Amirhossein},
3 title = {Multi-Modal Vision-Language Models for Hateful Content Classification},
4 year = {2024},
5 publisher = {GitHub},
6 howpublished = {\url{https://github.com/amirhossein-yousefi/text_image_multi_modal_vlm}},
7 note = {PaliGemma 2 LoRA adapter for MMHS150K hate speech detection}
8}
For more details on training, evaluation, and usage, see the
GitHub repository.