CoolFace
Apppublic

YuITC/Vietnamese-Legal-Doc-Retrieval

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

Vietnamese Legal Document Retrieval System

![Hugging Face Spaces](https://huggingface.co/spaces/YuITC/Vietnamese-Legal-Doc-Retrieval) ![Model](https://huggingface.co/YuITC/bert-base-multilingual-cased-finetuned-VNLegalDocs) ![Dataset](https://huggingface.co/datasets/YuITC/Vietnamese-Legal-Doc-Retrieval-Data)

A retrieval system specifically designed for Vietnamese legal documents using fine-tuned SBERT (Sentence-BERT) technology.

πŸ“Œ Overview

This project implements a retrieval system for retrieving relevant Vietnamese legal documents based on user queries. The system uses a fine-tuned multilingual BERT model to encode legal queries and documents into a semantic vector space, allowing for retrieval based on meaning rather than just keyword matching.

[image]

πŸ”‘ Key features

  • β€”Step-by-step notebook for understanding.
  • β€”Fine-tuned SBERT model specialized for Vietnamese legal document retrieval.
  • β€”FAISS indexing for efficient vector search.
  • β€”Evaluation based on MTEB.
  • β€”Interactive web interface for quick legal document search.
  • β€”High-performance retrieval of relevant legal passages.

πŸ› οΈ Installation & Usage

bash
# Install dependencies
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
conda install faiss-gpu=1.9.0 -c pytorch -c nvidia
pip install -r requirements.txt

# Running the Application
python main.py

The application will start a local web server with the Gradio interface, allowing you to enter legal queries and retrieve relevant documents.

πŸ“‚ Project Structure

Vietnamese-Legal-Doc-Retrieval/
β”œβ”€β”€ assets/                   # Visual assets for documentation 
β”‚   └── gradio_demo.png       # Screenshot of the Gradio demo interface
β”œβ”€β”€ cache/                    # Cached model files
β”‚   └── VN-legalDocs-SBERT/   # Cached BERT model files
β”œβ”€β”€ data/                     # Dataset files
β”‚   β”œβ”€β”€ original/             # Original downloaded dataset
β”‚   β”‚   β”œβ”€β”€ corpus.csv        # Raw corpus documents
β”‚   β”‚   β”œβ”€β”€ train_split.csv   # Training data
β”‚   β”‚   β”œβ”€β”€ val_split.csv     # Validation data
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ processed/            # Processed dataset files
β”‚   β”‚   β”œβ”€β”€ corpus_data.parquet  # Processed corpus for embedding
β”‚   β”‚   β”œβ”€β”€ train_data.parquet  # Processed training data
β”‚   β”‚   └── test_data.parquet   # Processed test data
β”‚   └── retrieval/            # Files for retrieval system
β”‚       └── legal_faiss.index # FAISS index for fast vector search
β”œβ”€β”€ models/                   # Trained model files
β”‚   └── VN-legalDocs-SBERT/   # Fine-tuned BERT model for legal documents
β”‚       β”œβ”€β”€ model.safetensors # Model weights
β”‚       β”œβ”€β”€ config.json       # Model configuration
β”‚       └── checkpoint-*/     # Training checkpoints
β”œβ”€β”€ results/                  # Evaluation results
β”œβ”€β”€ Dockerfile                # Docker configuration for deployment
β”œβ”€β”€ main.py                   # Main application entry point
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ settings.py               # Configuration settings
└── step_*_*.ipynb            # Jupyter notebooks for each step of the process

πŸ’Ύ Dataset

The system is trained on a Vietnamese legal document corpus containing:

  • β€”Legal texts from various domains
  • β€”Query-document pairs for training and evaluation
  • β€”Processed and structured for semantic search training

The dataset is available on Hugging Face (modified by me, the base dataset is cited below).

πŸ“Š Model Training Process

The project follows a systematic approach to build the retrieval system:

  1. 1.Data Preparation (step_01_Prepare_Data.ipynb):
  2. 2.Processes raw legal documents
  3. 3.Creates query-document pairs for training
  4. 4.Formats data for the embedding model
  1. 1.SBERT Fine-tuning (step_02_Finetune_SBERT.ipynb):
  2. 2.Fine-tunes a multilingual BERT model with legal document pairs
  3. 3.Uses CachedMultipleNegativesRankingLoss for training
  4. 4.Optimizes for semantic similarity in legal context
  1. 1.Evaluation (step_03_Eval_with_MTEB.ipynb):
  2. 2.Evaluates model performance using retrieval metrics
  3. 3.Compares with baseline models
  1. 1.Retrieval System Setup (step_04_Retrieval.ipynb):
  2. 2.Creates FAISS index from document embeddings
  3. 3.Implements efficient search functionality
  4. 4.Prepares for deployment

πŸ” Usage Examples

The system accepts natural language queries in Vietnamese related to legal topics. Example queries:

  • β€”"Tα»™i xΓΊc phαΊ‘m danh dα»±?" (Crimes against honor?)
  • β€”"Quyền lợi cα»§a người lao Δ‘α»™ng?" (Rights of workers?)
  • β€”"Thα»§ tα»₯c Δ‘Δƒng kΓ½ kαΊΏt hΓ΄n?" (Marriage registration procedures?)

πŸ§ͺ Performance

The fine-tuned model was evaluated using the MTEB benchmark on the BKAILegalDocRetrieval dataset. Key results:

Metric@kPre-trained model score (%)Fine-tuned model score (%)
NDCG10.00742.425
50.01157.387
100.02360.389
200.04962.160
1000.14763.894
MAP10.00740.328
50.00952.297
100.01453.608
200.02154.136
1000.03354.418
Recall10.00740.328
50.01770.466
100.05479.407
200.15786.112
1000.71394.805
Precision10.00742.425
50.00315.119
100.0058.587
200.0084.687
1000.0071.045
MRR10.00742.418
50.01054.337
100.01455.510
200.02155.956
1000.03356.172
  • β€”NDCG@k (Normalized Discounted Cumulative Gain) Measures ranking quality by evaluating the relevance of results with logarithmic position-based discounting.
  • β€”MAP@k (Mean Average Precision) Computes the average precision for each query up to rank kβ€”precision at each relevant retrieved documentβ€”then averages across all queries.
  • β€”Recall@k The proportion of all relevant documents that are retrieved in the top k results.
  • β€”Precision@k The proportion of the top k retrieved documents that are relevant.
  • β€”MRR@k (Mean Reciprocal Rank) The average of the reciprocal of the rank position of the first relevant document across all queries.

The model significantly outperforms baseline retrieval methods, with the main evaluation score (NDCG@10) reaching 60.4%, demonstrating strong performance on Vietnamese legal document retrieval tasks.

🐳 Docker Deployment

The project includes a Docker configuration for easy deployment. The Docker image is built on continuumio/miniconda3 and includes GPU support via PyTorch CUDA and FAISS-GPU.

bash
# Build the Docker image
docker build -t vietnamese-legal-retrieval .

# Run the container
docker run -p 7860:7860 vietnamese-legal-retrieval

The container:

  • β€”Uses Python 3.10 with CUDA 12.1 support
  • β€”Installs required dependencies from requirements.txt
  • β€”Exposes port 7860 for the Gradio web interface
  • β€”Sets proper environment variables for security and performance
  • β€”Runs as a non-root user for enhanced security

You can access the web interface by navigating to http://localhost:7860 after starting the container.

πŸ“œ License

This project is licensed under the MIT License – feel free to modify and distribute it as needed.

🀝 Acknowledgments

Thanks for:

If you find this project useful, consider ⭐️ starring the repository or contributing to further improvements!

πŸ“¬ Contact

For any questions or collaboration opportunities, feel free to reach out:

πŸ“§ Email: tainguyenphu2502@gmail.com