Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,30 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
model = Pix2StructForConditionalGeneration.from_pretrained(
|
| 6 |
-
processor = AutoProcessor.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return result
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|