File size: 986 Bytes
37e1eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d842fb9
37e1eef
 
ed31e4d
d842fb9
cd18497
ed31e4d
 
37e1eef
 
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
import os
import gradio as gr
from transformers import pipeline

from transformers import AutoProcessor, BlipForQuestionAnswering

processor = AutoProcessor.from_pretrained(
    "Salesforce/blip-vqa-base")

model = BlipForQuestionAnswering.from_pretrained(
    "Salesforce/blip-vqa-base")

def launch(pil_image, question):
    inputs = processor(pil_image, question, return_tensors="pt")
    out = model.generate(**inputs)
    return processor.decode(out[0], skip_special_tokens=True)

iface = gr.Interface(fn=launch, 
                     inputs=[gr.Image(label="Input image", type='pil'), 
                             gr.Textbox(label="Question", lines=3)], 
                     outputs=[gr.Textbox(label="Answer", lines=3)],
                     title="Image Q&A with Salesforce BLIP",
                     description="1. Upload an image.\n2. Type a question.\n3. Press submit.\n4. Get an answer.",
                     allow_flagging="never"
                    )

iface.launch()