File size: 828 Bytes
129f9ee
0218651
 
 
129f9ee
0218651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b75b70a
0218651
 
 
b75b70a
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
import gradio as gr
import torch
from PIL import Image
from transformers import AutoModelForImageClassification, ViTImageProcessor

# Load model
model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
processor = ViTImageProcessor.from_pretrained('Falconsai/nsfw_image_detection')

# Function to make predictions
def classify_image(img):
    with torch.no_grad():
        inputs = processor(images=img, return_tensors="pt")
        outputs = model(**inputs)
        logits = outputs.logits

    predicted_label = logits.argmax(-1).item()
    return model.config.id2label[predicted_label]

# Gradio Interface
iface = gr.Interface(
    fn=classify_image,
    inputs="image",
    outputs="text",
    live=True  # Remove the 'interpretation' argument
)

# Launch the Gradio Interface
iface.launch()