An educational notebook that transforms a Moroccan legal PDF into searchable articles and uses retrieval-augmented generation to produce answers grounded in the retrieved source text.
MoroccoLaw-LLM demonstrates the foundations of a legal RAG workflow using the Moroccan Code des Obligations et des Contrats as an example source. The project extracts PDF text, detects individual articles, creates multilingual embeddings, searches them with FAISS, and asks a compact Qwen instruction model to answer only from retrieved context.
This repository is a learning prototype. It is not a complete legal product and does not provide legal advice.
flowchart LR
A[Legal PDF] --> B[Text extraction]
B --> C[Cleaning]
C --> D[Article parsing]
D --> E[Multilingual embeddings]
E --> F[FAISS index]
F --> G[Relevant articles]
G --> H[Grounded Qwen answer]
- Google Colab and local Jupyter setup
- Configurable project directories
- Qwen2.5-0.5B-Instruct loading and inference
- PDF text extraction with PyMuPDF
- Legal-text normalization
- Article detection with regular expressions
- Structured JSON generation
- Multilingual Sentence Transformer embeddings
- Cosine-similarity search with FAISS
- Source-grounded prompting and article citations
- Optional instruction-dataset generation
- JSON repair and dataset validation
| Area | Technology |
|---|---|
| Language | Python |
| Notebook | Jupyter / Google Colab |
| Language model | Qwen2.5-0.5B-Instruct |
| Embeddings | paraphrase-multilingual-MiniLM-L12-v2 |
| Vector search | FAISS |
| PDF processing | PyMuPDF |
| ML tooling | Transformers, Datasets, PEFT, TRL, Accelerate |
MoroccoLaw-LLM/
├── notebooks/
│ └── Lesson_01_Setup.ipynb
├── scripts/
│ └── validate_notebook.py
├── .github/workflows/notebook-quality.yml
├── .gitignore
├── LICENSE
├── requirements.txt
└── README.md
The notebook creates the following local directories while running. They are ignored by Git because they can contain source documents, generated datasets, or large model artifacts.
datasets/
├── raw/legal_documents/
├── cleaned/
├── processed/
└── training/
models/
checkpoints/
outputs/
-
Open the notebook using the Colab button above.
-
Run the dependency-installation cell.
-
Restart the runtime if Colab requests it.
-
Place an authorized copy of
Code_obligations_contrats.pdfin:/content/drive/MyDrive/MoroccoLaw-LLM/datasets/raw/legal_documents/ -
Run the remaining cells in order.
git clone https://github.com/deleted04user/MoroccoLaw-LLM.git
cd MoroccoLaw-LLM
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
jupyter notebook notebooks/Lesson_01_Setup.ipynbOn Windows PowerShell, activate the environment with:
.\.venv\Scripts\Activate.ps1Quelles sont les conditions de validité d'un contrat ?
The system retrieves the most relevant legal articles, passes them to Qwen as constrained context, and returns the answer together with the source articles used.
This first lesson is an end-to-end prototype rather than a production system. It currently uses one example legal code, regex-based structure detection, dense retrieval, and a lightweight local model.
- Add document name, page number, chapter, and section metadata
- Support additional official Moroccan legal sources
- Add Arabic/French bilingual normalization and retrieval
- Combine BM25 and dense retrieval
- Add deterministic citation and numerical-claim verification
- Create a reviewed legal evaluation dataset
- Add automated tests and reproducible benchmarks
- Expose the pipeline through a FastAPI service
- Build a bilingual web interface
- Use official or otherwise authorized legal documents.
- Keep provenance metadata for every extracted article.
- Review generated instruction examples before training.
- Verify every answer against the cited source text.
- Do not treat model output as professional legal advice.
Run the repository's lightweight notebook structure and secret-safety check:
python scripts/validate_notebook.pyMoroccoLaw-LLM is an educational and research project. Its output may be incomplete or incorrect and must not replace consultation with a qualified legal professional.
Original project code is released under the MIT License. Source legal documents, model weights, and third-party datasets retain their respective rights and licenses.