Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
import logging
|
| 4 |
import os
|
|
|
|
| 5 |
import tempfile
|
| 6 |
import uuid
|
| 7 |
from typing import List, Tuple
|
|
@@ -86,6 +87,60 @@ def _resize_for_inference(img):
|
|
| 86 |
return img.resize(new_size, Image.LANCZOS)
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def _infer_image(image_path: str) -> str:
|
| 90 |
"""Run fine-tuned model on a single image file and return markdown string."""
|
| 91 |
import torch
|
|
@@ -132,7 +187,9 @@ def _infer_image(image_path: str) -> str:
|
|
| 132 |
ids[0][inputs["input_ids"].shape[1]:],
|
| 133 |
skip_special_tokens=True,
|
| 134 |
)
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
|
| 137 |
finally:
|
| 138 |
try:
|
|
@@ -235,13 +292,26 @@ def _create_gradio_demo():
|
|
| 235 |
|
| 236 |
with gr.Blocks(title="GLM-OCR Fine-tuned") as demo:
|
| 237 |
gr.Markdown("# GLM-OCR (Fine-tuned)")
|
|
|
|
|
|
|
| 238 |
file_in = gr.File(
|
| 239 |
label="Upload PDF or image",
|
| 240 |
file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"],
|
| 241 |
)
|
| 242 |
run_btn = gr.Button("Run OCR", variant="primary")
|
| 243 |
-
out = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
run_btn.click(fn=run_ocr, inputs=file_in, outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
return demo
|
| 246 |
|
| 247 |
|
|
|
|
| 2 |
|
| 3 |
import logging
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
import tempfile
|
| 7 |
import uuid
|
| 8 |
from typing import List, Tuple
|
|
|
|
| 87 |
return img.resize(new_size, Image.LANCZOS)
|
| 88 |
|
| 89 |
|
| 90 |
+
def _clean_markdown(text: str) -> str:
|
| 91 |
+
"""Post-process markdown to fix common formatting issues."""
|
| 92 |
+
|
| 93 |
+
# Fix table formatting - ensure proper spacing
|
| 94 |
+
lines = text.split('\n')
|
| 95 |
+
cleaned = []
|
| 96 |
+
in_table = False
|
| 97 |
+
|
| 98 |
+
for i, line in enumerate(lines):
|
| 99 |
+
# Detect table rows
|
| 100 |
+
if '|' in line and line.strip().startswith('|'):
|
| 101 |
+
if not in_table:
|
| 102 |
+
# First table row - add blank line before if needed
|
| 103 |
+
if cleaned and cleaned[-1].strip():
|
| 104 |
+
cleaned.append('')
|
| 105 |
+
in_table = True
|
| 106 |
+
|
| 107 |
+
# Clean up table row spacing
|
| 108 |
+
line = re.sub(r'\s*\|\s*', ' | ', line)
|
| 109 |
+
line = re.sub(r'\s+', ' ', line)
|
| 110 |
+
cleaned.append(line.strip())
|
| 111 |
+
|
| 112 |
+
elif in_table and line.strip() == '':
|
| 113 |
+
# End of table - add blank line after
|
| 114 |
+
in_table = False
|
| 115 |
+
cleaned.append('')
|
| 116 |
+
|
| 117 |
+
else:
|
| 118 |
+
in_table = False
|
| 119 |
+
# Remove excessive blank lines
|
| 120 |
+
if line.strip() or (cleaned and cleaned[-1].strip()):
|
| 121 |
+
cleaned.append(line)
|
| 122 |
+
|
| 123 |
+
# Join lines
|
| 124 |
+
text = '\n'.join(cleaned)
|
| 125 |
+
|
| 126 |
+
# Fix header spacing
|
| 127 |
+
text = re.sub(r'(#{1,6})\s*([^\n]+)', r'\1 \2', text)
|
| 128 |
+
|
| 129 |
+
# Fix list spacing
|
| 130 |
+
text = re.sub(r'\n([•\-\*])\s+', r'\n\1 ', text)
|
| 131 |
+
|
| 132 |
+
# Remove trailing whitespace from lines
|
| 133 |
+
text = '\n'.join(line.rstrip() for line in text.split('\n'))
|
| 134 |
+
|
| 135 |
+
# Normalize multiple blank lines to maximum 2
|
| 136 |
+
text = re.sub(r'\n{4,}', '\n\n\n', text)
|
| 137 |
+
|
| 138 |
+
# Fix common OCR artifacts in tables
|
| 139 |
+
text = re.sub(r'\|\s*:\s*---\s*\|\s*:\s*---\s*\|', '| :--- | :--- |', text)
|
| 140 |
+
|
| 141 |
+
return text.strip()
|
| 142 |
+
|
| 143 |
+
|
| 144 |
def _infer_image(image_path: str) -> str:
|
| 145 |
"""Run fine-tuned model on a single image file and return markdown string."""
|
| 146 |
import torch
|
|
|
|
| 187 |
ids[0][inputs["input_ids"].shape[1]:],
|
| 188 |
skip_special_tokens=True,
|
| 189 |
)
|
| 190 |
+
|
| 191 |
+
# Clean up the markdown output
|
| 192 |
+
return _clean_markdown(result.strip())
|
| 193 |
|
| 194 |
finally:
|
| 195 |
try:
|
|
|
|
| 292 |
|
| 293 |
with gr.Blocks(title="GLM-OCR Fine-tuned") as demo:
|
| 294 |
gr.Markdown("# GLM-OCR (Fine-tuned)")
|
| 295 |
+
gr.Markdown("Upload a PDF or image to extract structured markdown content.")
|
| 296 |
+
|
| 297 |
file_in = gr.File(
|
| 298 |
label="Upload PDF or image",
|
| 299 |
file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"],
|
| 300 |
)
|
| 301 |
run_btn = gr.Button("Run OCR", variant="primary")
|
| 302 |
+
out = gr.Textbox(
|
| 303 |
+
lines=40,
|
| 304 |
+
label="Output (markdown)",
|
| 305 |
+
show_copy_button=True
|
| 306 |
+
)
|
| 307 |
run_btn.click(fn=run_ocr, inputs=file_in, outputs=out)
|
| 308 |
+
|
| 309 |
+
gr.Markdown("""
|
| 310 |
+
### Tips
|
| 311 |
+
- Tables are formatted in markdown format
|
| 312 |
+
- Use the copy button to export the markdown
|
| 313 |
+
- Each page is separated by `---page-separator---`
|
| 314 |
+
""")
|
| 315 |
return demo
|
| 316 |
|
| 317 |
|