Spaces:
Sleeping
Sleeping
xiaoyao9184
commited on
Commit
•
bb007de
1
Parent(s):
f53438d
Synced repo using 'sync_with_huggingface' Github Action
Browse files- gradio_app.py +59 -43
gradio_app.py
CHANGED
@@ -2,9 +2,12 @@ import os
|
|
2 |
import sys
|
3 |
|
4 |
if "APP_PATH" in os.environ:
|
5 |
-
os.
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
import gradio as gr
|
10 |
|
@@ -27,8 +30,7 @@ from marker.output import text_from_rendered
|
|
27 |
def load_models():
|
28 |
return create_model_dict()
|
29 |
|
30 |
-
def convert_pdf(fname: str,
|
31 |
-
config_parser = ConfigParser(kwargs)
|
32 |
config_dict = config_parser.generate_config_dict()
|
33 |
config_dict["pdftext_workers"] = 1
|
34 |
converter = PdfConverter(
|
@@ -57,9 +59,6 @@ def get_page_image(pdf_file, page_num, dpi=96):
|
|
57 |
png_image = png.convert("RGB")
|
58 |
return png_image
|
59 |
|
60 |
-
def get_uploaded_image(in_file):
|
61 |
-
return Image.open(in_file).convert("RGB")
|
62 |
-
|
63 |
|
64 |
def img_to_html(img, img_alt):
|
65 |
img_bytes = io.BytesIO()
|
@@ -80,8 +79,9 @@ def markdown_insert_images(markdown, images):
|
|
80 |
markdown = markdown.replace(image_markdown, img_to_html(images[image_path], image_alt))
|
81 |
return markdown
|
82 |
|
83 |
-
|
84 |
-
model_dict
|
|
|
85 |
|
86 |
with gr.Blocks(title="Marker") as demo:
|
87 |
gr.Markdown("""
|
@@ -94,54 +94,63 @@ with gr.Blocks(title="Marker") as demo:
|
|
94 |
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
-
in_file = gr.File(label="PDF file
|
98 |
-
in_num = gr.Slider(label="
|
99 |
-
in_img = gr.Image(label="
|
100 |
|
101 |
page_range_txt = gr.Textbox(label="Page range to parse, comma separated like 0,5-10,20", value=f"0-0")
|
102 |
output_format_dd = gr.Dropdown(label="Output format", choices=["markdown", "json", "html"], value="markdown")
|
103 |
|
104 |
force_ocr_ckb = gr.Checkbox(label="Force OCR", value=True, info="Force OCR on all pages")
|
105 |
debug_ckb = gr.Checkbox(label="Debug", value=False, info="Show debug information")
|
106 |
-
trun_marker_btn = gr.Button("Run Marker")
|
107 |
with gr.Column():
|
108 |
-
result_md = gr.Markdown(label="Result markdown")
|
109 |
-
result_json = gr.JSON(label="Result json")
|
110 |
-
result_html = gr.Markdown(label="Result html")
|
111 |
debug_img_pdf = gr.Image(label="PDF debug image", visible=False)
|
112 |
debug_img_layout = gr.Image(label="Layout debug image", visible=False)
|
113 |
|
114 |
def show_image(file, num=1):
|
115 |
-
if file
|
116 |
-
count = count_pdf(file)
|
117 |
-
img = get_page_image(file, num)
|
118 |
-
return [
|
119 |
-
gr.update(visible=True, maximum=count),
|
120 |
-
gr.update(value=img)]
|
121 |
-
else:
|
122 |
-
img = get_uploaded_image(file)
|
123 |
return [
|
|
|
124 |
gr.update(visible=False),
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
in_file.upload(
|
128 |
fn=show_image,
|
129 |
inputs=[in_file],
|
130 |
-
outputs=[in_num, in_img]
|
131 |
)
|
132 |
in_num.change(
|
133 |
fn=show_image,
|
134 |
inputs=[in_file, in_num],
|
135 |
-
outputs=[in_num, in_img]
|
136 |
)
|
137 |
|
138 |
def check_page_range(page_range, file):
|
139 |
count = count_pdf(file) if file is not None else 1
|
140 |
if not re.match(r"^(\d+(-\d+)?)?$", page_range):
|
141 |
gr.Warning(f"Invalid format. Please use 0-{count-1}", duration=0)
|
142 |
-
return
|
|
|
|
|
143 |
else:
|
144 |
-
return
|
|
|
|
|
145 |
page_range_txt.change(
|
146 |
fn=check_page_range,
|
147 |
inputs=[page_range_txt, in_file],
|
@@ -150,28 +159,34 @@ with gr.Blocks(title="Marker") as demo:
|
|
150 |
|
151 |
# Run Marker
|
152 |
def run_marker_img(filename, page_range, force_ocr, output_format, debug):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
rendered = convert_pdf(
|
154 |
filename,
|
155 |
-
|
156 |
-
force_ocr=force_ocr,
|
157 |
-
output_format=output_format,
|
158 |
-
output_dir=settings.DEBUG_DATA_FOLDER if debug else None,
|
159 |
-
debug=debug
|
160 |
)
|
161 |
-
text, ext, images = text_from_rendered(rendered)
|
162 |
-
|
163 |
gr_debug_pdf = gr.update(visible=False)
|
164 |
gr_debug_lay = gr.update(visible=False)
|
165 |
if debug:
|
166 |
debug_data_path = rendered.metadata.get("debug_data_path")
|
167 |
if debug_data_path:
|
168 |
-
|
|
|
|
|
|
|
169 |
img = Image.open(pdf_image_path)
|
170 |
gr_debug_pdf = gr.update(visible=True, value=img)
|
171 |
-
layout_image_path = os.path.join(debug_data_path, f"
|
172 |
img = Image.open(layout_image_path)
|
173 |
gr_debug_lay = gr.update(visible=True, value=img)
|
174 |
|
|
|
175 |
if output_format == "markdown":
|
176 |
text = markdown_insert_images(text, images)
|
177 |
return [
|
@@ -197,11 +212,12 @@ with gr.Blocks(title="Marker") as demo:
|
|
197 |
gr_debug_pdf,
|
198 |
gr_debug_lay
|
199 |
]
|
200 |
-
|
201 |
trun_marker_btn.click(
|
202 |
fn=run_marker_img,
|
203 |
inputs=[in_file, page_range_txt, force_ocr_ckb, output_format_dd, debug_ckb],
|
204 |
-
outputs=[result_md, result_json, result_html, debug_img_pdf, debug_img_layout]
|
205 |
)
|
206 |
|
207 |
-
|
|
|
|
2 |
import sys
|
3 |
|
4 |
if "APP_PATH" in os.environ:
|
5 |
+
app_path = os.path.abspath(os.environ["APP_PATH"])
|
6 |
+
if os.getcwd() != app_path:
|
7 |
+
# fix sys.path for import
|
8 |
+
os.chdir(app_path)
|
9 |
+
if app_path not in sys.path:
|
10 |
+
sys.path.append(app_path)
|
11 |
|
12 |
import gradio as gr
|
13 |
|
|
|
30 |
def load_models():
|
31 |
return create_model_dict()
|
32 |
|
33 |
+
def convert_pdf(fname: str, config_parser: ConfigParser) -> (str, Dict[str, Any], dict):
|
|
|
34 |
config_dict = config_parser.generate_config_dict()
|
35 |
config_dict["pdftext_workers"] = 1
|
36 |
converter = PdfConverter(
|
|
|
59 |
png_image = png.convert("RGB")
|
60 |
return png_image
|
61 |
|
|
|
|
|
|
|
62 |
|
63 |
def img_to_html(img, img_alt):
|
64 |
img_bytes = io.BytesIO()
|
|
|
79 |
markdown = markdown.replace(image_markdown, img_to_html(images[image_path], image_alt))
|
80 |
return markdown
|
81 |
|
82 |
+
# Load models if not already loaded in reload mode
|
83 |
+
if 'model_dict' not in globals():
|
84 |
+
model_dict = load_models()
|
85 |
|
86 |
with gr.Blocks(title="Marker") as demo:
|
87 |
gr.Markdown("""
|
|
|
94 |
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
97 |
+
in_file = gr.File(label="PDF file:", file_types=[".pdf"])
|
98 |
+
in_num = gr.Slider(label="PDF file page number", minimum=1, maximum=1, value=1, step=1, visible=False)
|
99 |
+
in_img = gr.Image(label="PDF file (preview)", type="pil", sources=None, visible=False)
|
100 |
|
101 |
page_range_txt = gr.Textbox(label="Page range to parse, comma separated like 0,5-10,20", value=f"0-0")
|
102 |
output_format_dd = gr.Dropdown(label="Output format", choices=["markdown", "json", "html"], value="markdown")
|
103 |
|
104 |
force_ocr_ckb = gr.Checkbox(label="Force OCR", value=True, info="Force OCR on all pages")
|
105 |
debug_ckb = gr.Checkbox(label="Debug", value=False, info="Show debug information")
|
106 |
+
trun_marker_btn = gr.Button("Run Marker", interactive=False)
|
107 |
with gr.Column():
|
108 |
+
result_md = gr.Markdown(label="Result markdown", visible=False)
|
109 |
+
result_json = gr.JSON(label="Result json", visible=False)
|
110 |
+
result_html = gr.Markdown(label="Result html", visible=False)
|
111 |
debug_img_pdf = gr.Image(label="PDF debug image", visible=False)
|
112 |
debug_img_layout = gr.Image(label="Layout debug image", visible=False)
|
113 |
|
114 |
def show_image(file, num=1):
|
115 |
+
if file is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
return [
|
117 |
+
gr.update(visible=False, maximum=1, value=num),
|
118 |
gr.update(visible=False),
|
119 |
+
"0-0"]
|
120 |
+
count = count_pdf(file)
|
121 |
+
img = get_page_image(file, num)
|
122 |
+
return [
|
123 |
+
gr.update(visible=True, maximum=count),
|
124 |
+
gr.update(visible=True, value=img),
|
125 |
+
f"0-{num-1}"]
|
126 |
+
|
127 |
+
in_file.clear(
|
128 |
+
fn=show_image,
|
129 |
+
inputs=[in_file],
|
130 |
+
outputs=[in_num, in_img, page_range_txt]
|
131 |
+
)
|
132 |
in_file.upload(
|
133 |
fn=show_image,
|
134 |
inputs=[in_file],
|
135 |
+
outputs=[in_num, in_img, page_range_txt]
|
136 |
)
|
137 |
in_num.change(
|
138 |
fn=show_image,
|
139 |
inputs=[in_file, in_num],
|
140 |
+
outputs=[in_num, in_img, page_range_txt]
|
141 |
)
|
142 |
|
143 |
def check_page_range(page_range, file):
|
144 |
count = count_pdf(file) if file is not None else 1
|
145 |
if not re.match(r"^(\d+(-\d+)?)?$", page_range):
|
146 |
gr.Warning(f"Invalid format. Please use 0-{count-1}", duration=0)
|
147 |
+
return [
|
148 |
+
gr.update(info=f"format 0-{count-1}"),
|
149 |
+
gr.update(interactive=False)]
|
150 |
else:
|
151 |
+
return [
|
152 |
+
gr.update(info=f"format 0-{count-1}"),
|
153 |
+
gr.update(interactive=True)]
|
154 |
page_range_txt.change(
|
155 |
fn=check_page_range,
|
156 |
inputs=[page_range_txt, in_file],
|
|
|
159 |
|
160 |
# Run Marker
|
161 |
def run_marker_img(filename, page_range, force_ocr, output_format, debug):
|
162 |
+
cli_options = {
|
163 |
+
"output_format": output_format,
|
164 |
+
"page_range": page_range,
|
165 |
+
"force_ocr": force_ocr,
|
166 |
+
"debug": debug,
|
167 |
+
"output_dir": settings.DEBUG_DATA_FOLDER if debug else None,
|
168 |
+
}
|
169 |
+
config_parser = ConfigParser(cli_options)
|
170 |
rendered = convert_pdf(
|
171 |
filename,
|
172 |
+
config_parser
|
|
|
|
|
|
|
|
|
173 |
)
|
|
|
|
|
174 |
gr_debug_pdf = gr.update(visible=False)
|
175 |
gr_debug_lay = gr.update(visible=False)
|
176 |
if debug:
|
177 |
debug_data_path = rendered.metadata.get("debug_data_path")
|
178 |
if debug_data_path:
|
179 |
+
page_range = config_parser.generate_config_dict()["page_range"]
|
180 |
+
first_page = page_range[0] if page_range else 0
|
181 |
+
|
182 |
+
pdf_image_path = os.path.join(debug_data_path, f"pdf_page_{first_page}.png")
|
183 |
img = Image.open(pdf_image_path)
|
184 |
gr_debug_pdf = gr.update(visible=True, value=img)
|
185 |
+
layout_image_path = os.path.join(debug_data_path, f"layout_page_{first_page}.png")
|
186 |
img = Image.open(layout_image_path)
|
187 |
gr_debug_lay = gr.update(visible=True, value=img)
|
188 |
|
189 |
+
text, ext, images = text_from_rendered(rendered)
|
190 |
if output_format == "markdown":
|
191 |
text = markdown_insert_images(text, images)
|
192 |
return [
|
|
|
212 |
gr_debug_pdf,
|
213 |
gr_debug_lay
|
214 |
]
|
215 |
+
|
216 |
trun_marker_btn.click(
|
217 |
fn=run_marker_img,
|
218 |
inputs=[in_file, page_range_txt, force_ocr_ckb, output_format_dd, debug_ckb],
|
219 |
+
outputs=[result_md, result_json, result_html, debug_img_pdf, debug_img_layout]
|
220 |
)
|
221 |
|
222 |
+
if __name__ == "__main__":
|
223 |
+
demo.launch()
|