How aryandhole594-art/nyayasetu works
1. NyayaSetu is a multi-module legal-assistant monorepo centered on Indian constitutional law. The core architecture is a Flask backend that exposes legal Q&A and tool APIs, a React frontend that consumes them, and several adjacent specialty modules for drafting, precedent prediction, and rights/compliance logic.
2. The primary runtime is backend/app.py (with app_remote.py for an alternate deployment path). This Flask server owns the main HTTP surface: /api/chat, the advanced reasoning endpoints, and document-intelligence endpoints. The routes are split into blueprints under backend/routes, which keeps the API boundary clean by feature area.
3. The main answer-generation path is retrieval-augmented. Queries flow through backend/rag_engine.py and backend/utils/retriever.py, which combine BM25, TF-IDF, and title boosting over the constitutional text and case corpus. Retrieved context is then passed to an LLM via backend/utils/llm_client.py, with backend/utils/hallucination_guard.py and backend/utils/explainability.py adding safety and traceability.
4. The backend’s “advanced legal tools” are implemented as modular feature services in backend/features. These include amendment tracking, article comparison, fairness checking, layman-to-legal translation, rights-card generation, and scenario simulation. Each tool is exposed through its own route blueprint and returns structured output, not just free-form text.
5. A key architectural boundary is the standardized response schema described in the README. Advanced endpoints return status, feature name, structured data, confidence, warnings, and explainability metadata such as retrieved chunks, sources, and reasoning steps. That makes downstream UI rendering and auditability consistent across tools.
6. Document intelligence is a separate pipeline under backend/document_intelligence.py and backend/legal_doc_intel.py. It supports file upload, OCR/text extraction, contract risk analysis, and court-notice decoding. The pipeline merges extracted content back into the legal corpus and uses grounding against constitutional/legal sources to reduce purely heuristic judgments.
7. The repository also contains a standalone precedent_module, which is a separate Flask app for precedent-based case prediction. Its index_cases.py, predictor.py, and evaluator.py suggest an isolated index-build-and-predict workflow, distinct from the main chatbot RAG flow. This module is likely meant to be deployed independently or integrated later.
8. drafting_module is another self-contained service focused on legal document generation. It has its own app, ingestion/processing/sanitization stages, a template library, and generated output directories. This is a separate boundary from the conversational assistant: it turns structured inputs into document templates rather than answering legal questions.
9. rights_module appears to be a rule/compliance subsystem. It includes a situation classifier, corpus loader, rights handler, and compliance checker, indicating a deterministic or semi-deterministic rights-assessment path that complements the LLM-based assistant. Architecturally, it functions like a domain rules engine for specific legal situations.
10. The frontend is a React app in frontend/ that serves the user-facing product experience. It includes pages and components for legal query handling, judgement prediction, drafting, and an “AI legal tools” experience. The frontend talks to the Flask backend over HTTP and is configured for local development on port 3000 against backend port 5555.
11. The floating NyayaSetu bot widget is a notable cross-cutting UI subsystem. Its JavaScript/CSS assets exist both under frontend/public/static/bot and backend/static/bot, which indicates the widget can be served from either the React app or Flask static hosting. The bot has two modes: Legal Assistant mode hits /api/chat, while Site Guide mode answers from a static site knowledge base.
12. The knowledge and corpus layer is file-based and domain-specific. backend/constitution.txt, backend/static/constitution.pdf, and backend/data/*.json provide constitutional and mapping data, while case_corpus/ contains a large collection of topic-labeled case texts used for retrieval and benchmarking. The corpus/ directory holds law PDFs used for ingestion/sample generation.
13. A few endpoints are intentionally stubbed and act as future integration points: document analyzer, petition draft, landmark cases, bail eligibility, and legal timeline return 501 until their retrieve→generate flow is implemented. This makes the current architecture a hybrid of production-ready tools and explicit extension seams.
14. Testing is focused on core behavioral boundaries rather than broad UI coverage. The tests/ directory validates bot intent classification, fairness checking, hallucination guarding, and retrieval quality, which matches the system’s main risk areas: routing, grounding, and answer reliability.
15. Operationally, the repo is designed for local-first deployment with optional external LLMs. The backend can use Ollama locally or Gemini as a fallback, and the README notes Tesseract for OCR. Those external dependencies matter because they affect the inference boundary, latency, and document-intelligence capabilities more than the basic app structure.
Main entry points: routes API routes · rag_engine.py RAG engine · retriever.py Retriever · constitution.txt Constitution
Components 21
Backend
Flask API, retrieval, and legal tools
- Flask app · Main Flask entrypoint for HTTP APIs and deployment wiring
backend/app.py - Remote app · Alternate Flask deployment path
backend/app_remote.py - API routes · Feature-split route layer for chat, tools, and stubs
backend/routes - RAG engine · Coordinates retrieval-augmented legal answering
backend/rag_engine.py - Retriever · BM25, TF-IDF, and title-boost retrieval over legal sources
backend/utils/retriever.py - LLM client · LLM access layer for local or external generation
backend/utils/llm_client.py - Safety layer · Hallucination checks and response explainability
backend/utils/hallucination_guard.py - Legal tools · Modular advanced legal tools exposed through dedicated endpoints
backend/features - Doc intel · Upload, OCR, extraction, and grounded legal document analysis
backend/document_intelligence.py - Legal intel · Contract risk and notice decoding logic
backend/legal_doc_intel.py
Frontend
React UI and bot widget
- React app · Main browser application for queries, tools, and prediction flows
frontend/src/App.js - Tool UI · UI for legal query, drafting, prediction, and advanced tools
frontend/src/components - Bot widget · Floating assistant with legal-assistant and site-guide modes
frontend/public/static/bot/nyayasetu_bot.js
Specialty Modules
Standalone legal services
- Precedent app · Standalone Flask service for precedent-based prediction
precedent_module/app.py - Case index · Builds case index and vectors for precedent lookup
precedent_module/index_cases.py - Predictor · Generates precedent-based case outcome predictions
precedent_module/predictor.py - Drafting app · Standalone legal document generation service
drafting_module/app.py - Draft pipeline · Ingestion, processing, and sanitization for template filling
drafting_module/processor.py - Rights engine · Situation classification and compliance checks for rights analysis
rights_module/rights_handler.py
Knowledge Base
Constitution, cases, and PDFs
- Constitution · Core constitutional text used across retrieval and tools
backend/constitution.txt - Case corpus · Topic-labeled case texts used for retrieval and benchmarking
case_corpus
Connections 20
- Flask app → API routes: registers · Main app mounts feature blueprints
- Remote app → API routes: registers · Alternate deployment path uses same API surface
- API routes → RAG engine: chat · Chat endpoint delegates to retrieval-augmented answering
- API routes → Legal tools: tools · Advanced endpoints dispatch to feature services
- API routes → Doc intel: document APIs · Document intelligence routes call analysis pipeline
- RAG engine → Retriever: retrieves · Pulls relevant constitutional and case context
- RAG engine → LLM client: generates · Sends grounded context to the model client
- RAG engine → Safety layer: guards · Applies hallucination checks and traceability
- Retriever → Constitution: grounds on · Uses constitutional text as a primary source
- Retriever → Case corpus: searches · Ranks case texts for supporting context
- Doc intel → Legal intel: analyzes · Document pipeline delegates legal-specific interpretation
- Legal tools → API routes: served by · Tool services are exposed through blueprints
- React app → Flask app: HTTP API · React UI consumes Flask backend endpoints
- Tool UI → React app: renders in · Tool pages live within the main React application
- Bot widget → API routes: chat mode · Legal Assistant mode calls the chat API
- Bot widget → Constitution: site guide · Site Guide mode uses static knowledge content
- Precedent app → Case index: indexes · Service builds precedent search structures
- Precedent app → Predictor: predicts · Prediction flow uses indexed cases and model logic
- Drafting app → Draft pipeline: processes · Drafting service runs ingestion and sanitization pipeline
- Rights engine → Constitution: interprets · Rights checks are grounded in legal source material