Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
from PIL import Image | |
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor | |
import spaces | |
def infer_infographics(image, question): | |
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-ai2d-base") | |
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-ai2d-base") | |
inputs = processor(images=image, text=question, return_tensors="pt") | |
predictions = model.generate(**inputs) | |
return processor.decode(predictions[0], skip_special_tokens=True) | |
def infer_ui(image, question): | |
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-screen2words-base") | |
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-screen2words-base") | |
inputs = processor(images=image,text=question, return_tensors="pt") | |
predictions = model.generate(**inputs) | |
return processor.decode(predictions[0], skip_special_tokens=True) | |
def infer_chart(image, question): | |
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-chartqa-base") | |
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-chartqa-base") | |
inputs = processor(images=image, text=question, return_tensors="pt") | |
predictions = model.generate(**inputs) | |
return processor.decode(predictions[0], skip_special_tokens=True) | |
def infer_doc(image, question): | |
model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-docvqa-base") | |
processor = Pix2StructProcessor.from_pretrained("google/pix2struct-docvqa-base") | |
inputs = processor(images=image, text=question, return_tensors="pt") | |
predictions = model.generate(**inputs) | |
return processor.decode(predictions[0], skip_special_tokens=True) | |
css = """ | |
#mkd { | |
height: 500px; | |
overflow: auto; | |
border: 1px solid #ccc; | |
} | |
""" | |
with gr.Blocks(css=css) as demo: | |
gr.HTML("<h1><center>Pix2Struct π<center><h1>") | |
gr.HTML("<h3><center>Pix2Struct is a powerful backbone for visual question answering. β‘</h3>") | |
gr.HTML("<h3><center>This app has base version of the model. For better performance, use large checkpoints.<h3>") | |
with gr.Row(): | |
with gr.Column(): | |
input_img = gr.Image(label="Input Document") | |
question = gr.Text(label="Question") | |
submit_btn = gr.Button(value="Submit") | |
output = gr.Text(label="Answer") | |
gr.Examples( | |
[["docvqa_example.png", "How many items are sold?"]], | |
inputs = [input_img, question], | |
outputs = [output], | |
fn=infer_doc, | |
cache_examples=True, | |
label='Click on any Examples below to get Document Question Answering results quickly π' | |
) | |
submit_btn.click(infer_doc, [input_img, question], [output]) | |
demo.launch(debug=True) |