realkun commited on
Commit
ae3e99f
·
verified ·
1 Parent(s): 6ab2abb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -1,22 +1,30 @@
1
- import gradio as gr
2
- from transformers import AutoProcessor, Pix2StructForConditionalGeneration
 
3
  from PIL import Image
4
 
5
- model = Pix2StructForConditionalGeneration.from_pretrained('google/deplot')
6
- processor = AutoProcessor.from_pretrained('google/deplot')
 
 
 
 
7
 
8
- def process_image(image):
9
- inputs = processor("Generate the underlying data table of the figure below:", image, return_tensors="pt")
10
- predictions = model.generate(**inputs, max_new_tokens=512)
11
- result = processor.decode(predictions[0], skip_special_tokens=True)
12
- return result
13
 
14
- demo = gr.Interface(
15
- fn=process_image,
16
- inputs=gr.Image(type="pil"),
17
- outputs="text",
18
- title="Chart to Text Converter",
19
- description="Upload a chart image to extract its data into table format"
20
- )
21
 
22
- demo.launch()
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, Pix2StructForConditionalGeneration, Pix2StructProcessor
2
+ import requests
3
+ import json
4
  from PIL import Image
5
 
6
+ model = Pix2StructForConditionalGeneration.from_pretrained("google/deplot")
7
+ processor = AutoProcessor.from_pretrained("google/deplot")
8
+ # processor = Pix2StructProcessor.from_pretrained('google/deplot')
9
+ # url = "https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/5090.png"
10
+ # image = Image.open(requests.get(url, stream=True).raw)
11
+ image = Image.open('222.png')
12
 
13
+ inputs = processor(images=image, text="Generate underlying data table of the figure below:", return_tensors="pt")
14
+ predictions = model.generate(**inputs, max_new_tokens=512)
15
+ print("prediction")
16
+ print(processor.decode(predictions[0], skip_special_tokens=True))
 
17
 
18
+ raw_output = processor.decode(predictions[0], skip_special_tokens=True)
19
+ split_by_newline = raw_output.split("<0x0A>")
20
+ result_array = []
 
 
 
 
21
 
22
+ for item in split_by_newline:
23
+     result_array.append([x.strip() for x in item.split("|")])
24
+
25
+ print("result:")
26
+ print(result_array)
27
+
28
+ with open('test.log', mode='w') as file:
29
+     for row in result_array:
30
+         file.write(" | ".join(row) + "\n")