eisenjulian fl399 commited on
Commit
72d3114
0 Parent(s):

Duplicate from fl399/matcha_chartqa

Browse files

Co-authored-by: Fangyu Liu <[email protected]>

Files changed (5) hide show
  1. .DS_Store +0 -0
  2. .gitattributes +34 -0
  3. README.md +13 -0
  4. app.py +48 -0
  5. requirements.txt +3 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: MatCha ChartQA
3
+ emoji: 📊
4
+ colorFrom: red
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.24.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: fl399/matcha_chartqa
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
3
+ import requests
4
+ from PIL import Image
5
+ import torch
6
+
7
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/20294671002019.png', 'chart_example.png')
8
+ 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')
9
+ torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/18143564004789.png', 'chart_example_3.png')
10
+ torch.hub.download_url_to_file('https://sharkcoder.com/files/article/matplotlib-bar-plot.png', 'chart_example_4.png')
11
+
12
+
13
+ model_name = "google/matcha-chartqa"
14
+ model = Pix2StructForConditionalGeneration.from_pretrained(model_name)
15
+ processor = Pix2StructProcessor.from_pretrained(model_name)
16
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
+ model.to(device)
18
+
19
+ def filter_output(output):
20
+ return output.replace("<0x0A>", "")
21
+
22
+ def chart_qa(image, question):
23
+ inputs = processor(images=image, text=question, return_tensors="pt").to(device)
24
+ predictions = model.generate(**inputs, max_new_tokens=512)
25
+ return filter_output(processor.decode(predictions[0], skip_special_tokens=True))
26
+
27
+
28
+ image = gr.inputs.Image(type="pil", label="Chart")
29
+ question = gr.inputs.Textbox(label="Question")
30
+ answer = gr.outputs.Textbox(label="Model Output")
31
+ examples = [["chart_example.png", "Which country has the second highest death rate?"],
32
+ ["chart_example_2.png", "What is the B2B sales in 2017?"],
33
+ ["chart_example_3.png", "Which country has the lowest CPA received across all times?"],
34
+ ["chart_example_4.png", "How much revenue did Furious 7 make?"]]
35
+
36
+ title = "Interactive demo: Chart QA with MatCha🍵"
37
+ 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. \n Quick links: [[paper]](https://arxiv.org/abs/2212.09662) [[google-ai blog]](https://ai.googleblog.com/2023/05/foundation-models-for-reasoning-on.html) [[code]](https://github.com/google-research/google-research/tree/master/deplot)"
38
+
39
+ interface = gr.Interface(fn=chart_qa,
40
+ inputs=[image, question],
41
+ outputs=answer,
42
+ examples=examples,
43
+ title=title,
44
+ description=description,
45
+ theme='gradio/soft',
46
+ enable_queue=True)
47
+
48
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.26.0
2
+ torch
3
+ git+https://github.com/huggingface/transformers