jpjpjpjpjp commited on
Commit
0489f49
1 Parent(s): 5c19b42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -49
app.py CHANGED
@@ -1,60 +1,35 @@
1
- import os
2
- os.system('pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html')
3
- os.system("git clone https://github.com/microsoft/unilm.git")
4
-
5
- import sys
6
- sys.path.append("unilm")
7
-
8
- import cv2
9
-
10
- from unilm.dit.object_detection.ditod import add_vit_config
11
-
12
- import torch
13
-
14
- from detectron2.config import CfgNode as CN
15
- from detectron2.config import get_cfg
16
- from detectron2.utils.visualizer import ColorMode, Visualizer
17
- from detectron2.data import MetadataCatalog
18
- from detectron2.engine import DefaultPredictor
19
-
20
  import gradio as gr
 
 
 
 
 
21
 
22
 
23
- # Step 1: instantiate config
24
- cfg = get_cfg()
25
- add_vit_config(cfg)
26
- cfg.merge_from_file("cascade_dit_base.yml")
 
27
 
28
- # Step 2: add model weights URL to config
29
- cfg.MODEL.WEIGHTS = "https://layoutlm.blob.core.windows.net/dit/dit-fts/publaynet_dit-b_cascade.pth"
 
 
 
 
 
30
 
31
- # Step 3: set device
32
- cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
33
- test = ""
34
- # Step 4: define model
35
- predictor = DefaultPredictor(cfg)
36
 
 
37
 
38
- def analyze_image(img):
39
- md = MetadataCatalog.get(cfg.DATASETS.TEST[0])
40
- if cfg.DATASETS.TEST[0]=='icdar2019_test':
41
- md.set(thing_classes=["table"])
42
- else:
43
- md.set(thing_classes=["text","title","list","table","figure"])
44
-
45
- output = predictor(img)["instances"]
46
- v = Visualizer(img[:, :, ::-1],
47
- md,
48
- scale=1.0,
49
- instance_mode=ColorMode.SEGMENTATION)
50
- result = v.draw_instance_predictions(output.to("cpu"))
51
- result_image = result.get_image()[:, :, ::-1]
52
-
53
- return result_image
54
 
55
- title = "Interactive demo: Document Layout Analysis with DiT"
56
- description = "Demo for Microsoft's DiT, the Document Image Transformer for state-of-the-art document understanding tasks. This particular model is fine-tuned on PubLayNet, a large dataset for document layout analysis (read more at the links below). 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'."
57
- article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2203.02378' target='_blank'>Paper</a> | <a href='https://github.com/microsoft/unilm/tree/master/dit' target='_blank'>Github Repo</a></p> | <a href='https://huggingface.co/docs/transformers/master/en/model_doc/dit' target='_blank'>HuggingFace doc</a></p>"
58
  examples =[['publaynet_example.jpeg']]
59
  css = ".output-image, .input-image, .image-preview {height: 600px !important}"
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import base64
3
+ import io
4
+ import requests
5
+ import json
6
+ from PIL import Image
7
 
8
 
9
+ def analyze_image(img):
10
+ #img64 = base64.b64decode(image)
11
+ im = Image.fromarray(image)
12
+ in_mem_file = io.BytesIO()
13
+ im.save(in_mem_file, format="png")
14
 
15
+ payload = {
16
+ "model":"Baseline",
17
+ "tasktype":"Extraction",
18
+ "questions":[{"Pages":[1],"Text":question}],
19
+ "image": base64.b64encode(in_mem_file.getvalue()).decode()
20
+ }
21
+ url = "https://ky8mfb27dj.execute-api.us-east-1.amazonaws.com/dev/analyzedocument/submit"
22
 
23
+ payload = json.dumps(payload)
24
+ headers = {'Content-Type': 'application/json'}
 
 
 
25
 
26
+ response = requests.request("POST", url, headers=headers, data=payload)
27
 
28
+ return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ description = "Hyland Demo for Document Question & Answering , fine-tuned on DocVQA (document visual question answering). To use it, simply upload your image and type a question and click 'submit', or click one of the examples to load them. Read more at the links below."
31
+ title = "DocVQA"
32
+ article = "<p style='text-align: center'><a href='https://www.docvqa.org/datasets/docvqa' target='_blank'>DocVQA: Challenge | <a href='https://rrc.cvc.uab.es/?ch=17' target='_blank'>Overview - Document Visual Question Answering</a></p>"
33
  examples =[['publaynet_example.jpeg']]
34
  css = ".output-image, .input-image, .image-preview {height: 600px !important}"
35