# pixeler5diti/delhi-high-court-virtual-judge architecture

> Architecture diagram of the GitHub repository pixeler5diti/delhi-high-court-virtual-judge, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pixeler5diti/delhi-high-court-virtual-judge
- Repository: https://github.com/pixeler5diti/delhi-high-court-virtual-judge

## Overview

##### Project Type and Purpose
- This is a full-stack, AI-powered legal analysis and judgment prediction system.
- It leverages advanced NLP (with LegalBERT) for processing legal documents and uses vector similarity search (via FAISS) to match legal precedents.
- The system also provides a user interface built in Streamlit to allow legal professionals to upload documents and view analysis and predictions.

##### File Structure
- Top-Level Files and Scripts:
- app.py: The main Streamlit web app that acts as the frontend interface.
- data_downloader.py, run_training.py, train_model.py: Scripts for data acquisition and model training.
- setup_local.py and others indicate local deployment setups.
- Folders:
- data: Contains raw and processed legal documents, plus pre-computed FAISS indices and metadata.
- model: Holds modules related to embedding generation (embedding_utils.py), training (model_trainer.py), and model inference (use_trained_model.py).
- utils: Contains helper utilities for loading data, predicting judgments, processing PDF documents, integrating OpenAI’s GPT-4o, vector storage, and visualization.
- Configuration files such as LICENSE, pyproject.toml, replit.nix, requirements_local.txt indicate environment/deployment and dependency management.

##### Key Architectural Components and Their Relationships
a. Document Processing Module:
- Components: PDF processing utilities (pdf_processor.py in utils) and Data Loader (data_loader.py).
- Flow: Incoming documents are loaded and preprocessed (e.g., text extraction from PDFs) before being analyzed.

b. Vector Embedding and Similarity Search Module:
- Components: embedding_utils.py (converts texts into high-dimensional legal vectors) and vector_store.py (in utils) for storing and retrieving embeddings.
- Flow: Processed text gets converted into embeddings via LegalBERT; these vectors are stored in a FAISS index to enable similarity search against historical legal cases.

c. Judgment Prediction Engine:
- Components: judgment_predictor.py (contains the logic for linking extracted data, similar cases, and making predictions) and use_trained_model.py (for making use of trained embedding models).
- Flow: Based on similarities and learned patterns, predicted judgment outcomes are generated.

d. Legal Analysis Generator:
- Components: Possibly a blend of the judgment_predictor and additional transformer-based processing (optionally enhanced with OpenAI integration via openai_integration.py).
- Flow: Generates comprehensive legal analysis with reasoning, case summaries, liability determination, and recommended remedies.

e. Model Training Module:
- Components: train_model.py, run_training.py, and model_trainer.py.
- Flow: Facilitates periodic retraining and fine-tuning using updated legal data.

f. User Interface (Frontend):
- Components: app.py that implements a Streamlit application.
- Flow: Provides a multi-tab interface for document upload, viewing similar cases, judgment prediction, and additional legal analyses. It interacts with backend modules for processing and analysis.

##### Architectural Patterns & Design Principles
- Modular Design: Each component (document processing, vector embedding, similarity search, judgment prediction) is encapsulated into its module, making updates and testing easier.
- Clear Separation of Concerns: The data preparation, model training/inference, prediction logic, and UI are clearly delineated.
- Optional Component Integration: The design gracefully handles dependencies (for example, integrating OpenAI GPT-4o only when the API key is present).
- Pipeline Approach: The processing flows from data intake (document upload) through preprocessing, vectorization, similarity search, prediction, and then analysis generation.

- That the integration between components is explicit—demonstrate how document analysis and model training form a continuous improvement pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Delhi High Court Virtual Judge System"
        %% User Interface Layer
        subgraph "User Interface Layer"
            UI["Streamlit Web Interface (app.py)"]:::frontend
        end

        %% Processing and Analysis Layer
        subgraph "Processing and Analysis Layer"
            %% Document Processing Module
            subgraph "Document Processing"
                DP1["PDF Text Extraction (pdf_processor.py)"]:::backend
                DP2["Data Loader (data_loader.py)"]:::backend
            end

            %% Vector Embedding and Similarity Search Module
            subgraph "Vector Embedding and Similarity Search"
                VE1["LegalBERT Embedding (embedding_utils.py)"]:::backend
                VE2["Vector Store (FAISS) (vector_store.py)"]:::backend
            end

            %% Judgment Prediction & Analysis Engine
            subgraph "Judgment Prediction & Analysis"
                JP1["Judgment Prediction (judgment_predictor.py)"]:::backend
                JP2["Trained Model Utilization (use_trained_model.py)"]:::backend
                JP3["Legal Analysis Generation (openai_integration.py)"]:::external
            end
        end

        %% Training and Model Update Layer
        subgraph "Training and Model Update Layer"
            TM1["Training Script (train_model.py)"]:::training
            TM2["Training Script (run_training.py)"]:::training
            TM3["Model Training Pipeline (model_trainer.py)"]:::training
        end

        %% Data Persistence Layer
        subgraph "Data Persistence Layer"
            DPRepo["Data Repository (data)"]:::database
        end

        %% Deployment & Configuration
        Deploy["Deployment & Configuration (pyproject.toml, etc.)"]:::config
    end

    %% Flow Connections
    UI -->|"DocumentUpload"| DP1
    UI -->|"DocumentUpload"| DP2
    DP1 -->|"ExtractText"| DP2
    DP2 -->|"PreprocessedDocs"| VE1
    VE1 -->|"GenerateEmbeddings"| VE2
    VE2 -->|"EmbedWithSearch"| JP1
    VE1 -->|"EmbedForPrediction"| JP2
    JP1 -->|"PredictOutcome"| JP2
    JP2 -->|"GenerateLegalAnalysis"| JP3

    %% Training Modules feeding back to Embedding
    TM1 -->|"RetrainModel"| VE1
    TM2 -->|"RetrainModel"| VE1
    TM3 -->|"RetrainModel"| VE1
    VE2 -->|"FeedbackLoop"| TM3

    %% Data storage flows
    DP1 -->|"StoreData"| DPRepo
    DP2 -->|"StoreData"| DPRepo
    VE2 -->|"StoreIndex"| DPRepo

    %% Additional connection to Deployment configuration
    UI --> Deploy

    %% Click Events
    click UI "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/app.py"
    click DP1 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/utils/pdf_processor.py"
    click DP2 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/utils/data_loader.py"
    click VE1 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/model/embedding_utils.py"
    click VE2 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/utils/vector_store.py"
    click JP1 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/utils/judgment_predictor.py"
    click JP2 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/model/use_trained_model.py"
    click TM1 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/train_model.py"
    click TM2 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/run_training.py"
    click TM3 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/model/model_trainer.py"
    click JP3 "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/blob/main/utils/openai_integration.py"
    click DPRepo "https://github.com/pixeler5diti/delhi-high-court-virtual-judge/tree/main/data/"

    %% Style Definitions
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef training fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef config fill:#d1ecf1,stroke:#333,stroke-width:2px;
```
