mp-02 commited on
Commit
3fda4ee
1 Parent(s): dc6e69c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -44
app.py CHANGED
@@ -1,44 +1,45 @@
1
- from cord_inference import prediction as cord_prediction
2
- from sroie_inference import prediction as sroie_prediction
3
- import gradio as gr
4
- import json
5
-
6
- def prediction(image):
7
-
8
- #we first use mp-02/layoutlmv3-finetuned-cord on the image, which gives us a JSON with some info and a blurred image
9
- d, image_blurred = sroie_prediction(image)
10
-
11
- #then we use the model fine-tuned on sroie (for now it is Theivaprakasham/layoutlmv3-finetuned-sroie)
12
- d1, image1 = cord_prediction(image_blurred)
13
-
14
- #we then link the two json files
15
- if len(d) == 0:
16
- k = d1
17
- else:
18
- k = json.dumps(d).split('}')[0] + ', ' + json.dumps(d1).split('{')[1]
19
-
20
- return d, image_blurred, d1, image1, k
21
-
22
-
23
- title = "Interactive demo: LayoutLMv3 for receipts"
24
- description = "Demo for Microsoft's LayoutLMv3, a Transformer for state-of-the-art document image understanding tasks. This particular model is fine-tuned on CORD and SROIE, which are datasets of receipts.\n It firsts uses the fine-tune on SROIE to extract date, company and address, then the fine-tune on CORD for the other info.\n To use it, simply upload an image or use the example image below. Results will show up in a few seconds."
25
- examples = [['image.png']]
26
-
27
- css = """.output_image, .input_image {height: 600px !important}"""
28
-
29
- # we use a gradio interface that takes in input an image and return a JSON file that contains its info
30
- # we show also the intermediate steps (first we take some info with the model fine-tuned on SROIE and we blur the relative boxes
31
- # then we pass the image to the model fine-tuned on CORD
32
- iface = gr.Interface(fn=prediction,
33
- inputs=gr.Image(type="pil"),
34
- outputs=[gr.JSON(label="json parsing"),
35
- gr.Image(type="pil", label="blurred image"),
36
- gr.JSON(label="json parsing"),
37
- gr.Image(type="pil", label="annotated image"),
38
- gr.JSON(label="json parsing")],
39
- title=title,
40
- description=description,
41
- examples=examples,
42
- css=css)
43
-
44
- iface.launch()
 
 
1
+ from cord_inference import prediction as cord_prediction
2
+ from sroie_inference import prediction as sroie_prediction
3
+ import gradio as gr
4
+ import json
5
+
6
+ def prediction(image):
7
+
8
+ #we first use mp-02/layoutlmv3-finetuned-cord on the image, which gives us a JSON with some info and a blurred image
9
+ j, image_blurred = sroie_prediction(image)
10
+
11
+ #then we use the model fine-tuned on sroie (for now it is Theivaprakasham/layoutlmv3-finetuned-sroie)
12
+ image = image_blurred
13
+ j2, image_final = cord_prediction(image)
14
+
15
+ #we then link the two json files
16
+ if len(d) == 0:
17
+ j3 = j2
18
+ else:
19
+ j3 = json.dumps(j).split('}')[0] + ', ' + json.dumps(j2).split('{')[1]
20
+
21
+ return j, image_blurred, j2, image_final, j3
22
+
23
+
24
+ title = "Interactive demo: LayoutLMv3 for receipts"
25
+ description = "Demo for Microsoft's LayoutLMv3, a Transformer for state-of-the-art document image understanding tasks. This particular model is fine-tuned on CORD and SROIE, which are datasets of receipts.\n It firsts uses the fine-tune on SROIE to extract date, company and address, then the fine-tune on CORD for the other info.\n To use it, simply upload an image or use the example image below. Results will show up in a few seconds."
26
+ examples = [['image.png']]
27
+
28
+ css = """.output_image, .input_image {height: 600px !important}"""
29
+
30
+ # we use a gradio interface that takes in input an image and return a JSON file that contains its info
31
+ # we show also the intermediate steps (first we take some info with the model fine-tuned on SROIE and we blur the relative boxes
32
+ # then we pass the image to the model fine-tuned on CORD
33
+ iface = gr.Interface(fn=prediction,
34
+ inputs=gr.Image(type="pil"),
35
+ outputs=[gr.JSON(label="json parsing"),
36
+ gr.Image(type="pil", label="blurred image"),
37
+ gr.JSON(label="json parsing"),
38
+ gr.Image(type="pil", label="annotated image"),
39
+ gr.JSON(label="json parsing")],
40
+ title=title,
41
+ description=description,
42
+ examples=examples,
43
+ css=css)
44
+
45
+ iface.launch()