Spaces:
Sleeping
Sleeping
import torch | |
from PIL import Image | |
from transformers import AutoProcessor, AutoModelForImageClassification | |
import gradio as gr | |
import pytesseract | |
def classify_meme(image: Image.Image): | |
# OCR: extract text from image | |
extracted_text = pytesseract.image_to_string(image) | |
# Process image with SigLIP2 model | |
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": predictions, | |
"Extracted Text": extracted_text.strip() | |
} | |
# Load model and processor from Hugging Face | |
model = AutoModelForImageClassification.from_pretrained("google/siglip2-base-patch16-naflex") | |
processor = AutoProcessor.from_pretrained("google/siglip2-base-patch16-naflex") | |
labels = model.config.id2label | |
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, label="Predictions"), | |
gr.Textbox(label="Extracted Text") | |
], | |
title="Meme Classifier with OCR", | |
description="Upload a meme to classify its sentiment and extract text using OCR." | |
) | |
if __name__ == "__main__": | |
demo.launch(share = True) | |