Views
No views yet

Nsfw_Image_Detection_OSS is an image classification vision-language encoder model fine-tuned from facebook/metaclip-2-worldwide-s16 for a binary NSFW detection task. It is designed to classify whether an image is Safe For Work (SFW) or Not Safe For Work (NSFW) using the MetaClip2ForImageClassification architecture.
[!note] MetaCLIP 2: A Worldwide Scaling Recipe https://huggingface.co/papers/2507.22062
1Classification report:
2
3 precision recall f1-score support
4
5 SFW 0.8736 0.8673 0.8705 11103
6 NSFW 0.9047 0.9094 0.9071 15380
7
8 accuracy 0.8918 26483
9 macro avg 0.8892 0.8884 0.8888 26483
10weighted avg 0.8917 0.8918 0.8917 26483
1{
2 "id2label": {
3 "0": "SFW",
4 "1": "NSFW"
5 },
6 "label2id": {
7 "SFW": 0,
8 "NSFW": 1
9 }
10}!pip install -q transformers torch pillow gradio1import gradio as gr
2import torch
3from transformers import AutoImageProcessor, AutoModelForImageClassification
4from PIL import Image
5
6# Model name from Hugging Face Hub
7model_name = "prithivMLmods/Nsfw_Image_Detection_OSS"
8
9# Load processor and model
10processor = AutoImageProcessor.from_pretrained(model_name)
11model = AutoModelForImageClassification.from_pretrained(model_name)
12model.eval()
13
14# Define labels
15LABELS = {
16 0: "SFW",
17 1: "NSFW"
18}
19
20def nsfw_detection(image):
21 """Predict whether an image is SFW or NSFW."""
22 image = Image.fromarray(image).convert("RGB")
23 inputs = processor(images=image, return_tensors="pt")
24
25 with torch.no_grad():
26 outputs = model(**inputs)
27 logits = outputs.logits
28 probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
29
30 predictions = {LABELS[i]: round(probs[i], 3) for i in range(len(probs))}
31 return predictions
32
33# Build Gradio interface
34iface = gr.Interface(
35 fn=nsfw_detection,
36 inputs=gr.Image(type="numpy", label="Upload Image"),
37 outputs=gr.Label(label="NSFW Detection Probabilities"),
38 title="NSFW Image Detection (MetaCLIP-2)",
39 description="Upload an image to classify whether it is Safe For Work (SFW) or Not Safe For Work (NSFW)."
40)
41
42# Launch app
43if __name__ == "__main__":
44 iface.launch()