Views
No views yet

Guard-Against-Unsafe-Content2-Siglip2 is an image classification vision-language encoder model fine-tuned from google/siglip2-base-patch16-224 for a binary classification task. It is designed to classify images as either "normal" or "nsfw" using the SiglipForImageClassification architecture.
[!WARNING] Experimental: This NSFW filter is an experimental model. Since I haven't found a better dataset to improve it, its performance may be inconsistent in some cases. I am currently looking for a better open dataset to enhance its effectiveness in a multi-label classification problem.
1Classification Report:
2 precision recall f1-score support
3
4 normal 0.9975 0.9988 0.9981 4000
5 nsfw 0.9992 0.9983 0.9987 6000
6
7 accuracy 0.9985 10000
8 macro avg 0.9983 0.9985 0.9984 10000
9weighted avg 0.9985 0.9985 0.9985 10000
!pip install -q transformers torch pillow gradio1import gradio as gr
2from transformers import AutoImageProcessor
3from transformers import SiglipForImageClassification
4from transformers.image_utils import load_image
5from PIL import Image
6import torch
7
8# Load model and processor
9model_name = "prithivMLmods/Guard-Against-Unsafe-Content2-Siglip2"
10model = SiglipForImageClassification.from_pretrained(model_name)
11processor = AutoImageProcessor.from_pretrained(model_name)
12
13def nsfw_classification(image):
14 """Predicts whether an image is NSFW or normal."""
15 image = Image.fromarray(image).convert("RGB")
16 inputs = processor(images=image, return_tensors="pt")
17
18 with torch.no_grad():
19 outputs = model(**inputs)
20 logits = outputs.logits
21 probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
22
23 labels = {
24 "0": "normal",
25 "1": "nsfw"
26 }
27 predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
28
29 return predictions
30
31# Create Gradio interface
32iface = gr.Interface(
33 fn=nsfw_classification,
34 inputs=gr.Image(type="numpy"),
35 outputs=gr.Label(label="Prediction Scores"),
36 title="NSFW Image Classification",
37 description="Upload an image to classify whether it is normal or NSFW."
38)
39
40# Launch the app
41if __name__ == "__main__":
42 iface.launch()