Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForImageClassification
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load model and processor
|
| 7 |
+
model_name = "your-username/your-siglip2-meme-classifier" # Or local path
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 9 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
labels = model.config.id2label # e.g., {0: "non-hateful", 1: "hateful"}
|
| 12 |
+
|
| 13 |
+
def classify_meme(image: Image.Image):
|
| 14 |
+
inputs = processor(images=image, return_tensors="pt").to(model.device)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 18 |
+
predictions = {labels[i]: float(probs[0][i]) for i in range(len(labels))}
|
| 19 |
+
return predictions
|
| 20 |
+
|
| 21 |
+
# Gradio interface
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=classify_meme,
|
| 24 |
+
inputs=gr.Image(type="pil"),
|
| 25 |
+
outputs=gr.Label(num_top_classes=2),
|
| 26 |
+
title="Meme Sentiment Classifier (SigLIP2)",
|
| 27 |
+
description="Upload a meme to classify its sentiment using a SigLIP2-based model."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|