rehan953 commited on
Commit
621cdc6
·
verified ·
1 Parent(s): e707a60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -1
app.py CHANGED
@@ -207,6 +207,22 @@ def ocr_zone(image_path, y_start_frac, y_end_frac):
207
  return ""
208
 
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  def get_page_md_and_regions(page_result):
211
  md = ""
212
  if hasattr(page_result, "markdown_result") and page_result.markdown_result:
@@ -286,7 +302,7 @@ def run_ocr(uploaded_file):
286
  parts.append(hdr.strip())
287
 
288
  if page_md:
289
- parts.append(page_md)
290
 
291
  # Footer extraction removed — MaaS API already captures all footer
292
  # content correctly inside page_md. Extracting again causes duplication.
 
207
  return ""
208
 
209
 
210
+ def close_unclosed_html(md):
211
+ """Close any unclosed <table>, <tbody>, <tr> tags to prevent layout bleed."""
212
+ if not md:
213
+ return md
214
+ import re
215
+ open_tags = re.findall(r'<(table|tbody|thead|tr|td|th)[\s>]', md)
216
+ close_tags = re.findall(r'</(table|tbody|thead|tr|td|th)>', md)
217
+ # Count unclosed tags and close them in reverse order
218
+ for tag in reversed(['table', 'tbody', 'thead', 'tr', 'td', 'th']):
219
+ opened = open_tags.count(tag)
220
+ closed = close_tags.count(tag)
221
+ if opened > closed:
222
+ md += '</{}>'.format(tag) * (opened - closed)
223
+ return md
224
+
225
+
226
  def get_page_md_and_regions(page_result):
227
  md = ""
228
  if hasattr(page_result, "markdown_result") and page_result.markdown_result:
 
302
  parts.append(hdr.strip())
303
 
304
  if page_md:
305
+ parts.append(close_unclosed_html(page_md))
306
 
307
  # Footer extraction removed — MaaS API already captures all footer
308
  # content correctly inside page_md. Extracting again causes duplication.