Spaces:
Running
Running
File size: 1,686 Bytes
ddd8598 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import sys
import traceback
from pathlib import Path
from PIL import Image
from transformers import pipeline
# Setup models exactly as in main.py
print("Loading models...")
models = {}
device = -1
models['nsfw_image'] = pipeline(
"image-classification",
model="Falconsai/nsfw_image_detection",
device=device
)
models['nsfw_image_robust'] = pipeline(
"image-classification",
model="AdamCodd/vit-base-nsfw-detector",
device=device
)
file_path = Path("uploads/1779226485_Screenshot 2026-05-19 000650.png")
try:
print("Running moderation...")
scores = []
# 1. Falconsai
if 'nsfw_image' in models:
try:
image = Image.open(file_path)
res = models['nsfw_image'](image)
raw_score = float(res[0]['score'])
label = res[0]['label']
nsfw_score = raw_score if label != "normal" else (1.0 - raw_score)
scores.append(nsfw_score)
print("Falconsai score:", nsfw_score)
except Exception as e:
print("Falconsai failed:")
traceback.print_exc()
# 2. AdamCodd
if 'nsfw_image_robust' in models:
try:
image = Image.open(file_path)
res = models['nsfw_image_robust'](image)
raw_score = float(res[0]['score'])
label = res[0]['label']
nsfw_score = raw_score if label == "nsfw" else (1.0 - raw_score)
scores.append(nsfw_score)
print("AdamCodd score:", nsfw_score)
except Exception as e:
print("AdamCodd failed:")
traceback.print_exc()
except Exception as e:
print("Error:", e)
traceback.print_exc()
|