blip-vqa-space / app.py
ybelkada's picture
Update app.py
0debe65
import gradio as gr
import torch
from transformers import BlipForQuestionAnswering, BlipProcessor
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
model_vqa = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base").to(device)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def inference(raw_image, question, decoding_strategy):
inputs = processor(images=raw_image, text=question,return_tensors="pt")
if decoding_strategy == "Beam search":
inputs["max_length"] = 20
inputs["num_beams"] = 5
elif decoding_strategy == "Nucleus sampling":
inputs["max_length"] = 20
inputs["num_beams"] = 1
inputs["do_sample"] = True
inputs["top_k"] = 50
inputs["top_p"] = 0.95
out = model_vqa.generate(**inputs)
return processor.batch_decode(out, skip_special_tokens=True)[0]
inputs = [
gr.inputs.Image(type='pil'),
gr.inputs.Textbox(lines=2, label="Question"),
gr.inputs.Radio(choices=['Beam search','Nucleus sampling'], type="value", default="Nucleus sampling", label="Caption Decoding Strategy")
]
outputs = gr.outputs.Textbox(label="Output")
title = "BLIP"
description = "Gradio demo for BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation (Salesforce Research). To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2201.12086' target='_blank'>BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation</a> | <a href='https://github.com/salesforce/BLIP' target='_blank'>Github Repo</a></p>"
gr.Interface(inference, inputs, outputs, title=title, description=description, article=article).launch(enable_queue=True)