Spaces:
Sleeping
Sleeping
xiaoyao9184
commited on
Synced repo using 'sync_with_huggingface' Github Action
Browse files- gradio_app.py +143 -0
- requirements.txt +4 -0
gradio_app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
from torch import Value
|
5 |
+
|
6 |
+
if "APP_PATH" in os.environ:
|
7 |
+
sys.path.append(os.environ["APP_PATH"])
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
from tabled.assignment import assign_rows_columns
|
12 |
+
from tabled.fileinput import load_pdfs_images
|
13 |
+
from tabled.formats.markdown import markdown_format
|
14 |
+
from tabled.inference.detection import detect_tables
|
15 |
+
from tabled.inference.recognition import get_cells, recognize_tables
|
16 |
+
|
17 |
+
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
18 |
+
os.environ["IN_STREAMLIT"] = "true"
|
19 |
+
import pypdfium2
|
20 |
+
|
21 |
+
import io
|
22 |
+
import tempfile
|
23 |
+
from PIL import Image
|
24 |
+
|
25 |
+
from tabled.inference.models import load_detection_models, load_recognition_models, load_layout_models
|
26 |
+
|
27 |
+
def load_models():
|
28 |
+
return load_detection_models(), load_recognition_models(), load_layout_models()
|
29 |
+
|
30 |
+
def run_table_rec(image, highres_image, text_line, models, skip_detection=False, detect_boxes=False):
|
31 |
+
if not skip_detection:
|
32 |
+
table_imgs, table_bboxes, _ = detect_tables([image], [highres_image], models[2])
|
33 |
+
else:
|
34 |
+
table_imgs = [highres_image]
|
35 |
+
table_bboxes = [[0, 0, highres_image.size[0], highres_image.size[1]]]
|
36 |
+
|
37 |
+
table_text_lines = [text_line] * len(table_imgs)
|
38 |
+
highres_image_sizes = [highres_image.size] * len(table_imgs)
|
39 |
+
cells, needs_ocr = get_cells(table_imgs, table_bboxes, highres_image_sizes, table_text_lines, models[0], detect_boxes=detect_boxes)
|
40 |
+
|
41 |
+
table_rec = recognize_tables(table_imgs, cells, needs_ocr, models[1])
|
42 |
+
cells = [assign_rows_columns(tr, im_size) for tr, im_size in zip(table_rec, highres_image_sizes)]
|
43 |
+
|
44 |
+
out_data = []
|
45 |
+
for idx, (cell, pred, table_img) in enumerate(zip(cells, table_rec, table_imgs)):
|
46 |
+
md = markdown_format(cell)
|
47 |
+
out_data.append((md, table_img))
|
48 |
+
return out_data
|
49 |
+
|
50 |
+
def open_pdf(pdf_file):
|
51 |
+
return pypdfium2.PdfDocument(pdf_file)
|
52 |
+
|
53 |
+
def count_pdf(pdf_file):
|
54 |
+
doc = open_pdf(pdf_file)
|
55 |
+
return len(doc)
|
56 |
+
|
57 |
+
def get_page_image(pdf_file, page_num, dpi=96):
|
58 |
+
doc = open_pdf(pdf_file)
|
59 |
+
renderer = doc.render(
|
60 |
+
pypdfium2.PdfBitmap.to_pil,
|
61 |
+
page_indices=[page_num - 1],
|
62 |
+
scale=dpi / 72,
|
63 |
+
)
|
64 |
+
png = list(renderer)[0]
|
65 |
+
png_image = png.convert("RGB")
|
66 |
+
return png_image
|
67 |
+
|
68 |
+
def get_uploaded_image(in_file):
|
69 |
+
return Image.open(in_file).convert("RGB")
|
70 |
+
|
71 |
+
|
72 |
+
models = load_models()
|
73 |
+
|
74 |
+
with gr.Blocks(title="Tabled") as demo:
|
75 |
+
gr.Markdown("""
|
76 |
+
# Tabled Demo
|
77 |
+
|
78 |
+
This app will let you try tabled, a table detection and recognition model. It will detect and recognize the tables.
|
79 |
+
|
80 |
+
Find the project [here](https://github.com/VikParuchuri/tabled).
|
81 |
+
""")
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
with gr.Column():
|
85 |
+
in_file = gr.File(label="PDF file or image:", file_types=[".pdf", ".png", ".jpg", ".jpeg", ".gif", ".webp"])
|
86 |
+
in_num = gr.Slider(label="Page number", minimum=1, maximum=100, value=1, step=1)
|
87 |
+
in_img = gr.Image(label="Page of Image", type="pil", sources=None)
|
88 |
+
|
89 |
+
skip_detection_ckb = gr.Checkbox(label="Skip table detection", info="Use this if tables are already cropped (the whole PDF page or image is a table)")
|
90 |
+
detect_boxes_ckb = gr.Checkbox(label="Detect cell boxes", info="Detect table cell boxes vs extract from PDF. Will also run OCR.")
|
91 |
+
|
92 |
+
tabled_btn = gr.Button("Run Tabled")
|
93 |
+
with gr.Column():
|
94 |
+
result_img = gr.Gallery(label="Result images", show_label=False, columns=[1], rows=[10], object_fit="contain", height="auto")
|
95 |
+
result_md = gr.Markdown(label="Result markdown")
|
96 |
+
|
97 |
+
def show_image(file, num=1):
|
98 |
+
if file.endswith('.pdf'):
|
99 |
+
count = count_pdf(file)
|
100 |
+
img = get_page_image(file, num)
|
101 |
+
return [
|
102 |
+
gr.update(visible=True, maximum=count),
|
103 |
+
gr.update(value=img)]
|
104 |
+
else:
|
105 |
+
img = get_uploaded_image(file)
|
106 |
+
return [
|
107 |
+
gr.update(visible=False),
|
108 |
+
gr.update(value=img)]
|
109 |
+
|
110 |
+
in_file.upload(
|
111 |
+
fn=show_image,
|
112 |
+
inputs=[in_file],
|
113 |
+
outputs=[in_num, in_img],
|
114 |
+
)
|
115 |
+
in_num.change(
|
116 |
+
fn=show_image,
|
117 |
+
inputs=[in_file, in_num],
|
118 |
+
outputs=[in_num, in_img],
|
119 |
+
)
|
120 |
+
|
121 |
+
# Run OCR
|
122 |
+
def text_rec_img(in_file, page_number, skip_detection, detect_boxes):
|
123 |
+
images, highres_images, names, text_lines = load_pdfs_images(in_file, max_pages=1, start_page=page_number - 1)
|
124 |
+
|
125 |
+
out_data = run_table_rec(images[0], highres_images[0], text_lines[0], models, skip_detection=skip_detection, detect_boxes=detect_boxes)
|
126 |
+
|
127 |
+
if len(out_data) == 0:
|
128 |
+
gr.Warning(f"No tables found on page {page_number}", duration=5)
|
129 |
+
|
130 |
+
table_md = ""
|
131 |
+
table_imgs = []
|
132 |
+
for idx, (md, table_img) in enumerate(out_data):
|
133 |
+
table_md += f"\n## Table {idx}\n"
|
134 |
+
table_md += md
|
135 |
+
table_imgs.append(table_img)
|
136 |
+
return gr.update(rows=[len(table_imgs)], value=table_imgs), table_md
|
137 |
+
tabled_btn.click(
|
138 |
+
fn=text_rec_img,
|
139 |
+
inputs=[in_file, in_num, skip_detection_ckb, detect_boxes_ckb],
|
140 |
+
outputs=[result_img, result_md]
|
141 |
+
)
|
142 |
+
|
143 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.5.1
|
2 |
+
tabled-pdf==0.1.7
|
3 |
+
gradio==5.8.0
|
4 |
+
huggingface-hub==0.26.3
|