Spaces:
Running
Running
import gradio as gr | |
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor | |
import requests | |
from PIL import Image | |
import torch | |
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/20294671002019.png', 'chart_example.png') | |
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/multi_col_1081.png', 'chart_example_2.png') | |
torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/18143564004789.png', 'chart_example_3.png') | |
torch.hub.download_url_to_file('https://sharkcoder.com/files/article/matplotlib-bar-plot.png', 'chart_example_4.png') | |
model_name = "google/matcha-chartqa" | |
model = Pix2StructForConditionalGeneration.from_pretrained(model_name) | |
processor = Pix2StructProcessor.from_pretrained(model_name) | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model.to(device) | |
def filter_output(output): | |
return output.replace("<0x0A>", "") | |
def chart_qa(image, question): | |
inputs = processor(images=image, text=question, return_tensors="pt").to(device) | |
predictions = model.generate(**inputs, max_new_tokens=512) | |
return filter_output(processor.decode(predictions[0], skip_special_tokens=True)) | |
image = gr.inputs.Image(type="pil", label="Chart") | |
question = gr.inputs.Textbox(label="Question") | |
answer = gr.outputs.Textbox(label="Model Output") | |
examples = [["chart_example.png", "Which country has the second highest death rate?"], | |
["chart_example_2.png", "What is the highest number of B2C sales ever achived?"], | |
["chart_example_3.png", "Which country has the lowest CPA received across all times?"],] # ["chart_example_4.png"]] | |
title = "Interactive demo: Chart QA with MatCha" | |
description = "Gradio Demo for the [matcha](https://arxiv.org/abs/2212.09662) model, fine-tuned on the [ChartQA](https://paperswithcode.com/dataset/chartqa) dataset. To use it, simply upload your image and click 'submit', or click one of the examples to load them." | |
interface = gr.Interface(fn=chart_qa, | |
inputs=[image, question], | |
outputs=answer, | |
examples=examples, | |
title=title, | |
description=description, | |
theme='gradio/soft', | |
enable_queue=True) | |
interface.launch(debug=True) |