""" JanSahayak Flask Web Application Beautiful UI for Multi-Agent Government Intelligence System """ from flask import Flask, render_template, request, jsonify, session, send_file import json import os from datetime import datetime from graph.workflow import run_workflow import uuid import io import re from reportlab.lib.pagesizes import letter, A4 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.units import inch from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak from reportlab.lib import colors from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_JUSTIFY app = Flask(__name__) app.secret_key = os.urandom(24) # For session management # Store active sessions sessions = {} # Global vectorstores (loaded on first use for faster startup) SCHEME_VECTORSTORE = None EXAM_VECTORSTORE = None VECTORSTORES_INITIALIZED = False # Check if running on a memory-constrained platform SKIP_VECTORSTORES = os.environ.get('SKIP_VECTORSTORES', 'false').lower() == 'true' def initialize_vectorstores(): """Load vectorstores lazily on first use to avoid blocking port binding""" global SCHEME_VECTORSTORE, EXAM_VECTORSTORE, VECTORSTORES_INITIALIZED if VECTORSTORES_INITIALIZED: return # Already initialized # Skip vectorstore loading on memory-constrained platforms (use web search only) if SKIP_VECTORSTORES: print("\n" + "="*70) print("⚔ LIGHTWEIGHT MODE: Skipping vectorstore loading") print("="*70) print("āœ… Using Tavily web search only (no embeddings model)") print("āœ… Low memory usage (<200MB)") print("āœ… Real-time, up-to-date information") print("="*70 + "\n") SCHEME_VECTORSTORE = None EXAM_VECTORSTORE = None VECTORSTORES_INITIALIZED = True return print("\n" + "="*70) print("šŸ“š Initializing Vector Stores (lazy loading)") print("="*70) # Load scheme vectorstore try: from rag.scheme_vectorstore import load_scheme_vectorstore SCHEME_VECTORSTORE = load_scheme_vectorstore() print("āœ… Scheme vectorstore loaded successfully") except Exception as e: print(f"āš ļø Scheme vectorstore not available: {str(e)}") print(" Will use web search only for schemes") SCHEME_VECTORSTORE = None # Load exam vectorstore try: from rag.exam_vectorstore import load_exam_vectorstore EXAM_VECTORSTORE = load_exam_vectorstore() print("āœ… Exam vectorstore loaded successfully") except Exception as e: print(f"āš ļø Exam vectorstore not available: {str(e)}") print(" Will use web search only for exams") EXAM_VECTORSTORE = None VECTORSTORES_INITIALIZED = True print("="*70 + "\n") def format_markdown(text): """Convert markdown-style text to HTML""" if not text or not isinstance(text, str): return text import re # Convert headers (### heading) text = re.sub(r'###\s+(.+?)(?=\n|$)', r'

\1

', text) text = re.sub(r'##\s+(.+?)(?=\n|$)', r'

\1

', text) # Convert bold (**text**) text = re.sub(r'\*\*(.+?)\*\*', r'\1', text) # Convert italic (*text*) text = re.sub(r'\*(.+?)\*', r'\1', text) # Convert bullet points (- item or * item) text = re.sub(r'^[\-\*]\s+(.+)$', r'
  • \1
  • ', text, flags=re.MULTILINE) text = re.sub(r'(
  • .*?
  • )', r'', text, flags=re.DOTALL) text = text.replace('\n