Spaces:
Runtime error
Runtime error
jpjpjpjpjp
commited on
Commit
•
0489f49
1
Parent(s):
5c19b42
Update app.py
Browse files
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 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
test = ""
|
34 |
-
# Step 4: define model
|
35 |
-
predictor = DefaultPredictor(cfg)
|
36 |
|
|
|
37 |
|
38 |
-
|
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 |
-
|
56 |
-
|
57 |
-
article = "<p style='text-align: center'><a href='https://
|
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 |
|