Visual_Q_and_A / app.py
Walid-Ahmed's picture
Update app.py
1de7de8 verified
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()