Skip to content

Latest commit

 

History

History
450 lines (382 loc) · 20.2 KB

File metadata and controls

450 lines (382 loc) · 20.2 KB

🏛️ LEGALOS 4.0 (NYAYASAHAYAK) - COMPLETE MASTER DOCUMENT

Ultimate Reference & Backup Guide

Version: 4.0.0 | Last Updated: Complete Implementation


📚 TABLE OF CONTENTS

  1. Project Overview & Architecture
  2. Phase 0: Foundation (Backend)
  3. Phase 1: Frontend Integration
  4. Phase 2: Skills Implementation
  5. Phase 3: Security (SecureGuard)
  6. Phase 4: Testing Infrastructure
  7. Phase 5: Deployment

1. PROJECT OVERVIEW & ARCHITECTURE

1.1 Mission Statement

LegalOS 4.0 (NyayaSahayak) is an enterprise-grade, AI-powered judicial case management platform designed for the Indian legal system. It serves four primary personas: Police (Rakshak), Judge (Nyaya Mitra), Citizen (Vidhi Mitra), and Admin (Prabandhak).

1.2 System Architecture

┌─────────────────────────────────────────────────────────────────────────┐
│                         LEGALOS 4.0 ARCHITECTURE                        │
├─────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────────────────────────────────────────────────────┐   │
│  │                        FRONTEND LAYER                           │   │
│  │  React 18 + TypeScript + Vite + Tailwind CSS                    │   │
│  │  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌───────────┐ │   │
│  │  │   POLICE    │ │    JUDGE    │ │   CITIZEN   │ │   ADMIN   │ │   │
│  │  │ • Smart-FIR │ │ • Bench Memo│ │ • Doc Gen   │ │ • Registry│ │   │
│  │  │ • Financial │ │ • Bail      │ │ • Track Case│ │ • Listing │ │   │
│  │  │   Analyzer  │ │ • Judgment  │ │ • Nyayabot  │ │ • Docket  │ │   │
│  │  └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └─────┬─────┘ │   │
│  │         └────────────────┴────────────────┴──────────────┘      │   │
│  │              ┌───────────────────────────────────┐               │   │
│  │              │  TestDashboard / SecureMask / Auth │               │   │
│  │              └───────────────────────────────────┘               │   │
│  └─────────────────────────────────────────────────────────────────┘   │
│                            │ HTTPS / REST / JWT                        │
│  ┌─────────────────────────┴───────────────────────────────────────┐   │
│  │                        BACKEND LAYER                            │   │
│  │  FastAPI + Python 3.11 + SQLAlchemy + PostgreSQL + Redis        │   │
│  │  ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐   │   │
│  │  │ /police    │ │  /judge    │ │ /citizen   │ │  /admin    │   │   │
│  │  │ smart-fir  │ │ bench-memo │ │ documents  │ │ registry   │   │   │
│  │  │ financial  │ │ bail       │ │ templates  │ │ listing    │   │   │
│  │  │ evidence   │ │ judgment   │ │ generate   │ │ optimize   │   │   │
│  │  └─────┬──────┘ └─────┬──────┘ └─────┬──────┘ └─────┬──────┘   │   │
│  │        └────────────────┴────────────────┴─────────────┘        │   │
│  │           ┌─────────────────────────────────────┐               │   │
│  │           │    Security Layer (SecureGuard)      │               │   │
│  │           │  PII Redaction · JWT · RBAC · Audit  │               │   │
│  │           └─────────────────────────────────────┘               │   │
│  └─────────────────────────────────────────────────────────────────┘   │
│  ┌─────────────────────────────────────────────────────────────────┐   │
│  │  DATA: PostgreSQL 16 · Redis 7 · File Storage                   │   │
│  └─────────────────────────────────────────────────────────────────┘   │
└───────────────────────────────────────────────────────────────────────┘

1.3 Technology Stack

Layer Technology Purpose
Frontend React 18, TypeScript, Vite, Tailwind CSS UI
Backend FastAPI, Python 3.11, SQLAlchemy, Pydantic API
Database PostgreSQL 16, Redis 7 Storage & Cache
AI/ML spaCy, NetworkX, scikit-learn Analysis
DevOps Docker, Docker Compose, Nginx Deployment

1.4 Complete File Structure

backend/
├── app/
│   ├── main.py
│   ├── api/v1/
│   │   ├── router.py
│   │   ├── deps.py
│   │   └── endpoints/ (auth, police, judge, citizen, admin, financial, registry, listing, judgment)
│   ├── core/ (config, security, auth, pii_redactor, middleware)
│   ├── models/ (user, fir, bns_section, bail_application, document_template)
│   ├── schemas/ (user, fir, bail, document, financial, registry, listing, judgment)
│   ├── services/ (smart_fir, bail_reckoner, document_generator, financial_service,
│   │              registry_service, listing_service, judgment_service, bench_memo)
│   └── db/ (base, session)
│
frontend/src/
├── core/
│   ├── services/ (api, auth, police, judge, citizen, admin, financial, registry, listing, judgment)
│   ├── types/ (police, judge, citizen, admin, financial, registry, listing, judgment)
│   └── utils/security.ts
├── personas/ (police, judge, citizen, admin — each with pages/ and components/)
├── shared/components/ (3d, optics/SecureMask, layout, graph)
├── pages/TestDashboard.tsx
├── tests/manual/ (per-skill test files)
└── App.tsx

2. PHASE 0: FOUNDATION (BACKEND)

2.1 Core Configuration (backend/app/core/config.py)

from pydantic_settings import BaseSettings
from functools import lru_cache

class Settings(BaseSettings):
    APP_NAME: str = "LegalOS 4.0"
    DEBUG: bool = False
    VERSION: str = "4.0.0"
    SECRET_KEY: str
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    ALGORITHM: str = "HS256"
    DATABASE_URL: str = "postgresql://user:password@localhost/legalos"
    REDIS_URL: str = "redis://localhost:6379/0"
    class Config:
        env_file = ".env"

@lru_cache()
def get_settings():
    return Settings()
settings = get_settings()

2.2 Authentication (backend/app/core/auth.py)

from datetime import datetime, timedelta
from jose import JWTError, jwt
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

def create_access_token(data: dict, expires_delta: timedelta = None):
    to_encode = data.copy()
    expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
    to_encode.update({"exp": expire})
    return jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)

def verify_token(token: str) -> dict:
    try:
        return jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
    except JWTError:
        return None

2.3 Database Models

# backend/app/models/user.py
class UserRole(str, enum.Enum):
    POLICE = "POLICE"; JUDGE = "JUDGE"; CITIZEN = "CITIZEN"; ADMIN = "ADMIN"

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    full_name = Column(String)
    role = Column(Enum(UserRole), default=UserRole.CITIZEN)
    is_active = Column(Boolean, default=True)

3. PHASE 1: FRONTEND INTEGRATION

3.1 API Client (frontend/src/core/services/api.ts)

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000/api/v1';

export const apiClient = async <T>(endpoint: string, options: RequestInit = {}): Promise<ApiResponse<T>> => {
    const token = localStorage.getItem('accessToken');
    const response = await fetch(`${API_BASE_URL}${endpoint}`, {
        ...options,
        headers: { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : '', ...options.headers },
    });
    const data = await response.json();
    return { success: response.ok, data: response.ok ? data : undefined, error: !response.ok ? data.detail : undefined };
};

export const policeApi = {
    smartFIR: { generate: (data) => apiClient('/police/smart-fir/generate', { method: 'POST', body: JSON.stringify(data) }) },
    financial: { analyze: (data) => apiClient('/police/financial/analyze', { method: 'POST', body: JSON.stringify(data) }) },
};
export const judgeApi = {
    bail: { analyze: (data) => apiClient('/judge/bail/analyze', { method: 'POST', body: JSON.stringify(data) }) },
    benchMemo: { generate: (data) => apiClient('/judge/bench-memo/generate', { method: 'POST', body: JSON.stringify(data) }) },
    judgment: { validate: (data) => apiClient('/judge/judgment/validate', { method: 'POST', body: JSON.stringify(data) }) },
};
export const citizenApi = {
    documents: { listTemplates: () => apiClient('/citizen/documents/templates'),
                 generate: (data) => apiClient('/citizen/documents/generate', { method: 'POST', body: JSON.stringify(data) }) },
};
export const adminApi = {
    registry: { scrutinize: (data) => apiClient('/admin/registry/scrutinize', { method: 'POST', body: JSON.stringify(data) }),
                calculateFees: (data) => apiClient('/admin/registry/calculate-fees', { method: 'POST', body: JSON.stringify(data) }) },
    listing: { optimize: (data) => apiClient('/admin/listing/optimize', { method: 'POST', body: JSON.stringify(data) }),
               getPendingCases: (courtId) => apiClient(`/admin/listing/pending-cases/${courtId}`) },
};

4. PHASE 2: SKILLS IMPLEMENTATION

SKILL 1: Smart-FIR (Police / Rakshak)

# backend/app/schemas/fir.py
class SectionSuggestion(BaseModel):
    code: str; description: str; confidence: float; severity: str

class SmartFIRResponse(BaseModel):
    fir_id: int; complaint_text: str; extracted_entities: dict
    bns_sections: List[SectionSuggestion]; confidence_score: float; draft_fir: str; status: str

# backend/app/services/smart_fir.py
class SmartFIRService:
    def generate_fir(self, complaint_text: str):
        entities = self.extract_entities(complaint_text)  # NLP entity extraction
        sections = self.map_to_bns(complaint_text)         # Vector similarity matching
        return {"fir_id": 123, "complaint_text": complaint_text, "extracted_entities": entities,
                "bns_sections": sections, "confidence_score": 0.85,
                "draft_fir": self.generate_draft(complaint_text, sections), "status": "draft"}

SKILL 2: Financial Trail Analyzer (Police / Rakshak)

# backend/app/services/financial_service.py
import networkx as nx

class FinancialAnalyzer:
    def analyze_trail(self, transactions):
        G = nx.DiGraph()
        for txn in transactions:
            G.add_edge(txn.from_account, txn.to_account, weight=txn.amount)
        cycles = list(nx.simple_cycles(G))  # Detect circular trading
        # Also detects: layering, high value transfers, rapid succession, unusual hours
        return {"graph": self.build_graph(transactions), "alerts": self.anomalies, "metrics": self.calculate_metrics(transactions)}

SKILL 8: Bench Memo Generator (Judge / Nyaya Mitra)

# backend/app/schemas/bench_memo.py
class BenchMemoAnalysis(BaseModel):
    facts_summary: str; legal_issues: List[str]; applicable_laws: List[str]
    precedents: List[dict]; arguments_petitioner: List[str]; arguments_respondent: List[str]
    potential_outcomes: List[str]; recommended_orders: List[str]

SKILL 9: Bail Reckoner (Judge / Nyaya Mitra)

# backend/app/schemas/bail.py
class BailAnalysisResponse(BaseModel):
    antil_category: str  # A, B, C, D
    eligibility_status: str  # FAVORABLE, NEUTRAL, UNFAVORABLE
    risk_scores: dict; mitigating_factors: List[str]

SKILL 16: Document Generator (Citizen / Vidhi Mitra)

# backend/app/schemas/document.py
class DocumentTemplate(BaseModel):
    code: str; name: str; required_fields: List[str]; template_content: str

class GeneratedDocument(BaseModel):
    id: int; template_code: str; output_content: str; stamp_duty_calculated: float

SKILL 19: Registry Automator (Admin / Prabandhak)

# backend/app/schemas/registry.py
class Defect(BaseModel):
    id: str; severity: DefectSeverity; description: str; section_reference: str; suggestion: str

class ScrutinyResponse(BaseModel):
    filing_id: str; status: str; defect_count: int; defects_found: List[Defect]; ai_summary: str

class FeeBreakdown(BaseModel):
    base_fee: float; value_based_fee: float; total_fee: float; max_fee_applied: bool

# backend/app/services/registry_service.py
class RegistryService:
    def scrutinize_document(self, document_url):
        # AI checks: court fees, stamps, indexing, format compliance, vakalatnama
        pass
    def calculate_fees(self, filing_type, value):
        # Multi-tiered: base + ad-valorem (capped at 1L) + additional
        pass

SKILL 20: Listing Optimizer (Admin / Prabandhak)

# backend/app/schemas/listing.py
class CaseListing(BaseModel):
    id: str; case_number: str; title: str; case_type: CaseType
    stage: CaseStage; priority: CasePriority; estimated_minutes: int

class OptimizedSchedule(BaseModel):
    date: str; court_id: str; total_cases: int; total_minutes_scheduled: int
    utilization_percentage: float; schedule: List[ScheduledSlot]
    unlisted_cases: List[CaseListing]; breaks: List[TimeSlot]

# backend/app/services/listing_service.py — First Fit Decreasing Bin Packing
class ListingService:
    def optimize_schedule(self, request):
        # Sort by priority score, schedule within 330-min (5.5h) judicial day
        # Handles lunch break (13:00-14:00), priority weighting, adjournment boost
        pass

SKILL 21: Judgment Validator (Judge / Nyaya Mitra)

# backend/app/schemas/judgment.py
class ValidationIssue(BaseModel):
    severity: str; category: str; description: str; suggested_fix: str

class JudgmentValidationResponse(BaseModel):
    total_citations: int; citations_valid: int; citations_overruled: int
    validation_issues: List[ValidationIssue]; missing_precedents: List[dict]

# backend/app/services/judgment_service.py
class JudgmentValidator:
    def validate_judgment(self, draft_text, case_type):
        citations = self._extract_citations(draft_text)   # Regex: AIR YYYY SC/HC XXXX
        # Check good law status, logical consistency, missing precedents
        pass

5. PHASE 3: SECURITY (SecureGuard)

# backend/app/core/pii_redactor.py
class PIIRedactor:
    patterns = {
        'aadhaar': (r'\d{4}-\d{4}-\d{4}', 'XXXX-XXXX-XXXX'),
        'pan': (r'[A-Z]{5}\d{4}[A-Z]', 'XXXXX-XXXX-X'),
        'phone': (r'\+91-\d{5}-\d{5}', 'XXXXX-XXXXX'),
        'email': (r'[\w\.-]+@[\w\.-]+', 'xxxx@xxxx.com'),
    }
    def redact(self, text): # Apply all patterns
// frontend/src/core/utils/security.ts
export const maskPII = (value: string, type: 'aadhaar' | 'phone' | 'email'): string => { ... }

// frontend/src/shared/components/optics/SecureMask.tsx
// Toggle-able PII mask component with 👁 reveal button

6. PHASE 4: TESTING INFRASTRUCTURE

Test Dashboard (src/pages/TestDashboard.tsx)

  • Police Module: Test Smart-FIR, Test Financial Analyzer
  • Judge Module: Test Bench Memo, Test Bail Reckoner, Test Judgment Validator
  • Admin Module: Test Registry Automator, Test Listing Optimizer
  • Security: Test SecureGuard PII

Access at: http://localhost:5173/test


7. PHASE 5: DEPLOYMENT

Docker Compose (docker-compose.yml)

version: '3.8'
services:
  backend:
    build: ./backend
    ports: ["8000:8000"]
    environment:
      - DATABASE_URL=postgresql://user:pass@postgres/db
      - REDIS_URL=redis://redis:6379
    depends_on: [postgres, redis]
  frontend:
    build: ./frontend
    ports: ["80:80"]
    depends_on: [backend]
  postgres:
    image: postgres:16
    environment: { POSTGRES_USER: user, POSTGRES_PASSWORD: pass, POSTGRES_DB: legalos }
  redis:
    image: redis:7

Quick Start

# Backend
cd d:/Project/nationals/backend
pip install -r requirements.txt
uvicorn app.main:app --reload

# Frontend
cd d:/Project/nationals/nyayasahayak-main-main
npm install && npm run dev

# Test
curl http://localhost:8000/health
open http://localhost:5173/test

API Endpoints Reference

Endpoint Method Skill
/api/v1/police/smart-fir/generate POST Skill 1
/api/v1/police/financial/analyze POST Skill 2
/api/v1/judge/bench-memo/generate POST Skill 8
/api/v1/judge/bail/analyze POST Skill 9
/api/v1/citizen/documents/generate POST Skill 16
/api/v1/admin/registry/scrutiny POST Skill 19
/api/v1/admin/registry/calculate-fees POST Skill 19
/api/v1/admin/listing/optimize POST Skill 20
/api/v1/admin/listing/court/{id}/pending-cases GET Skill 20
/api/v1/judge/judgment/validate POST Skill 21

📊 PROJECT STATISTICS

Metric Value
Skills Implemented 8
Total Files 80+
Lines of Code ~15,000+
Test Coverage All skills testable
Personas 4 (Police, Judge, Citizen, Admin)
Security DPDP Act 2023 Compliant

✅ Skills Summary

  1. ✅ Smart-FIR (Police) — AI crime reporting with BNS 2023 mapping
  2. ✅ Financial Trail Analyzer (Police) — NetworkX graph + anomaly detection
  3. ✅ Bench Memo Generator (Judge) — AI 2-page summaries
  4. ✅ Bail Reckoner (Judge) — Antil category + risk scoring
  5. ✅ Document Generator (Citizen) — Template-based legal docs
  6. ✅ Registry Automator (Admin) — AI scrutiny + fee calculation
  7. ✅ Listing Optimizer (Admin) — Bin packing scheduler
  8. ✅ Judgment Validator (Judge) — Good law verification
  9. ✅ SecureGuard PII Protection — DPDP compliant redaction

Status: READY FOR PILOT DEPLOYMENT 🚀