rehan953 commited on
Commit
f7e376f
Β·
verified Β·
1 Parent(s): a3c1f00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -37
app.py CHANGED
@@ -8,7 +8,6 @@ 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
- # ── Config: MaaS on, keep header/footer ────────────────────────────────────
12
  with open(config_path, "r") as f:
13
  config = yaml.safe_load(f)
14
  config["pipeline"]["maas"]["enabled"] = True
@@ -20,7 +19,6 @@ config["pipeline"]["enable_layout"] = True
20
  with open(config_path, "w") as f:
21
  yaml.dump(config, f, default_flow_style=False, sort_keys=False)
22
 
23
- # ── Formatter: don't strip header/footer ───────────────────────────────────
24
  with open(formatter_path, "r") as f:
25
  source = f.read()
26
  for label in ['"header"', "'header'", '"footer"', "'footer'", '"doc_header"', "'doc_header'", '"doc_footer"', "'doc_footer'"]:
@@ -32,32 +30,6 @@ with open(formatter_path, "w") as f:
32
 
33
  ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
34
 
35
- def get_regions(page_item):
36
- if page_item is None:
37
- return []
38
- if isinstance(page_item, list):
39
- return page_item
40
- if isinstance(page_item, dict):
41
- for key in ("regions", "blocks", "items", "content_list"):
42
- if key in page_item and isinstance(page_item[key], list):
43
- return page_item[key]
44
- if "content" in page_item or "text" in page_item:
45
- return [page_item]
46
- if isinstance(page_item, str):
47
- return [{"label": "text", "content": page_item}]
48
- return []
49
-
50
- def get_label(r):
51
- if not isinstance(r, dict):
52
- return "text"
53
- return (r.get("label") or r.get("type") or r.get("block_type") or "text")
54
-
55
- def get_content(r):
56
- if isinstance(r, str):
57
- return r
58
- if not isinstance(r, dict):
59
- return str(r)
60
- return str(r.get("content") or r.get("text") or r.get("raw") or "")
61
 
62
  def run_ocr(uploaded_file):
63
  if uploaded_file is None:
@@ -81,22 +53,102 @@ def run_ocr(uploaded_file):
81
  result = parse(page_images)
82
  else:
83
  result = parse(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  pages_data = result if isinstance(result, list) else []
85
  total_headers = 0
86
  total_footers = 0
87
  all_markdown = []
88
  all_summary = []
89
- for page_num, page_item in enumerate(pages_data):
90
- page_regions = get_regions(page_item)
91
  if not isinstance(page_regions, list):
92
- page_regions = [page_regions]
93
  page_md = []
94
  page_summary = [f"── PAGE {page_num + 1} of {len(pages_data)} ──"]
95
  for region in page_regions:
96
- label = get_label(region)
97
- content = get_content(region)
98
- if not content.strip():
99
  continue
 
 
100
  if label in ABANDON:
101
  continue
102
  if label == "header":
@@ -113,20 +165,23 @@ def run_ocr(uploaded_file):
113
  all_markdown.append("\n\n".join(page_md))
114
  all_summary.extend(page_summary)
115
  all_summary.append("")
116
- summary = (
117
  f"Total pages : {len(pages_data)}\n"
118
  f"Headers found : {total_headers}\n"
119
  f"Footers found : {total_footers}\n"
120
  f"{'─'*40}\n" + "\n".join(all_summary)
121
  )
122
  markdown = "\n\n---\n\n".join(all_markdown) if all_markdown else "(No content)"
 
123
  return markdown, summary
 
124
  except Exception as e:
125
  import traceback
126
  return f"Error: {str(e)}\n\n{traceback.format_exc()}", "Failed."
127
 
 
128
  with gr.Blocks(title="GLM-OCR β€” MaaS") as demo:
129
- gr.Markdown("# πŸ” GLM-OCR β€” Zhipu MaaS\nUpload PDF or image. Headers πŸ”΅ and Footers 🟒 when returned by the API.")
130
  file_input = gr.File(label="Upload PDF or Image", file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"])
131
  run_btn = gr.Button("β–Ά Run OCR", variant="primary", size="lg")
132
  with gr.Row():
@@ -134,7 +189,7 @@ with gr.Blocks(title="GLM-OCR β€” MaaS") as demo:
134
  gr.Markdown("### πŸ“„ Markdown Output")
135
  markdown_out = gr.Textbox(lines=30, label="")
136
  with gr.Column():
137
- gr.Markdown("### πŸ—‚οΈ Detected Regions")
138
  regions_out = gr.Textbox(lines=30, label="")
139
  run_btn.click(fn=run_ocr, inputs=file_input, outputs=[markdown_out, regions_out])
140
  demo.launch()
 
8
  config_path = os.path.join(GLMOCR_BASE, "config.yaml")
9
  formatter_path = os.path.join(GLMOCR_BASE, "postprocess", "result_formatter.py")
10
 
 
11
  with open(config_path, "r") as f:
12
  config = yaml.safe_load(f)
13
  config["pipeline"]["maas"]["enabled"] = True
 
19
  with open(config_path, "w") as f:
20
  yaml.dump(config, f, default_flow_style=False, sort_keys=False)
21
 
 
22
  with open(formatter_path, "r") as f:
23
  source = f.read()
24
  for label in ['"header"', "'header'", '"footer"', "'footer'", '"doc_header"', "'doc_header'", '"doc_footer"', "'doc_footer'"]:
 
30
 
31
  ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def run_ocr(uploaded_file):
35
  if uploaded_file is None:
 
53
  result = parse(page_images)
54
  else:
55
  result = parse(path)
56
+
57
+ # ------ DEBUG: right after parse() ------
58
+ debug_lines = ["========== DEBUG =========="]
59
+ debug_lines.append(f"1. result type: {type(result).__name__}")
60
+ debug_lines.append(f"2. result length: {len(result) if isinstance(result, list) else 'N/A (single object)'}")
61
+ if isinstance(result, list) and len(result) > 0:
62
+ first = result[0]
63
+ debug_lines.append(f"3. first element type: {type(first).__name__}")
64
+ debug_lines.append(f"4. first element dir (attrs): {[x for x in dir(first) if not x.startswith('_')][:20]}")
65
+ debug_lines.append(f"5. has markdown_result: {hasattr(first, 'markdown_result')}")
66
+ debug_lines.append(f"6. has json_result: {hasattr(first, 'json_result')}")
67
+ if hasattr(first, "markdown_result"):
68
+ md = getattr(first, "markdown_result", None)
69
+ debug_lines.append(f"7. markdown_result type: {type(md).__name__}, len: {len(md) if md else 0}")
70
+ if md:
71
+ debug_lines.append(f"8. markdown_result snippet (first 200 chars): {repr((md or '')[:200])}")
72
+ if hasattr(first, "json_result"):
73
+ j = getattr(first, "json_result", None)
74
+ debug_lines.append(f"9. json_result type: {type(j).__name__}, len: {len(j) if j else 0}")
75
+ if j and len(j) > 0:
76
+ debug_lines.append(f"10. json_result[0] (first page) type: {type(j[0]).__name__}, len: {len(j[0]) if isinstance(j[0], list) else 'N/A'}")
77
+ if isinstance(j[0], list) and len(j[0]) > 0:
78
+ debug_lines.append(f"11. first region keys: {list(j[0][0].keys()) if isinstance(j[0][0], dict) else 'not a dict'}")
79
+ debug_lines.append("==========================")
80
+
81
+ if not isinstance(result, list):
82
+ result = [result]
83
+ if not result:
84
+ return "(No content)", "\n".join(debug_lines)
85
+
86
+ first = result[0]
87
+ has_markdown = getattr(first, "markdown_result", None) is not None
88
+ has_json = getattr(first, "json_result", None) is not None
89
+
90
+ if has_markdown or has_json:
91
+ markdown_parts = []
92
+ pages_data = []
93
+ for r in result:
94
+ md = getattr(r, "markdown_result", None) or ""
95
+ markdown_parts.append(md.strip() if md else "(empty page)")
96
+ j = getattr(r, "json_result", None) or []
97
+ for page in j:
98
+ pages_data.append(page)
99
+ markdown = "\n\n---\n\n".join(markdown_parts)
100
+
101
+ debug_lines.append(f"12. markdown_parts count: {len(markdown_parts)}, total markdown len: {len(markdown)}")
102
+ debug_lines.append(f"13. pages_data count: {len(pages_data)}")
103
+
104
+ total_headers = 0
105
+ total_footers = 0
106
+ all_summary = []
107
+ for page_num, page_regions in enumerate(pages_data):
108
+ if not isinstance(page_regions, list):
109
+ page_regions = [page_regions] if page_regions else []
110
+ page_summary = [f"── PAGE {page_num + 1} of {len(pages_data)} ──"]
111
+ for region in page_regions:
112
+ if not isinstance(region, dict):
113
+ continue
114
+ label = region.get("label", region.get("type", "text"))
115
+ content = str(region.get("content", region.get("text", "")))
116
+ if label in ABANDON:
117
+ continue
118
+ if label == "header":
119
+ total_headers += 1
120
+ page_summary.append(f"πŸ”΅ HEADER: {content[:100]}")
121
+ elif label == "footer":
122
+ total_footers += 1
123
+ page_summary.append(f"🟒 FOOTER: {content[:100]}")
124
+ else:
125
+ page_summary.append(f"[{label}]: {content[:100]}")
126
+ all_summary.extend(page_summary)
127
+ all_summary.append("")
128
+ summary_body = (
129
+ f"Total pages : {len(pages_data)}\n"
130
+ f"Headers found : {total_headers}\n"
131
+ f"Footers found : {total_footers}\n"
132
+ f"{'─'*40}\n" + "\n".join(all_summary)
133
+ )
134
+ summary = "\n".join(debug_lines) + "\n\n" + summary_body
135
+ return markdown, summary
136
+
137
  pages_data = result if isinstance(result, list) else []
138
  total_headers = 0
139
  total_footers = 0
140
  all_markdown = []
141
  all_summary = []
142
+ for page_num, page_regions in enumerate(pages_data):
 
143
  if not isinstance(page_regions, list):
144
+ page_regions = [page_regions] if page_regions else []
145
  page_md = []
146
  page_summary = [f"── PAGE {page_num + 1} of {len(pages_data)} ──"]
147
  for region in page_regions:
148
+ if not isinstance(region, dict):
 
 
149
  continue
150
+ label = region.get("label", "text")
151
+ content = str(region.get("content", ""))
152
  if label in ABANDON:
153
  continue
154
  if label == "header":
 
165
  all_markdown.append("\n\n".join(page_md))
166
  all_summary.extend(page_summary)
167
  all_summary.append("")
168
+ summary_body = (
169
  f"Total pages : {len(pages_data)}\n"
170
  f"Headers found : {total_headers}\n"
171
  f"Footers found : {total_footers}\n"
172
  f"{'─'*40}\n" + "\n".join(all_summary)
173
  )
174
  markdown = "\n\n---\n\n".join(all_markdown) if all_markdown else "(No content)"
175
+ summary = "\n".join(debug_lines) + "\n\n" + summary_body
176
  return markdown, summary
177
+
178
  except Exception as e:
179
  import traceback
180
  return f"Error: {str(e)}\n\n{traceback.format_exc()}", "Failed."
181
 
182
+
183
  with gr.Blocks(title="GLM-OCR β€” MaaS") as demo:
184
+ gr.Markdown("# πŸ” GLM-OCR β€” Zhipu MaaS\nUpload PDF or image. Headers πŸ”΅ and Footers 🟒 from the API.")
185
  file_input = gr.File(label="Upload PDF or Image", file_types=[".pdf", ".png", ".jpg", ".jpeg", ".tiff", ".bmp"])
186
  run_btn = gr.Button("β–Ά Run OCR", variant="primary", size="lg")
187
  with gr.Row():
 
189
  gr.Markdown("### πŸ“„ Markdown Output")
190
  markdown_out = gr.Textbox(lines=30, label="")
191
  with gr.Column():
192
+ gr.Markdown("### πŸ—‚οΈ Detected Regions (+ Debug)")
193
  regions_out = gr.Textbox(lines=30, label="")
194
  run_btn.click(fn=run_ocr, inputs=file_input, outputs=[markdown_out, regions_out])
195
  demo.launch()