Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,36 +47,36 @@ with open(formatter_path, "w") as f:
|
|
| 47 |
print("β
result_formatter.py fixed")
|
| 48 |
|
| 49 |
# ββ STEP 3: Helper functions ββββββββββββββββββββββββββββββββββ
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
for r in regions:
|
| 54 |
bbox = r.get("bbox_2d")
|
| 55 |
if bbox and len(bbox) == 4:
|
| 56 |
y_tops.append(bbox[1])
|
| 57 |
-
y_bottoms.append(bbox[3])
|
| 58 |
if not y_tops:
|
| 59 |
-
return None
|
| 60 |
first_y = min(y_tops)
|
| 61 |
-
|
| 62 |
-
top_gap = (first_y / img_height) if first_y > img_height * 0.08 else None
|
| 63 |
-
bottom_gap = (last_y / img_height) if last_y < img_height * 0.92 else None
|
| 64 |
-
return top_gap, bottom_gap
|
| 65 |
|
| 66 |
|
| 67 |
def extract_zone_text(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 68 |
-
|
|
|
|
| 69 |
doc = fitz.open(pdf_path)
|
| 70 |
page = doc[page_num]
|
| 71 |
-
h
|
| 72 |
-
|
| 73 |
-
rect = fitz.Rect(0, h * y_start_frac, w, h * y_end_frac)
|
| 74 |
-
text = page.get_text(clip=rect).strip()
|
| 75 |
doc.close()
|
| 76 |
return text
|
| 77 |
|
| 78 |
|
| 79 |
def get_page_data(page_result):
|
|
|
|
| 80 |
regions = []
|
| 81 |
if hasattr(page_result, "json_result"):
|
| 82 |
jr = page_result.json_result
|
|
@@ -97,12 +97,12 @@ def run_ocr(uploaded_file):
|
|
| 97 |
|
| 98 |
try:
|
| 99 |
from glmocr import parse
|
|
|
|
| 100 |
|
| 101 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 102 |
is_pdf = path.lower().endswith(".pdf")
|
| 103 |
|
| 104 |
if is_pdf:
|
| 105 |
-
import fitz
|
| 106 |
doc = fitz.open(path)
|
| 107 |
page_images = []
|
| 108 |
page_heights = []
|
|
@@ -111,6 +111,7 @@ def run_ocr(uploaded_file):
|
|
| 111 |
img_path = f"/tmp/maas_page_{i}.png"
|
| 112 |
pix.save(img_path)
|
| 113 |
page_images.append(img_path)
|
|
|
|
| 114 |
page_heights.append(doc[i].rect.height * 1.5)
|
| 115 |
doc.close()
|
| 116 |
results = parse(page_images)
|
|
@@ -129,23 +130,20 @@ def run_ocr(uploaded_file):
|
|
| 129 |
parts = []
|
| 130 |
|
| 131 |
if is_pdf and page_num < len(page_heights):
|
| 132 |
-
top_gap
|
| 133 |
|
| 134 |
-
# HEADER at TOP
|
| 135 |
if top_gap is not None:
|
| 136 |
hdr = extract_zone_text(path, page_num, 0, top_gap)
|
| 137 |
if hdr:
|
| 138 |
parts.append(hdr)
|
| 139 |
|
| 140 |
-
# BODY
|
| 141 |
if page_md:
|
| 142 |
parts.append(page_md)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
-
|
| 146 |
-
ftr = extract_zone_text(path, page_num, bottom_gap, 1.0)
|
| 147 |
-
if ftr:
|
| 148 |
-
parts.append(ftr)
|
| 149 |
else:
|
| 150 |
if page_md:
|
| 151 |
parts.append(page_md)
|
|
@@ -153,6 +151,7 @@ def run_ocr(uploaded_file):
|
|
| 153 |
if parts:
|
| 154 |
all_pages_md.append("\n\n".join(parts))
|
| 155 |
|
|
|
|
| 156 |
final = "\n\n---\n\n".join(all_pages_md) if all_pages_md else "(No content)"
|
| 157 |
return final
|
| 158 |
|
|
@@ -166,7 +165,7 @@ with gr.Blocks(title="GLM-OCR") as demo:
|
|
| 166 |
gr.Markdown("""
|
| 167 |
# π GLM-OCR
|
| 168 |
Upload a PDF or image to extract text.
|
| 169 |
-
Headers
|
| 170 |
""")
|
| 171 |
|
| 172 |
file_input = gr.File(
|
|
|
|
| 47 |
print("β
result_formatter.py fixed")
|
| 48 |
|
| 49 |
# ββ STEP 3: Helper functions ββββββββββββββββββββββββββββββββββ
|
| 50 |
+
def get_top_gap(regions, img_height):
|
| 51 |
+
"""
|
| 52 |
+
Find the top gap the API missed using bbox_2d coordinates.
|
| 53 |
+
Only extracts header (top gap) β never bottom β to avoid
|
| 54 |
+
duplicating body content the API already captured correctly.
|
| 55 |
+
"""
|
| 56 |
+
y_tops = []
|
| 57 |
for r in regions:
|
| 58 |
bbox = r.get("bbox_2d")
|
| 59 |
if bbox and len(bbox) == 4:
|
| 60 |
y_tops.append(bbox[1])
|
|
|
|
| 61 |
if not y_tops:
|
| 62 |
+
return None
|
| 63 |
first_y = min(y_tops)
|
| 64 |
+
return (first_y / img_height) if first_y > img_height * 0.08 else None
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
def extract_zone_text(pdf_path, page_num, y_start_frac, y_end_frac):
|
| 68 |
+
"""Extract text from a vertical slice of a PDF page."""
|
| 69 |
+
import pymupdf as fitz
|
| 70 |
doc = fitz.open(pdf_path)
|
| 71 |
page = doc[page_num]
|
| 72 |
+
h, w = page.rect.height, page.rect.width
|
| 73 |
+
text = page.get_text(clip=fitz.Rect(0, h * y_start_frac, w, h * y_end_frac)).strip()
|
|
|
|
|
|
|
| 74 |
doc.close()
|
| 75 |
return text
|
| 76 |
|
| 77 |
|
| 78 |
def get_page_data(page_result):
|
| 79 |
+
"""Extract markdown and raw regions from one PipelineResult."""
|
| 80 |
regions = []
|
| 81 |
if hasattr(page_result, "json_result"):
|
| 82 |
jr = page_result.json_result
|
|
|
|
| 97 |
|
| 98 |
try:
|
| 99 |
from glmocr import parse
|
| 100 |
+
import pymupdf as fitz
|
| 101 |
|
| 102 |
path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
|
| 103 |
is_pdf = path.lower().endswith(".pdf")
|
| 104 |
|
| 105 |
if is_pdf:
|
|
|
|
| 106 |
doc = fitz.open(path)
|
| 107 |
page_images = []
|
| 108 |
page_heights = []
|
|
|
|
| 111 |
img_path = f"/tmp/maas_page_{i}.png"
|
| 112 |
pix.save(img_path)
|
| 113 |
page_images.append(img_path)
|
| 114 |
+
# Image height at 1.5x matches bbox_2d coordinate space
|
| 115 |
page_heights.append(doc[i].rect.height * 1.5)
|
| 116 |
doc.close()
|
| 117 |
results = parse(page_images)
|
|
|
|
| 130 |
parts = []
|
| 131 |
|
| 132 |
if is_pdf and page_num < len(page_heights):
|
| 133 |
+
top_gap = get_top_gap(regions, page_heights[page_num])
|
| 134 |
|
| 135 |
+
# HEADER at TOP β from PDF text layer (API misses this zone)
|
| 136 |
if top_gap is not None:
|
| 137 |
hdr = extract_zone_text(path, page_num, 0, top_gap)
|
| 138 |
if hdr:
|
| 139 |
parts.append(hdr)
|
| 140 |
|
| 141 |
+
# BODY β from API (includes all content + footer text correctly)
|
| 142 |
if page_md:
|
| 143 |
parts.append(page_md)
|
| 144 |
|
| 145 |
+
# No bottom zone extraction β API already captures footer content.
|
| 146 |
+
# Extracting bottom zone causes duplication of body content.
|
|
|
|
|
|
|
|
|
|
| 147 |
else:
|
| 148 |
if page_md:
|
| 149 |
parts.append(page_md)
|
|
|
|
| 151 |
if parts:
|
| 152 |
all_pages_md.append("\n\n".join(parts))
|
| 153 |
|
| 154 |
+
# Exactly like original SDK output format
|
| 155 |
final = "\n\n---\n\n".join(all_pages_md) if all_pages_md else "(No content)"
|
| 156 |
return final
|
| 157 |
|
|
|
|
| 165 |
gr.Markdown("""
|
| 166 |
# π GLM-OCR
|
| 167 |
Upload a PDF or image to extract text.
|
| 168 |
+
Headers included at top of each page.
|
| 169 |
""")
|
| 170 |
|
| 171 |
file_input = gr.File(
|