Spaces:
Running
Running
File size: 1,395 Bytes
b1779fd 28a20f8 b1779fd 099370e b1779fd |
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 |
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForImageClassification
import gradio as gr
# Load model and processor
from transformers import AutoModelForImageClassification, AutoProcessor
model = AutoModelForImageClassification.from_pretrained("model")
processor = AutoProcessor.from_pretrained("model")
model = AutoModelForImageClassification.from_pretrained(model_name)
processor = AutoProcessor.from_pretrained(model_name)
labels = model.config.id2label # e.g., {0: "non-hateful", 1: "hateful"}
def classify_meme(image: Image.Image):
inputs = processor(images=image, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
predictions = {labels[i]: float(probs[0][i]) for i in range(len(labels))}
return predictions
# Gradio interface
demo = gr.Interface(
fn=classify_meme,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=2),
title="Meme Sentiment Classifier (SigLIP2)",
description="Upload a meme to classify its sentiment using a SigLIP2-based model."
)
from transformers import AutoModelForImageClassification, AutoProcessor
model = AutoModelForImageClassification.from_pretrained("model")
processor = AutoProcessor.from_pretrained("model")
if __name__ == "__main__":
demo.launch()
|