Spaces:
Sleeping
Sleeping
Rudra commited on
Commit ·
7aa828a
1
Parent(s): 3dfe050
Return failure reason and detected bank on 422s
Browse filesreason (unreadable / scan_no_rows / no_rows / not_statement), bank from a fixed
list, text-layer and header flags. No statement content is returned.
- app/api/convert.py +51 -9
app/api/convert.py
CHANGED
|
@@ -6,6 +6,7 @@ dir, and deleted before the response is built. Nothing is persisted.
|
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
import io
|
|
|
|
| 9 |
import logging
|
| 10 |
|
| 11 |
from fastapi import APIRouter, File, Header, HTTPException, Query, UploadFile
|
|
@@ -67,6 +68,45 @@ def _xlsx_bytes(statements: list[ExtractedStatement]) -> io.BytesIO:
|
|
| 67 |
return buf
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
@router.post("/convert")
|
| 71 |
async def convert(
|
| 72 |
file: UploadFile = File(...),
|
|
@@ -85,6 +125,7 @@ async def convert(
|
|
| 85 |
mb = MAX_UPLOAD_BYTES // (1024 * 1024)
|
| 86 |
raise HTTPException(status_code=413, detail=f"File too large (max {mb} MB).")
|
| 87 |
|
|
|
|
| 88 |
try:
|
| 89 |
with temp_pdf(data) as pdf_path:
|
| 90 |
conv = BankStatementConverter(pdf_path)
|
|
@@ -93,23 +134,24 @@ async def convert(
|
|
| 93 |
# Bad user input (corrupt / password-protected / not a real PDF) must
|
| 94 |
# surface as 4xx, never a 500. Logged for observability.
|
| 95 |
logger.exception("extraction failed for upload %r", file.filename)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
)
|
| 100 |
|
| 101 |
has_txns = any(s.transactions for s in statements)
|
| 102 |
if not has_txns and not conv.text.strip():
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
detail="Couldn't read any transactions from this scan. A clearer, straight-on scan "
|
| 106 |
"or the bank's original PDF usually works.",
|
|
|
|
| 107 |
)
|
| 108 |
if not has_txns or not conv.looks_like_statement():
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
"transactions with amounts and a balance. Please upload the statement PDF from your bank.",
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
if format == "xlsx":
|
|
|
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
import io
|
| 9 |
+
import re
|
| 10 |
import logging
|
| 11 |
|
| 12 |
from fastapi import APIRouter, File, Header, HTTPException, Query, UploadFile
|
|
|
|
| 68 |
return buf
|
| 69 |
|
| 70 |
|
| 71 |
+
# --- failure diagnostics -------------------------------------------------------
|
| 72 |
+
# When a conversion fails, the web app records WHY (a short code) and WHICH BANK
|
| 73 |
+
# (matched against a fixed list), so the common failures can be fixed. Nothing
|
| 74 |
+
# from the statement itself — no names, numbers or transactions — is returned.
|
| 75 |
+
_BANKS = [
|
| 76 |
+
("SBI", r"state bank of india|\bsbi\b"), ("HDFC", r"hdfc bank"), ("ICICI", r"icici bank"),
|
| 77 |
+
("Axis", r"axis bank"), ("Kotak", r"kotak mahindra"), ("PNB", r"punjab national bank"),
|
| 78 |
+
("Bank of Baroda", r"bank of baroda"), ("Canara", r"canara bank"), ("Union Bank", r"union bank of india"),
|
| 79 |
+
("Bank of India", r"\bbank of india\b"), ("IDFC First", r"idfc first"), ("IndusInd", r"indusind"),
|
| 80 |
+
("Yes Bank", r"\byes bank\b"), ("Federal Bank", r"federal bank"), ("IDBI", r"\bidbi\b"),
|
| 81 |
+
("Indian Bank", r"\bindian bank\b"), ("Central Bank of India", r"central bank of india"),
|
| 82 |
+
("Indian Overseas", r"indian overseas bank"), ("UCO", r"\buco bank\b"), ("Karnataka Bank", r"karnataka bank"),
|
| 83 |
+
("South Indian Bank", r"south indian bank"), ("RBL", r"\brbl bank\b"), ("AU SFB", r"au small finance"),
|
| 84 |
+
("Standard Chartered", r"standard chartered"), ("Paytm Payments Bank", r"paytm payments bank"),
|
| 85 |
+
("Barclays", r"barclays"), ("HSBC", r"\bhsbc\b"), ("Lloyds", r"\blloyds\b"), ("NatWest", r"natwest"),
|
| 86 |
+
("Santander", r"santander"), ("Nationwide", r"nationwide"), ("Monzo", r"\bmonzo\b"), ("Starling", r"starling"),
|
| 87 |
+
("Halifax", r"halifax"), ("Revolut", r"revolut"), ("Chase", r"\bchase\b"), ("Bank of America", r"bank of america"),
|
| 88 |
+
("Wells Fargo", r"wells fargo"), ("Citi", r"\bcitibank\b|\bciti\b"), ("Capital One", r"capital one"),
|
| 89 |
+
("CommBank", r"commonwealth bank|commbank"), ("ANZ", r"\banz\b"), ("Westpac", r"westpac"), ("NAB", r"national australia bank"),
|
| 90 |
+
("AIB", r"allied irish|\baib\b"), ("Bank of Ireland", r"bank of ireland"),
|
| 91 |
+
]
|
| 92 |
+
_HEADER_RE = re.compile(r"\bdate\b.*\b(balance|amount|debit|credit|withdraw\w*|deposit\w*|dr|cr)\b", re.I)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _diagnose(conv: "BankStatementConverter | None", reason: str) -> dict:
|
| 96 |
+
text = (conv.text if conv else "") or ""
|
| 97 |
+
low = text.lower()
|
| 98 |
+
bank = next((name for name, pat in _BANKS if re.search(pat, low)), "unknown")
|
| 99 |
+
chars = len(text.strip())
|
| 100 |
+
layer = "none" if chars == 0 else ("thin" if chars < 400 else "text")
|
| 101 |
+
header = bool(_HEADER_RE.search(text))
|
| 102 |
+
return {"reason": reason, "bank": bank, "layer": layer, "header": header}
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def _fail(detail: str, diag: dict) -> JSONResponse:
|
| 106 |
+
logger.info("convert failed: %s", diag)
|
| 107 |
+
return JSONResponse(status_code=422, content={"detail": detail, **diag})
|
| 108 |
+
|
| 109 |
+
|
| 110 |
@router.post("/convert")
|
| 111 |
async def convert(
|
| 112 |
file: UploadFile = File(...),
|
|
|
|
| 125 |
mb = MAX_UPLOAD_BYTES // (1024 * 1024)
|
| 126 |
raise HTTPException(status_code=413, detail=f"File too large (max {mb} MB).")
|
| 127 |
|
| 128 |
+
conv = None
|
| 129 |
try:
|
| 130 |
with temp_pdf(data) as pdf_path:
|
| 131 |
conv = BankStatementConverter(pdf_path)
|
|
|
|
| 134 |
# Bad user input (corrupt / password-protected / not a real PDF) must
|
| 135 |
# surface as 4xx, never a 500. Logged for observability.
|
| 136 |
logger.exception("extraction failed for upload %r", file.filename)
|
| 137 |
+
return _fail(
|
| 138 |
+
"Couldn't read this PDF. It may be corrupt, password-protected, or not a valid PDF.",
|
| 139 |
+
_diagnose(conv, "unreadable"),
|
| 140 |
)
|
| 141 |
|
| 142 |
has_txns = any(s.transactions for s in statements)
|
| 143 |
if not has_txns and not conv.text.strip():
|
| 144 |
+
return _fail(
|
| 145 |
+
"Couldn't read any transactions from this scan. A clearer, straight-on scan "
|
|
|
|
| 146 |
"or the bank's original PDF usually works.",
|
| 147 |
+
_diagnose(conv, "scan_no_rows"),
|
| 148 |
)
|
| 149 |
if not has_txns or not conv.looks_like_statement():
|
| 150 |
+
diag = _diagnose(conv, "no_rows" if not has_txns else "not_statement")
|
| 151 |
+
return _fail(
|
| 152 |
+
"This doesn't look like a bank statement. We couldn't find a list of dated "
|
| 153 |
"transactions with amounts and a balance. Please upload the statement PDF from your bank.",
|
| 154 |
+
diag,
|
| 155 |
)
|
| 156 |
|
| 157 |
if format == "xlsx":
|