File size: 1,662 Bytes
edb3fc9
 
 
 
 
 
 
b38b7b0
edb3fc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import torch
from transformers import AutoTokenizer
from PIL import Image
from torchvision import transforms

# Load model and tokenizer
model = load_model(model_weights.pth)
model.eval()
text_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

# Image transform pipeline
image_transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

# Prediction function
def predict(image: Image.Image, text: str) -> str:
    # Process text input
    text_inputs = text_tokenizer(
        text,
        return_tensors="pt",
        padding="max_length",
        truncation=True,
        max_length=512
    )

    # Process image input
    image_input = image_transform(image).unsqueeze(0)  # Add batch dimension

    # Model inference
    with torch.no_grad():
        classification_output = model(
            pixel_values=image_input,
            input_ids=text_inputs["input_ids"],
            attention_mask=text_inputs["attention_mask"]
        )
        predicted_class = torch.sigmoid(classification_output).round().item()

    return "Biased" if predicted_class == 1 else "Unbiased"

# Gradio Interface
interface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Textbox(lines=2, placeholder="Enter text for classification...", label="Input Text")
    ],
    outputs=gr.Label(label="Prediction"),
    title="Multimodal Bias Classifier",
    description="Upload an image and provide a text to classify it as 'Biased' or 'Unbiased'."
)

interface.launch()