40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from transformers import ViltProcessor, ViltForQuestionAnswering
|
|
from PIL import Image
|
|
import gradio as gr
|
|
import torch
|
|
from gradio.themes.utils import sizes
|
|
|
|
theme = gr.themes.Default(radius_size=sizes.radius_none).set(
|
|
block_label_text_color = '#4D63FF',
|
|
block_title_text_color = '#4D63FF',
|
|
button_primary_text_color = '#4D63FF',
|
|
button_primary_background_fill='#FFFFFF',
|
|
button_primary_border_color='#4D63FF',
|
|
button_primary_background_fill_hover='#EDEFFF',
|
|
)
|
|
processor = ViltProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
|
model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
|
|
|
|
|
def vqa(image, question):
|
|
inp = Image.fromarray(image.astype('uint8'), 'RGB')
|
|
inputs = processor(inp, question, return_tensors="pt")
|
|
|
|
outputs = model(**inputs)
|
|
logits = outputs.logits
|
|
idx = logits.argmax(-1).item()
|
|
|
|
return model.config.id2label[idx]
|
|
|
|
|
|
demo = gr.Interface(fn=vqa,
|
|
inputs=['image', 'text'],
|
|
outputs='text',
|
|
title = "vqa",
|
|
theme=theme,
|
|
examples = [['soccer.jpg', 'how many people in the picture?']])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
demo.queue(concurrency_count=3).launch()
|