rehan953 commited on
Commit
a58a36b
Β·
verified Β·
1 Parent(s): 133ecf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -8,22 +8,37 @@ GLMOCR_BASE = os.path.dirname(glmocr.__file__)
8
  config_path = os.path.join(GLMOCR_BASE, "config.yaml")
9
  formatter_path = os.path.join(GLMOCR_BASE, "postprocess", "result_formatter.py")
10
 
11
- # ── STEP 1: Fix config ────────────────────────────────────────
 
 
 
 
12
  with open(config_path, "r") as f:
13
  config = yaml.safe_load(f)
14
 
15
  config["pipeline"]["maas"]["enabled"] = True
16
  config["pipeline"]["maas"]["api_key"] = "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"
17
- config["pipeline"]["result_formatter"]["abandon"] = [
18
- "number", "footnote", "aside_text",
19
- "reference", "footer_image", "header_image",
20
- ]
21
- config["pipeline"]["enable_layout"] = True
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  with open(config_path, "w") as f:
24
  yaml.dump(config, f, default_flow_style=False, sort_keys=False)
25
 
26
- print("βœ… config.yaml fixed")
27
 
28
  # ── STEP 2: Fix result_formatter.py ──────────────────────────
29
  with open(formatter_path, "r") as f:
@@ -47,10 +62,7 @@ print("βœ… result_formatter.py fixed")
47
 
48
  # ── STEP 3: Helpers ───────────────────────────────────────────
49
  def get_header_footer_zones(regions, norm_height=1000):
50
- """
51
- From API regions' bbox_2d (normalized 0 to norm_height), return
52
- (header_end_frac, footer_start_frac). No hardcoded percentages.
53
- """
54
  if not regions:
55
  return None, None
56
  y_tops = []
@@ -62,9 +74,7 @@ def get_header_footer_zones(regions, norm_height=1000):
62
  y_bottoms.append(bbox[3])
63
  if not y_tops:
64
  return None, None
65
- header_end = min(y_tops) / norm_height
66
- footer_start = max(y_bottoms) / norm_height
67
- return header_end, footer_start
68
 
69
 
70
  def extract_zone_text(pdf_path, page_num, y_start_frac, y_end_frac):
@@ -131,17 +141,19 @@ def run_ocr(uploaded_file):
131
 
132
  if is_pdf:
133
  header_end, footer_start = get_header_footer_zones(regions, 1000)
 
 
134
 
135
- if header_end is not None and header_end > 0:
136
- hdr = extract_zone_text(path, page_num, 0, header_end)
137
  if hdr and hdr.strip():
138
  parts.append(hdr.strip())
139
 
140
  if page_md:
141
  parts.append(page_md)
142
 
143
- if footer_start is not None and footer_start < 1.0:
144
- ftr = extract_zone_text(path, page_num, footer_start, 1.0)
145
  if ftr and ftr.strip():
146
  parts.append(ftr.strip())
147
  else:
@@ -163,7 +175,7 @@ def run_ocr(uploaded_file):
163
  with gr.Blocks(title="GLM-OCR") as demo:
164
  gr.Markdown("""
165
  # πŸ” GLM-OCR
166
- Upload a PDF or image. Headers and footers from the PDF text layer (all PDFs).
167
  """)
168
 
169
  file_input = gr.File(
 
8
  config_path = os.path.join(GLMOCR_BASE, "config.yaml")
9
  formatter_path = os.path.join(GLMOCR_BASE, "postprocess", "result_formatter.py")
10
 
11
+ # Fallback band when API reports no gap (so all PDFs get header/footer)
12
+ FALLBACK_HEADER_FRAC = 0.05
13
+ FALLBACK_FOOTER_FRAC = 0.05
14
+
15
+ # ── STEP 1: Remove header/footer from abandon lists only ──────
16
  with open(config_path, "r") as f:
17
  config = yaml.safe_load(f)
18
 
19
  config["pipeline"]["maas"]["enabled"] = True
20
  config["pipeline"]["maas"]["api_key"] = "4570c28bdea5493c9efae9dae68edc66.sGbA9DLlcX1GlvqV"
21
+
22
+ to_remove = {"header", "footer"}
23
+
24
+ if "layout" in config["pipeline"] and "label_task_mapping" in config["pipeline"]["layout"]:
25
+ abandon = config["pipeline"]["layout"]["label_task_mapping"].get("abandon")
26
+ if isinstance(abandon, list):
27
+ config["pipeline"]["layout"]["label_task_mapping"]["abandon"] = [
28
+ x for x in abandon if x not in to_remove
29
+ ]
30
+
31
+ if "result_formatter" in config["pipeline"]:
32
+ abandon = config["pipeline"]["result_formatter"].get("abandon")
33
+ if isinstance(abandon, list):
34
+ config["pipeline"]["result_formatter"]["abandon"] = [
35
+ x for x in abandon if x not in to_remove
36
+ ]
37
 
38
  with open(config_path, "w") as f:
39
  yaml.dump(config, f, default_flow_style=False, sort_keys=False)
40
 
41
+ print("βœ… config.yaml updated (header/footer removed from abandon only)")
42
 
43
  # ── STEP 2: Fix result_formatter.py ──────────────────────────
44
  with open(formatter_path, "r") as f:
 
62
 
63
  # ── STEP 3: Helpers ───────────────────────────────────────────
64
  def get_header_footer_zones(regions, norm_height=1000):
65
+ """From bbox_2d (0–norm_height) return (header_end_frac, footer_start_frac)."""
 
 
 
66
  if not regions:
67
  return None, None
68
  y_tops = []
 
74
  y_bottoms.append(bbox[3])
75
  if not y_tops:
76
  return None, None
77
+ return min(y_tops) / norm_height, max(y_bottoms) / norm_height
 
 
78
 
79
 
80
  def extract_zone_text(pdf_path, page_num, y_start_frac, y_end_frac):
 
141
 
142
  if is_pdf:
143
  header_end, footer_start = get_header_footer_zones(regions, 1000)
144
+ he = max(header_end, FALLBACK_HEADER_FRAC) if header_end is not None else FALLBACK_HEADER_FRAC
145
+ fs = min(footer_start, 1.0 - FALLBACK_FOOTER_FRAC) if footer_start is not None else (1.0 - FALLBACK_FOOTER_FRAC)
146
 
147
+ if he > 0:
148
+ hdr = extract_zone_text(path, page_num, 0, he)
149
  if hdr and hdr.strip():
150
  parts.append(hdr.strip())
151
 
152
  if page_md:
153
  parts.append(page_md)
154
 
155
+ if fs < 1.0:
156
+ ftr = extract_zone_text(path, page_num, fs, 1.0)
157
  if ftr and ftr.strip():
158
  parts.append(ftr.strip())
159
  else:
 
175
  with gr.Blocks(title="GLM-OCR") as demo:
176
  gr.Markdown("""
177
  # πŸ” GLM-OCR
178
+ Upload a PDF or image. Headers and footers from PDF text layer (all PDFs).
179
  """)
180
 
181
  file_input = gr.File(