Spaces:
Runtime error
Runtime error
Msp Raja
commited on
Commit
β’
7be4744
1
Parent(s):
3dab771
fixes
Browse files- .DS_Store +0 -0
- app.py +69 -39
- gitattributes +27 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
app.py
CHANGED
@@ -1,54 +1,71 @@
|
|
1 |
import os
|
2 |
-
|
|
|
3 |
# workaround: install old version of pytorch since detectron2 hasn't released packages for pytorch 1.9 (issue: https://github.com/facebookresearch/detectron2/issues/3158)
|
4 |
-
os.system(
|
|
|
|
|
5 |
|
6 |
# install detectron2 that matches pytorch 1.8
|
7 |
# See https://detectron2.readthedocs.io/tutorials/install.html for instructions
|
8 |
-
os.system(
|
|
|
|
|
9 |
|
10 |
## install PyTesseract
|
11 |
-
os.system(
|
12 |
|
13 |
import gradio as gr
|
14 |
import numpy as np
|
15 |
-
from transformers import
|
16 |
from datasets import load_dataset
|
17 |
from PIL import Image, ImageDraw, ImageFont
|
18 |
|
19 |
-
processor =
|
20 |
-
model =
|
|
|
|
|
21 |
|
22 |
# load image example
|
23 |
dataset = load_dataset("nielsr/funsd", split="test")
|
24 |
image = Image.open(dataset[0]["image_path"]).convert("RGB")
|
25 |
image = Image.open("./invoice.png")
|
26 |
image.save("document.png")
|
27 |
-
|
28 |
-
labels = dataset.features[
|
29 |
id2label = {v: k for v, k in enumerate(labels)}
|
30 |
-
label2color = {
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def unnormalize_box(bbox, width, height):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
def iob_to_label(label):
|
41 |
label = label[2:]
|
42 |
if not label:
|
43 |
-
|
44 |
return label
|
45 |
|
|
|
46 |
def process_image(image):
|
47 |
width, height = image.size
|
48 |
|
49 |
# encode
|
50 |
-
encoding = processor(
|
51 |
-
|
|
|
|
|
52 |
|
53 |
# forward pass
|
54 |
outputs = model(**encoding)
|
@@ -58,9 +75,15 @@ def process_image(image):
|
|
58 |
token_boxes = encoding.bbox.squeeze().tolist()
|
59 |
|
60 |
# only keep non-subword predictions
|
61 |
-
is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
|
62 |
-
true_predictions = [
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# draw predictions over the image
|
66 |
draw = ImageDraw.Draw(image)
|
@@ -68,29 +91,36 @@ def process_image(image):
|
|
68 |
for prediction, box in zip(true_predictions, true_boxes):
|
69 |
predicted_label = iob_to_label(prediction).lower()
|
70 |
draw.rectangle(box, outline=label2color[predicted_label])
|
71 |
-
draw.text(
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
73 |
return image
|
74 |
|
75 |
|
76 |
-
title = "Interactive demo:
|
77 |
-
description = "Demo for Microsoft's
|
78 |
-
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/
|
79 |
-
examples =[[
|
80 |
|
81 |
css = ".output-image, .input-image {height: 40rem !important; width: 100% !important;}"
|
82 |
-
#css = "@media screen and (max-width: 600px) { .output_image, .input_image {height:20rem !important; width: 100% !important;} }"
|
83 |
# css = ".output_image, .input_image {height: 600px !important}"
|
84 |
|
85 |
css = ".image-preview {height: auto !important;}"
|
86 |
|
87 |
-
iface = gr.Interface(
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
1 |
import os
|
2 |
+
|
3 |
+
os.system("pip install pyyaml==5.1")
|
4 |
# workaround: install old version of pytorch since detectron2 hasn't released packages for pytorch 1.9 (issue: https://github.com/facebookresearch/detectron2/issues/3158)
|
5 |
+
os.system(
|
6 |
+
"pip install torch==1.8.0+cu101 torchvision==0.9.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html"
|
7 |
+
)
|
8 |
|
9 |
# install detectron2 that matches pytorch 1.8
|
10 |
# See https://detectron2.readthedocs.io/tutorials/install.html for instructions
|
11 |
+
os.system(
|
12 |
+
"pip install -q detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html"
|
13 |
+
)
|
14 |
|
15 |
## install PyTesseract
|
16 |
+
os.system("pip install -q pytesseract")
|
17 |
|
18 |
import gradio as gr
|
19 |
import numpy as np
|
20 |
+
from transformers import LayoutLMv3Processor, LayoutLMv3ForTokenClassification
|
21 |
from datasets import load_dataset
|
22 |
from PIL import Image, ImageDraw, ImageFont
|
23 |
|
24 |
+
processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
|
25 |
+
model = LayoutLMv3ForTokenClassification.from_pretrained(
|
26 |
+
"nielsr/layoutlmv3-finetuned-funsd"
|
27 |
+
)
|
28 |
|
29 |
# load image example
|
30 |
dataset = load_dataset("nielsr/funsd", split="test")
|
31 |
image = Image.open(dataset[0]["image_path"]).convert("RGB")
|
32 |
image = Image.open("./invoice.png")
|
33 |
image.save("document.png")
|
34 |
+
|
35 |
+
labels = dataset.features["ner_tags"].feature.names
|
36 |
id2label = {v: k for v, k in enumerate(labels)}
|
37 |
+
label2color = {
|
38 |
+
"question": "blue",
|
39 |
+
"answer": "green",
|
40 |
+
"header": "orange",
|
41 |
+
"other": "violet",
|
42 |
+
}
|
43 |
+
|
44 |
|
45 |
def unnormalize_box(bbox, width, height):
|
46 |
+
return [
|
47 |
+
width * (bbox[0] / 1000),
|
48 |
+
height * (bbox[1] / 1000),
|
49 |
+
width * (bbox[2] / 1000),
|
50 |
+
height * (bbox[3] / 1000),
|
51 |
+
]
|
52 |
+
|
53 |
|
54 |
def iob_to_label(label):
|
55 |
label = label[2:]
|
56 |
if not label:
|
57 |
+
return "other"
|
58 |
return label
|
59 |
|
60 |
+
|
61 |
def process_image(image):
|
62 |
width, height = image.size
|
63 |
|
64 |
# encode
|
65 |
+
encoding = processor(
|
66 |
+
image, truncation=True, return_offsets_mapping=True, return_tensors="pt"
|
67 |
+
)
|
68 |
+
offset_mapping = encoding.pop("offset_mapping")
|
69 |
|
70 |
# forward pass
|
71 |
outputs = model(**encoding)
|
|
|
75 |
token_boxes = encoding.bbox.squeeze().tolist()
|
76 |
|
77 |
# only keep non-subword predictions
|
78 |
+
is_subword = np.array(offset_mapping.squeeze().tolist())[:, 0] != 0
|
79 |
+
true_predictions = [
|
80 |
+
id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]
|
81 |
+
]
|
82 |
+
true_boxes = [
|
83 |
+
unnormalize_box(box, width, height)
|
84 |
+
for idx, box in enumerate(token_boxes)
|
85 |
+
if not is_subword[idx]
|
86 |
+
]
|
87 |
|
88 |
# draw predictions over the image
|
89 |
draw = ImageDraw.Draw(image)
|
|
|
91 |
for prediction, box in zip(true_predictions, true_boxes):
|
92 |
predicted_label = iob_to_label(prediction).lower()
|
93 |
draw.rectangle(box, outline=label2color[predicted_label])
|
94 |
+
draw.text(
|
95 |
+
(box[0] + 10, box[1] - 10),
|
96 |
+
text=predicted_label,
|
97 |
+
fill=label2color[predicted_label],
|
98 |
+
font=font,
|
99 |
+
)
|
100 |
+
|
101 |
return image
|
102 |
|
103 |
|
104 |
+
title = "Interactive demo: LayoutLMv3"
|
105 |
+
description = "Demo for Microsoft's LayoutLMv3, a Transformer for state-of-the-art document image understanding tasks. This particular model is fine-tuned on FUNSD, a dataset of manually annotated forms. It annotates the words appearing in the image as QUESTION/ANSWER/HEADER/OTHER. To use it, simply upload an image or use the example image below and click 'Submit'. Results will show up in a few seconds. If you want to make the output bigger, right-click on it and select 'Open image in new tab'."
|
106 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2204.08387' target='_blank'>LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking</a> | <a href='https://github.com/microsoft/unilm' target='_blank'>Github Repo</a></p>"
|
107 |
+
examples = [["document.png"]]
|
108 |
|
109 |
css = ".output-image, .input-image {height: 40rem !important; width: 100% !important;}"
|
110 |
+
# css = "@media screen and (max-width: 600px) { .output_image, .input_image {height:20rem !important; width: 100% !important;} }"
|
111 |
# css = ".output_image, .input_image {height: 600px !important}"
|
112 |
|
113 |
css = ".image-preview {height: auto !important;}"
|
114 |
|
115 |
+
iface = gr.Interface(
|
116 |
+
fn=process_image,
|
117 |
+
inputs=gr.inputs.Image(type="pil"),
|
118 |
+
outputs=gr.outputs.Image(type="pil", label="annotated image"),
|
119 |
+
title=title,
|
120 |
+
description=description,
|
121 |
+
article=article,
|
122 |
+
examples=examples,
|
123 |
+
css=css,
|
124 |
+
enable_queue=True,
|
125 |
+
)
|
126 |
+
iface.launch(debug=True)
|
gitattributes
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
19 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|