Modrik / README.md
Joriesk's picture
Deploy Modrik RAG assistant
197a96c
|
Raw History Blame Contribute Delete
11.4 kB
metadata
title: Modrik
emoji: πŸ”΅
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 8501
pinned: false

✦ Built at SDA Himmah Hackathon 2026

Participated β€” Modrik Team


╔══════════════════════════════════════════╗
β•‘                                          β•‘
β•‘               M O D R I K                β•‘
β•‘                   Ω…Ψ―Ψ±Ωƒ                   β•‘
β•‘                                          β•‘
β•‘   Every worker deserves to know their    β•‘
β•‘                 rights.                  β•‘
β•‘                                          β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

An Arabic-first AI legal assistant for Saudi Labor Law. Ask in plain Arabic. Upload your contract. Get an answer cited by article.


Python Streamlit OpenAI FAISS Docker


✦ What is Modrik?

Saudi Labor Law spans hundreds of articles across three states β€” active, amended, and repealed β€” and most workers never open it until they're already in a dispute.

Modrik (Ω…Ψ―Ψ±Ωƒ) lets anyone ask about their rights in plain Arabic, upload their actual contract or case file, and get a personal answer grounded in the specific article that applies β€” not a generic summary.

"Ω‡Ω„ ΨΉΩ‚Ψ―ΩŠ ΩŠΨ·Ψ§Ψ¨Ω‚ Ω†ΨΈΨ§Ω… Ψ§Ω„ΨΉΩ…Ω„ΨŸ" Modrik reads your contract, retrieves the relevant articles β€” whether still in force, amended, or repealed β€” and tells you exactly where you stand, citing the law instead of guessing.


✦ How a question gets answered

        USER ASKS A QUESTION  (+ optional contract / case file)
                        β”‚
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚     PII GUARD     β”‚   redacts Saudi ID / IBAN
              β”‚  (regex)          β”‚   before anything is sent
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                               β–Ό
  UPLOADED FILE                  FAISS RETRIEVAL
  (txt / pdf extracted)     query embedded with
                          multilingual-e5-large
                           β†’ top-k matching Labor
                             Law articles, local
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚   GPT-4o-mini     β”‚   streamed response,
              β”‚   (streamed)      β”‚   cites article numbers,
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   flags amended/repealed
                        β–Ό
              ANSWER + SOURCES POPOVER
              (article matches with similarity %)
Step What happens
Input User types in Arabic (or English) and may attach a .txt/.pdf contract or case document
Privacy guard Saudi National ID/Iqama numbers and IBANs are regex-redacted before anything leaves the session
Retrieval The query is embedded locally with multilingual-e5-large and FAISS searches the Labor Law index (active + amended + repealed) for the top-k matching articles
Generation GPT-4o-mini streams a personalized answer, always citing the article number and flagging amended/repealed status
Transparency A sources popover shows the exact retrieved articles with similarity scores, plus the contract excerpt actually used

✦ Tech stack

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FRONTEND / UI                    β”‚
β”‚        Streamlit Β· RTL Arabic chat interface        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        core/                        β”‚
β”‚                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ pii_guard  β”‚  β”‚ file_service  β”‚  β”‚ vector_storeβ”‚ β”‚
β”‚  β”‚ regex      β”‚  β”‚ PyMuPDF       β”‚  β”‚ FAISS,      β”‚ β”‚
β”‚  β”‚ redaction  β”‚  β”‚ txt / pdf     β”‚  β”‚ local, free β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                           β”‚         β”‚
β”‚                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚                                   β”‚  ai_service   β”‚ β”‚
β”‚                                   β”‚  OpenAI streamβ”‚ β”‚
β”‚                                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Layer Technology
LLM OpenAI GPT-4o-mini, streamed token-by-token
Embeddings intfloat/multilingual-e5-large, run locally (see below)
Vector store FAISS (IndexFlatIP over normalized vectors)
PDF / text extraction PyMuPDF (fitz)
Law ingestion pandas + openpyxl β€” Excel β†’ smart-chunked articles
Privacy regex-based PII redaction (Saudi ID/Iqama, IBAN)
UI Streamlit, RTL layout, custom CSS, IBM Plex Sans Arabic
Deployment Docker / docker-compose, Hugging Face Spaces

✦ Why this embedding model

Most RAG demos default straight to an embedding API. Modrik instead runs multilingual-e5-large locally β€” the multilingual E5 model β€” picked after comparing leading multilingual embedding models against published Arabic retrieval benchmarks (MIRACL, MTEB), where it consistently ranks among the strongest open models for Arabic semantic search. That matters here specifically: Saudi Labor Law articles are dense, formal legal Arabic, not casual text.

The practical upside: embedding has zero per-query API cost, and the index β€” active, amended, and repealed articles β€” is pre-warmed at container startup, so retrieval is instant from the first question.


✦ Smart law ingestion

Saudi Labor Law isn't one flat document β€” articles get amended or repealed, and the old text still matters for context. scripts/ingest_labor_law.py ingests three separate sources and tags each one:

Source Tag shown to the model
labor_law_active.xlsx βœ… Ω†ΨΈΨ§Ω… Ω†Ψ΄Ψ· وسارٍ Ψ­Ψ§Ω„ΩŠΨ§Ω‹
labor_law_amended.xlsx ⚠️ Ω‡Ψ°Ω‡ Ψ§Ω„Ω…Ψ§Ψ―Ψ© Ω…ΨΉΨ―Ω‘Ω„Ψ©
labor_law_repealed.xlsx 🚫 Ω‡Ψ°Ω‡ Ψ§Ω„Ω…Ψ§Ψ―Ψ© Ω…Ω„ΨΊΨ§Ψ© ΩˆΩ„Ψ§ ΨͺΩΨ·Ψ¨ΩŽΩ‘Ω‚ Ψ­Ψ§Ω„ΩŠΨ§Ω‹

Articles longer than 600 characters are split on definition markers or paragraph breaks first, then merged back up to the chunk limit β€” so no sub-chunk ever loses its article header or keyword context.


✦ Project structure

Modrik/
β”‚
β”œβ”€β”€ app.py                      ← Streamlit entry point (chat UI, RTL Arabic)
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ ai_service.py           ← OpenAI streaming + system prompt
β”‚   β”œβ”€β”€ file_service.py         ← txt/pdf extraction + chunking
β”‚   β”œβ”€β”€ pii_guard.py            ← Saudi ID / IBAN redaction
β”‚   └── vector_store.py         ← FAISS index, embed/retrieve
β”œβ”€β”€ scripts/
β”‚   └── ingest_labor_law.py     ← builds the FAISS index from Excel sources
β”œβ”€β”€ tests/
β”‚   └── test_pipeline.py        ← full unit test suite
β”œβ”€β”€ data/                       ← active / amended / repealed Labor Law (Excel)
β”œβ”€β”€ index/                      ← FAISS index + chunks (generated)
β”œβ”€β”€ assets/                     ← logo
β”œβ”€β”€ Dockerfile / docker-compose.yml / entrypoint.sh
└── requirements.txt

✦ Tests

Every public function ships with a test: chunking edge cases, .txt/.pdf extraction (PDF mocked via fitz), the OpenAI streaming contract, vector store retrieval, and every PII redaction pattern.

python -m pytest tests/ -v

✦ Privacy by default

Before any text β€” typed or uploaded β€” reaches OpenAI or gets logged, core/pii_guard.py strips Saudi National ID/Iqama numbers and IBANs with a regex pass, replaces them with [Ψ±Ω‚Ω… Ω‡ΩˆΩŠΨ©] / [Ψ±Ω‚Ω… IBAN], and tells the user how many were redacted. No opt-in required, no configuration needed.


✦ Quick start

Requirements

1 β€” Copy the env file and add your key

cp .env.example .env

Open .env and fill in your key:

OPENAI_API_KEY=sk-...

2 β€” Build and start

docker-compose up --build

On first run, the FAISS index is built automatically (~1-2 minutes). Subsequent starts are instant.

3 β€” Open in browser

http://localhost:8501

SSL error on corporate / university networks

SSLError: certificate verify failed

This is caused by a firewall or SSL-inspecting proxy. The fix is already included in the Dockerfile β€” no extra steps needed.

Useful commands

# Stream logs
docker-compose logs -f

# Stop the project
docker-compose down

# Rebuild after code or requirements.txt changes
docker-compose up --build

# Run the test suite
python -m pytest tests/ -v

✦ Roadmap

Status Feature
βœ… RAG over active / amended / repealed Labor Law articles
βœ… Contract & case file upload (.txt/.pdf) with personalized analysis
βœ… Automatic PII redaction (Saudi ID, IBAN)
βœ… Source transparency β€” article + similarity score popover
βœ… Full automated test suite
βœ… Dockerized, one-command run
πŸ”œ More legal domains beyond Labor Law
πŸ”œ Multi-turn case memory across sessions
πŸ”œ Export consultation as PDF

Every worker deserves to know where they stand.