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
This commit is contained in:
Atharv1705
2026-08-05 11:41:06 +05:30
parent c2e22902c8
commit 9c7f25e06f
36 changed files with 3357 additions and 5279 deletions
+56
View File
@@ -0,0 +1,56 @@
"""
Re-run AI extraction for all documents (or only NULL ones).
Run from: services/api/
python backfill_extraction.py # only missing extractions
python backfill_extraction.py --all # re-extract everything with Qwen
"""
import sys, os, argparse
sys.path.insert(0, os.path.dirname(__file__))
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '..', '.env'))
from app.db import SessionLocal
from app.models import PersonalDocument, BusinessDocument
from app.utils.ai import extract_document_data
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("--all", action="store_true", help="Re-extract all docs, not just missing ones")
args = parser.parse_args()
BASE_DIR = Path(__file__).resolve().parents[2]
UPLOAD_DIR = BASE_DIR / "uploads"
db = SessionLocal()
def backfill(model_class, table_name, doc_type_field):
query = db.query(model_class).filter(model_class.deleted_at == None)
if not args.all:
query = query.filter(model_class.extracted_data == None)
docs = query.all()
label = "all" if args.all else "missing"
print(f"\n[{table_name}] Found {len(docs)} {label} docs to process")
for doc in docs:
file_path = str(UPLOAD_DIR / doc.storage_key)
doc_type = getattr(doc, doc_type_field)
print(f" → id={doc.id} type={doc_type[:40]} file={doc.storage_key[:20]}...")
if not os.path.exists(file_path):
print(f" ⚠ File not found on disk, skipping")
continue
result = extract_document_data(file_path, doc_type)
if result is not None:
doc.extracted_data = result # store {} as-is — empty means blank template, not failure
db.commit()
print(f" ✓ Extracted {len(result)} fields: {list(result.keys())}")
else:
print(f" ✗ Extraction failed (unsupported type, blank doc, or API error)")
backfill(PersonalDocument, "personal_documents", "doc_type")
backfill(BusinessDocument, "business_documents", "business_type")
db.close()
print("\nDone.")