fl399 eliolio commited on
Commit
1cdf007
0 Parent(s):

Duplicate from eliolio/chart-summary

Browse files

Co-authored-by: eliolio <eliolio@users.noreply.huggingface.co>

Files changed (5) hide show
  1. .DS_Store +0 -0
  2. .gitattributes +34 -0
  3. README.md +13 -0
  4. app.py +44 -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: Chart Summarization
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: eliolio/chart-summary
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-chart2text-statista"
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 summarize_chart(image):
23
+ inputs = processor(images=image, text="", 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
+ answer = gr.outputs.Textbox(label="Chart Summary")
30
+ examples = [["chart_example.png"], ["chart_example_2.png"], ["chart_example_3.png"], ["chart_example_4.png"]]
31
+
32
+ title = "Interactive demo: chart summarization"
33
+ description = "Gradio Demo for matcha model, fine-tuned on chart2text statista, a model that can summarize charts. To use it, simply upload your image and click 'submit', or click one of the examples to load them."
34
+
35
+ interface = gr.Interface(fn=summarize_chart,
36
+ inputs=[image],
37
+ outputs=answer,
38
+ examples=examples,
39
+ title=title,
40
+ description=description,
41
+ theme='gradio/soft',
42
+ enable_queue=True)
43
+
44
+ interface.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ torch
3
+ git+https://github.com/huggingface/transformers