File size: 1,270 Bytes
e2782e7
544cd28
9b12d4a
 
 
e64ccec
9b12d4a
 
 
 
 
 
 
 
 
 
 
e64ccec
9b12d4a
143ba51
9b12d4a
 
 
 
 
 
 
 
 
 
 
 
515ab2f
 
9b12d4a
143ba51
9b12d4a
 
 
9a85bdd
 
 
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
import gradio as gr

# gr.load("models/ManishThota/InstructBlip-VQA").launch()

from PIL import Image
import torch
from transformers import BlipProcessor, BlipForQuestionAnswering

# Initialize the model and processor
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
model = BlipForQuestionAnswering.from_pretrained("ManishThota/InstructBlip-VQA")

def predict_answer(image, question):
    # Convert PIL image to RGB if not already
    image = image.convert("RGB")

    # Prepare inputs
    encoding = processor(image, question, return_tensors="pt")

    out = model.generate(**encoding, max_length=512)
    generated_text = processor.decode(out[0], skip_special_tokens=True)

    return generated_text


def gradio_predict(image, question):
    answer = predict_answer(image, question)
    return answer

# Define the Gradio interface
iface = gr.Interface(
    fn=gradio_predict,
    inputs=[gr.Image(type="pil", label="Upload or Drag an Image"), gr.Textbox(label="Question", placeholder="e.g. What is this?", scale=4)],
    outputs=gr.TextArea(label="Answer"),
    title="Visual Question Answering",
    description="Tiny 1B parameter Vision Language Model.",
)

# Launch the app
iface.queue().launch(debug=True)

# demo.queue().launch(debug=True)