Files
AI_CPA/services/api/app/main.py
T
Atharv1705 9c7f25e06f feat: major platform upgrade — AI extraction pipeline, responsive UI, admin chatbot enhancements
CRITICAL FIXES
- Fix blank pages: useCallback declared after useEffect in all 5 pages
- Fix showToast undefined everywhere (ToastContext exported addToast not showToast)
- Fix admin doc upload always 422 (missing doc_key + doc_label form fields)
- Fix audit log wrong field names (log.details -> log.detail, remove user_agent)
- Fix SPA catch-all missing in main.py (direct URL access was 404)
- Fix chatbot using broken nvidia free model -> llama-3.3-70b

AI EXTRACTION PIPELINE
- 3-stage pipeline: Stage1 native text (free) -> Stage2 Tesseract OCR (free CPU) -> Stage3 Qwen vision (paid)
- Tesseract installed with eng+hin bilingual support, psm6, 2.5x DPI
- Scanned PDF fallback via PyMuPDF page render
- Blank template fallback: returns form_type/purpose/tax_year instead of empty
- Improved prompt: extracts actual document field names, no predefined terms
- Auto-poll UI: shows Processing spinner when extraction is running
- backfill_extraction.py: re-process all docs, fixed empty-dict-as-None bug

ADMIN DASHBOARD
- Pending docs split into Pending Personal + Pending Business stat cards
- User cards show typed badges: N personal / N business instead of generic N pending
- Backend returns pending_personal, pending_business, pending_docs per user

ADMIN USER DETAIL
- Doc count chips in user info bar: personal / business / returns / total
- Doc name bold 15px, date with calendar icon + TY chip, status as colored badges
- Responsive layout: doc-review-layout CSS classes replace inline flex styles
- Filing Timeline sidebar stacks above doc list on mobile
- AI Summary: bold labels and values, blank template shows metadata

CHATBOT ENHANCEMENTS
- Ask anything free-text input on menu screen (both admin and user chatbots)
- Admin: live context with latest uploads by date, new users today
- Admin: per-user activity lookup by name/email (docs + audit log)
- New endpoint: /api/chatbot/admin-user-context
- ExpandableSummaryBlock + ExpandableValue: bold navy field labels and values

RESPONSIVENESS
- components.css: 480px/640px/768px breakpoints for all pages
- Admin table, stat cards, tabs, toasts, FAB all properly responsive
- Auth shell stacks on mobile correctly
2026-08-05 11:41:06 +05:30

155 lines
5.8 KiB
Python

import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from app.limiter import limiter
from app import models
from app.db import engine
log = logging.getLogger("uvicorn.error")
app = FastAPI(title="BookKeepPro API")
# Rate limiter — import from limiter.py to avoid circular imports
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# Create DB tables on startup (new tables only — does not alter existing)
models.Base.metadata.create_all(bind=engine)
# ─────────────────────────────────────────────
# Routers — each isolated so one failure never kills the others
# ─────────────────────────────────────────────
try:
from app.routers import auth # type: ignore
app.include_router(auth.router)
except Exception as exc:
log.exception("Failed to load auth router: %s", exc)
try:
from app.routers import upload # type: ignore
app.include_router(upload.router)
except Exception as exc:
log.exception("Failed to load upload router: %s", exc)
try:
from app.routers import contact # type: ignore
app.include_router(contact.router)
except Exception as exc:
log.exception("Failed to load contact router: %s", exc)
try:
from app.routers import review # type: ignore
app.include_router(review.router)
except Exception as exc:
log.exception("Failed to load review router: %s", exc)
try:
from app.routers import chatbot # type: ignore
app.include_router(chatbot.router)
except Exception as exc:
log.exception("Failed to load chatbot router: %s", exc)
# ─────────────────────────────────────────────
# CORS
# ─────────────────────────────────────────────
_ALLOWED_ORIGINS = [
o.strip()
for o in os.getenv(
"ALLOWED_ORIGINS",
"https://bookkeepro.net,http://localhost:8000",
).split(",")
if o.strip()
]
app.add_middleware(
CORSMiddleware,
allow_origins=_ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ─────────────────────────────────────────────
# Static file mounts
# ─────────────────────────────────────────────
FRONTEND_DIR = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "..", "..", "frontend")
)
def mount_if_exists(route: str, subdir: str, name: str):
path = os.path.join(FRONTEND_DIR, subdir)
if os.path.isdir(path):
app.mount(route, StaticFiles(directory=path), name=name)
log.info("Mounted static %s -> %s", route, path)
else:
log.debug("Static directory not found, skipping: %s", path)
mount_if_exists("/js", "dist/js", "js")
mount_if_exists("/images", "dist/images", "images")
mount_if_exists("/css", "dist/css", "css")
# Mount Vite build assets (JS bundles, hashed chunks)
DIST_ASSETS = os.path.join(FRONTEND_DIR, "dist", "assets")
if os.path.isdir(DIST_ASSETS):
app.mount("/assets", StaticFiles(directory=DIST_ASSETS), name="assets")
log.info("Mounted Vite assets -> %s", DIST_ASSETS)
UPLOAD_DIR = os.path.abspath(os.path.join(FRONTEND_DIR, "..", "uploads"))
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR, exist_ok=True)
log.info("FRONTEND_DIR resolved to: %s", FRONTEND_DIR)
log.info("CSS dir exists: %s", os.path.isdir(os.path.join(FRONTEND_DIR, "css")))
DIST_DIR = os.path.join(FRONTEND_DIR, "dist")
SPA_INDEX = os.path.join(DIST_DIR, "index.html")
# Mount the Vite dist folder for hashed static assets not already mounted above
# (favicon, icons.svg, etc.)
if os.path.isdir(DIST_DIR):
# Serve root-level static assets that Vite places at dist/
app.mount("/dist", StaticFiles(directory=DIST_DIR), name="dist_root")
# ─────────────────────────────────────────────
# Ops endpoints
# ─────────────────────────────────────────────
@app.get("/health", tags=["ops"])
def health():
"""Health check for monitoring and load balancers."""
return {"status": "ok"}
@app.post("/logout", tags=["auth"])
def logout():
# JWT is stateless — token is invalidated client-side by clearing localStorage
return {"message": "Logged out"}
# ─────────────────────────────────────────────
# SPA catch-all — serve index.html for every non-API GET route
# so React Router handles client-side navigation on direct URL access
# ─────────────────────────────────────────────
@app.get("/{full_path:path}", tags=["frontend"])
def spa_fallback(full_path: str):
if os.path.isfile(SPA_INDEX):
return FileResponse(
SPA_INDEX,
headers={
"Cache-Control": "no-store, no-cache, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
},
)
raise HTTPException(status_code=404, detail="Frontend not built. Run 'npm run build' in /frontend.")