File size: 1,010 Bytes
c9c2872
 
 
 
6d34948
7e0c205
 
 
 
 
 
 
 
c83567b
7e0c205
 
 
 
 
 
 
 
 
 
 
1de7de8
c9c2872
 
 
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
import gradio as gr
import torch
from transformers import BlipForQuestionAnswering, AutoProcessor
from PIL import Image
import spaces

# Check device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load model and processor
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(device)
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")

@spaces.GPU 
def answer_question(image, question):
    inputs = processor(image, question, return_tensors="pt").to(device)
    out = model.generate(**inputs)
    return processor.decode(out[0], skip_special_tokens=True)

iface = gr.Interface(
    fn=answer_question,
    inputs=[gr.Image(type="pil"), gr.Textbox(placeholder="Enter your question")],
    outputs=gr.Textbox(label="Answer"),
    title="Visual Question Answering with BLIP",
    description="Upload an image and ask a question about its content.",
    examples=[["beach.jpeg", "Is there a man or a woman in the image?"]],
)

iface.launch()