File size: 11,360 Bytes
197a96c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: Modrik
emoji: πŸ”΅
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 8501
pinned: false
---

## ✦ Built at SDA Himmah Hackathon 2026

> Participated β€” Modrik Team
---

<div align="center">

```
╔══════════════════════════════════════════╗
β•‘                                          β•‘
β•‘               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.

<br/>

![Python](https://img.shields.io/badge/Python-3.11-3776AB?style=flat-square&logo=python&logoColor=white)
![Streamlit](https://img.shields.io/badge/Streamlit-UI-FF4B4B?style=flat-square&logo=streamlit&logoColor=white)
![OpenAI](https://img.shields.io/badge/OpenAI-GPT--4o--mini-412991?style=flat-square&logo=openai&logoColor=white)
![FAISS](https://img.shields.io/badge/FAISS-Local%20RAG-009688?style=flat-square)
![Docker](https://img.shields.io/badge/Docker-Ready-2496ED?style=flat-square&logo=docker&logoColor=white)

</div>

---

## ✦ 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.

```bash
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
- [Docker Desktop](https://www.docker.com/products/docker-desktop/) installed and running
- An OpenAI API key

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

```bash
cp .env.example .env
```

Open `.env` and fill in your key:

```
OPENAI_API_KEY=sk-...
```

### 2 β€” Build and start

```bash
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

```bash
# 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 |

---

<div align="center">

*Every worker deserves to know where they stand.*

</div>