Spaces:
Sleeping
Sleeping
Download routers/processing.py from Coeinterns/bank-statement-api: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/routers/processing.py
- Command line
-
hf download hf://spaces/Coeinterns/bank-statement-api/routers/processing.py
-
curl -L -o processing.py https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/routers/processing.py
4.81 kB
| import logging | |
| from typing import Optional, List | |
| from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks | |
| from auth.models import User | |
| from auth.service import get_current_user | |
| from models.statements import ProcessingState, ProcessingStage | |
| from core.state import statements | |
| from core.extraction import run_coordinate_extraction_task | |
| from core.statement_io import _persist_swap_to_xlsx | |
| logger = logging.getLogger("befree.processing") | |
| router = APIRouter(prefix="/api/v1/statements", tags=["processing"]) | |
| async def start_processing( | |
| statement_id: str, | |
| background_tasks: BackgroundTasks, | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| """Start processing: detect layout then immediately begin extraction (no HITL gate).""" | |
| if statement_id not in statements: | |
| raise HTTPException(status_code=404, detail="Statement not found") | |
| stmt = statements[statement_id] | |
| stmt.status = "detecting_layout" | |
| stmt.layout_detected_count = 0 | |
| stmt.extracted_count = 0 | |
| background_tasks.add_task(run_coordinate_extraction_task, statement_id) | |
| return {"statementId": statement_id, "status": "detecting_layout"} | |
| async def get_processing_state(statement_id: str, current_user: User = Depends(get_current_user)): | |
| """Get processing progress and coordinate layout detection status.""" | |
| if statement_id not in statements: | |
| raise HTTPException(status_code=404, detail="Statement not found") | |
| stmt = statements[statement_id] | |
| stages = [ | |
| ProcessingStage(key="read", label="Reading PDF", status="completed", progress=100), | |
| ProcessingStage(key="detect", label="Detecting Layout", status="pending", progress=0), | |
| ProcessingStage(key="extract", label="Extracting Transactions", status="pending", progress=0), | |
| ProcessingStage(key="audit", label="Running Audits", status="pending", progress=0), | |
| ProcessingStage(key="generate", label="Generating Workbook", status="pending", progress=0), | |
| ] | |
| overall = 0 | |
| estimated = None | |
| total_files = len(getattr(stmt, "pdf_paths", [1])) if getattr(stmt, "pdf_paths", None) else 1 | |
| if stmt.status == "uploaded": | |
| stages[0].status = "active" | |
| stages[0].progress = 50 | |
| overall = 10 | |
| elif stmt.status == "detecting_layout": | |
| stages[0].status = "completed" | |
| stages[0].progress = 100 | |
| stages[1].status = "active" | |
| detected = getattr(stmt, "layout_detected_count", 0) | |
| stages[1].progress = int((detected / max(total_files, 1)) * 100) | |
| overall = 10 + int((stages[1].progress / 100) * 15) | |
| estimated = max(10 - detected * 2, 2) | |
| elif stmt.status in ("awaiting_layout_review", "extracting"): | |
| stages[0].status = "completed" | |
| stages[0].progress = 100 | |
| stages[1].status = "completed" | |
| stages[1].progress = 100 | |
| stages[2].status = "active" | |
| extracted = getattr(stmt, "extracted_count", 0) | |
| stages[2].progress = int((extracted / max(total_files, 1)) * 100) | |
| overall = 25 + int((stages[2].progress / 100) * 35) | |
| estimated = max(20 - extracted * 4, 3) | |
| elif stmt.status in ("ready", "completed"): | |
| for s in stages: | |
| s.status = "completed" | |
| s.progress = 100 | |
| overall = 100 | |
| elif stmt.status == "failed": | |
| stages[0].status = "failed" | |
| overall = 0 | |
| return ProcessingState( | |
| statementId=statement_id, | |
| overallProgress=overall, | |
| estimatedSecondsRemaining=estimated, | |
| stages=stages, | |
| ocrEnabled=True, | |
| multiEngine=True, | |
| confidenceScoring=True, | |
| validationChecks=True, | |
| status=stmt.status, | |
| ) | |
| async def cancel_processing(statement_id: str, current_user: User = Depends(get_current_user)): | |
| """Cancel processing.""" | |
| if statement_id not in statements: | |
| raise HTTPException(status_code=404, detail="Statement not found") | |
| statements[statement_id].status = "cancelled" | |
| return None | |
| async def save_progress(statement_id: str, current_user: User = Depends(get_current_user)): | |
| """Save progress.""" | |
| if statement_id not in statements: | |
| raise HTTPException(status_code=404, detail="Statement not found") | |
| try: | |
| _persist_swap_to_xlsx(statement_id) | |
| except Exception as e: | |
| logger.error(f"Failed to save progress for statement {statement_id}: {e}") | |
| raise HTTPException(status_code=500, detail=f"Failed to save progress: {e}") | |
| return {"status": "success", "message": "Progress saved successfully"} | |