Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1269,6 +1269,46 @@ def augment_with_derived_balance_views(md: str) -> str:
|
|
| 1269 |
return md.rstrip() + "\n\n" + "\n\n".join(extra) + "\n"
|
| 1270 |
|
| 1271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1272 |
def render_pdf_pages_to_images(pdf_path: str) -> Tuple[List[str], List[int]]:
|
| 1273 |
import pymupdf as fitz
|
| 1274 |
from PIL import Image
|
|
@@ -1407,8 +1447,8 @@ def run_ocr(uploaded_file):
|
|
| 1407 |
if merged and merged != "(No content)" and not merged.lstrip().startswith("Error:"):
|
| 1408 |
merged = stabilize_table_markup(merged, rounds=4)
|
| 1409 |
merged = enrich_transaction_tables_with_daily_balances(merged)
|
| 1410 |
-
|
| 1411 |
-
merged =
|
| 1412 |
merged = stabilize_table_markup(merged, rounds=2)
|
| 1413 |
merged = repair_thead_cell_semantics(merged)
|
| 1414 |
merged = strip_degenerate_html_tables(merged)
|
|
|
|
| 1269 |
return md.rstrip() + "\n\n" + "\n\n".join(extra) + "\n"
|
| 1270 |
|
| 1271 |
|
| 1272 |
+
def prepend_balance_snapshot(md: str) -> str:
|
| 1273 |
+
"""
|
| 1274 |
+
Add a compact, explicit balance snapshot near the top so downstream metadata
|
| 1275 |
+
extraction consistently captures beginning/ending balances.
|
| 1276 |
+
"""
|
| 1277 |
+
if not md:
|
| 1278 |
+
return md
|
| 1279 |
+
if "balance snapshot" in md.lower():
|
| 1280 |
+
return md
|
| 1281 |
+
|
| 1282 |
+
start_bal, end_bal = _parse_statement_edge_balances(md)
|
| 1283 |
+
if start_bal is None and end_bal is None:
|
| 1284 |
+
# Try account-summary table fallback
|
| 1285 |
+
for tm in re.finditer(r"<table\b[^>]*>.*?</table>", md, flags=re.IGNORECASE | re.DOTALL):
|
| 1286 |
+
rows = _extract_rows_plain_from_table(tm.group(0))
|
| 1287 |
+
for r in rows:
|
| 1288 |
+
if len(r) < 2:
|
| 1289 |
+
continue
|
| 1290 |
+
key = (r[0] or "").strip().lower()
|
| 1291 |
+
val = _parse_amount_or_none(r[1] if len(r) > 1 else "")
|
| 1292 |
+
if val is None:
|
| 1293 |
+
continue
|
| 1294 |
+
if start_bal is None and ("beginning balance" in key or "starting balance" in key):
|
| 1295 |
+
start_bal = val
|
| 1296 |
+
if end_bal is None and "ending balance" in key:
|
| 1297 |
+
end_bal = val
|
| 1298 |
+
if start_bal is not None or end_bal is not None:
|
| 1299 |
+
break
|
| 1300 |
+
if start_bal is None and end_bal is None:
|
| 1301 |
+
return md
|
| 1302 |
+
|
| 1303 |
+
lines = ["## Balance Snapshot"]
|
| 1304 |
+
if start_bal is not None:
|
| 1305 |
+
lines.append(f"Beginning balance: {start_bal:,.2f}")
|
| 1306 |
+
if end_bal is not None:
|
| 1307 |
+
lines.append(f"Ending balance: {end_bal:,.2f}")
|
| 1308 |
+
header = "\n".join(lines)
|
| 1309 |
+
return header + "\n\n" + md
|
| 1310 |
+
|
| 1311 |
+
|
| 1312 |
def render_pdf_pages_to_images(pdf_path: str) -> Tuple[List[str], List[int]]:
|
| 1313 |
import pymupdf as fitz
|
| 1314 |
from PIL import Image
|
|
|
|
| 1447 |
if merged and merged != "(No content)" and not merged.lstrip().startswith("Error:"):
|
| 1448 |
merged = stabilize_table_markup(merged, rounds=4)
|
| 1449 |
merged = enrich_transaction_tables_with_daily_balances(merged)
|
| 1450 |
+
# Keep transaction amounts untouched; add explicit snapshot for metadata extraction.
|
| 1451 |
+
merged = prepend_balance_snapshot(merged)
|
| 1452 |
merged = stabilize_table_markup(merged, rounds=2)
|
| 1453 |
merged = repair_thead_cell_semantics(merged)
|
| 1454 |
merged = strip_degenerate_html_tables(merged)
|