Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from PIL import Image
|
5 |
+
from torchvision import transforms
|
6 |
+
|
7 |
+
# Load model and tokenizer
|
8 |
+
model = load_model()
|
9 |
+
model.eval()
|
10 |
+
text_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
11 |
+
|
12 |
+
# Image transform pipeline
|
13 |
+
image_transform = transforms.Compose([
|
14 |
+
transforms.Resize((224, 224)),
|
15 |
+
transforms.ToTensor(),
|
16 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
17 |
+
])
|
18 |
+
|
19 |
+
# Prediction function
|
20 |
+
def predict(image: Image.Image, text: str) -> str:
|
21 |
+
# Process text input
|
22 |
+
text_inputs = text_tokenizer(
|
23 |
+
text,
|
24 |
+
return_tensors="pt",
|
25 |
+
padding="max_length",
|
26 |
+
truncation=True,
|
27 |
+
max_length=512
|
28 |
+
)
|
29 |
+
|
30 |
+
# Process image input
|
31 |
+
image_input = image_transform(image).unsqueeze(0) # Add batch dimension
|
32 |
+
|
33 |
+
# Model inference
|
34 |
+
with torch.no_grad():
|
35 |
+
classification_output = model(
|
36 |
+
pixel_values=image_input,
|
37 |
+
input_ids=text_inputs["input_ids"],
|
38 |
+
attention_mask=text_inputs["attention_mask"]
|
39 |
+
)
|
40 |
+
predicted_class = torch.sigmoid(classification_output).round().item()
|
41 |
+
|
42 |
+
return "Biased" if predicted_class == 1 else "Unbiased"
|
43 |
+
|
44 |
+
# Gradio Interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=predict,
|
47 |
+
inputs=[
|
48 |
+
gr.Image(type="pil", label="Upload Image"),
|
49 |
+
gr.Textbox(lines=2, placeholder="Enter text for classification...", label="Input Text")
|
50 |
+
],
|
51 |
+
outputs=gr.Label(label="Prediction"),
|
52 |
+
title="Multimodal Bias Classifier",
|
53 |
+
description="Upload an image and provide a text to classify it as 'Biased' or 'Unbiased'."
|
54 |
+
)
|
55 |
+
|
56 |
+
interface.launch()
|