File size: 11,813 Bytes
86f1108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
BPOM Compliance System โ€” Step 1: Ingest Regulations to ChromaDB

Purpose:
    Extract text from regulation PDFs, chunk per-Pasal (chunk_size=500, overlap=50),
    embed with paraphrase-multilingual-MiniLM-L12-v2, and upsert into
    ChromaDB persistent collections (one per product category).

Collections created:
    - bpom_suplemen
    - bpom_dairy
    - bpom_daging_olahan
    - bpom_buah_sayur

Usage:
    python src/ingest.py

Expected output:
    Terminal logs showing each PDF being ingested + chunk counts per collection.
"""

import re
import os
import sys
import logging
from pathlib import Path

import pdfplumber
import chromadb
from sentence_transformers import SentenceTransformer
from dotenv import load_dotenv

# โ”€โ”€โ”€ Configuration โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

load_dotenv()

CHROMA_DB_PATH = os.getenv("CHROMA_DB_PATH", "./chroma_db")
REGULATIONS_PATH = os.getenv("REGULATIONS_PATH", "./data/regulations")

CATEGORY_MAP = {
    "SUPLEMEN": "bpom_suplemen",
    "DAIRY": "bpom_dairy",
    "DAGING_OLAHAN": "bpom_daging_olahan",
    "BUAH_SAYUR": "bpom_buah_sayur",
}

# Lightweight multilingual model โ€” fits MacBook Air M2 8GB easily
EMBEDDING_MODEL = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"

CHUNK_SIZE = 500
CHUNK_OVERLAP = 50

# โ”€โ”€โ”€ Logging โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)

# โ”€โ”€โ”€ PDF Text Extraction โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


def clean_line(line: str) -> str:
    """Clean a single line from PDF extraction artefacts."""
    line = line.replace("\xa0", " ").replace("\ufffe", "-")
    # Remove page number markers like  -7-  or  - 7 -
    line = re.sub(r"^[-โ€“]\s*\d+\s*[-โ€“]\s*$", " ", line)
    # Remove jdih watermarks
    line = re.sub(r"\bjdih\.pom\.go\.id\b", " ", line, flags=re.IGNORECASE)
    # Fix common OCR typo: "Pasa 5" โ†’ "Pasal 5"
    line = re.sub(r"^\s*Pasa\s+(\d+)\s*$", r"Pasal \1", line)
    # Collapse whitespace
    line = re.sub(r"[ \t]+", " ", line)
    return line.strip()


def extract_pdf_lines(pdf_path: str) -> list[dict]:
    """
    Extract all text lines from a PDF, page by page.
    Returns list of {"page": int, "line": str}.
    """
    rows: list[dict] = []
    try:
        with pdfplumber.open(pdf_path) as pdf:
            for page_no, page in enumerate(pdf.pages, start=1):
                text = page.extract_text(x_tolerance=1, y_tolerance=3) or ""
                for raw_line in text.splitlines():
                    line = clean_line(raw_line)
                    if line:
                        rows.append({"page": page_no, "line": line})
    except Exception as e:
        logger.error(f"Failed to extract PDF {pdf_path}: {e}")
    return rows


# โ”€โ”€โ”€ Pasal-based Chunking โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


def chunk_by_pasal(rows: list[dict], source_filename: str) -> list[dict]:
    """
    Split extracted PDF lines into chunks grouped by Pasal heading.
    If a chunk exceeds CHUNK_SIZE chars, it is further split with overlap.

    Returns list of dicts:
        {pasal_id, teks, halaman_start, halaman_end, source}
    """
    chunks: list[dict] = []
    current_pasal = "HEADER"
    current_lines: list[str] = []
    current_start_page = 1

    for row in rows:
        line = row["line"]
        # Detect Pasal heading: "Pasal 1", "Pasal 23", etc.
        pasal_match = re.match(r"^Pasal\s+(\d+)\s*$", line)

        if pasal_match:
            # Save previous chunk
            if current_lines:
                _add_chunks(
                    chunks, current_pasal, current_lines,
                    current_start_page, row["page"], source_filename
                )
            current_pasal = f"Pasal {pasal_match.group(1)}"
            current_lines = [line]
            current_start_page = row["page"]
        else:
            current_lines.append(line)

    # Save last chunk
    if current_lines:
        last_page = rows[-1]["page"] if rows else current_start_page
        _add_chunks(
            chunks, current_pasal, current_lines,
            current_start_page, last_page, source_filename
        )

    # If no Pasal headings found at all, chunk the entire text
    if not chunks and rows:
        full_text = " ".join(r["line"] for r in rows)
        for i in range(0, len(full_text), CHUNK_SIZE - CHUNK_OVERLAP):
            chunk_text = full_text[i:i + CHUNK_SIZE]
            if len(chunk_text.strip()) > 20:
                chunks.append({
                    "pasal_id": "FULL_DOC",
                    "teks": chunk_text.strip(),
                    "halaman_start": rows[0]["page"],
                    "halaman_end": rows[-1]["page"],
                    "source": source_filename,
                })

    return chunks


def _add_chunks(
    chunks: list[dict],
    pasal_id: str,
    lines: list[str],
    start_page: int,
    end_page: int,
    source: str,
) -> None:
    """Join lines into text and split if > CHUNK_SIZE chars."""
    full_text = " ".join(lines).strip()
    if len(full_text) < 20:
        return

    if len(full_text) <= CHUNK_SIZE:
        chunks.append({
            "pasal_id": pasal_id,
            "teks": full_text,
            "halaman_start": start_page,
            "halaman_end": end_page,
            "source": source,
        })
    else:
        # Split into overlapping chunks
        step = CHUNK_SIZE - CHUNK_OVERLAP
        for i in range(0, len(full_text), step):
            chunk_text = full_text[i:i + CHUNK_SIZE]
            if len(chunk_text.strip()) > 20:
                chunks.append({
                    "pasal_id": pasal_id,
                    "teks": chunk_text.strip(),
                    "halaman_start": start_page,
                    "halaman_end": end_page,
                    "source": source,
                })


# โ”€โ”€โ”€ ChromaDB Ingest โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


def ingest_all_regulations() -> dict[str, int]:
    """
    Main ingestion function. Reads all regulation PDFs from
    data/regulations/<CATEGORY>/*.pdf, chunks them, embeds, and upserts
    into ChromaDB.

    Returns dict mapping collection_name โ†’ total chunk count.
    """
    logger.info("=" * 60)
    logger.info("BPOM Regulation Ingestion โ€” Starting")
    logger.info("=" * 60)

    # Initialise ChromaDB persistent client
    client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
    logger.info(f"ChromaDB path: {os.path.abspath(CHROMA_DB_PATH)}")

    # Load embedding model
    logger.info(f"Loading embedding model: {EMBEDDING_MODEL}")
    model = SentenceTransformer(EMBEDDING_MODEL)
    logger.info("โœ… Embedding model loaded")

    reg_path = Path(REGULATIONS_PATH)
    if not reg_path.exists():
        logger.error(f"Regulations path not found: {reg_path}")
        sys.exit(1)

    results: dict[str, int] = {}

    for category, collection_name in CATEGORY_MAP.items():
        logger.info("-" * 50)
        logger.info(f"๐Ÿ“‚ Category: {category} โ†’ Collection: {collection_name}")

        # Get or create collection (safe to re-run)
        collection = client.get_or_create_collection(
            name=collection_name,
            metadata={"hnsw:space": "cosine"},
        )

        cat_path = reg_path / category
        if not cat_path.exists():
            logger.warning(f"โš ๏ธ  Folder {cat_path} does not exist โ€” skipping")
            results[collection_name] = 0
            continue

        pdf_files = list(cat_path.glob("*.pdf"))
        if not pdf_files:
            logger.warning(f"โš ๏ธ  No PDFs in {cat_path} โ€” skipping")
            results[collection_name] = 0
            continue

        all_chunks: list[dict] = []

        for pdf_file in pdf_files:
            logger.info(f"  ๐Ÿ“„ Processing: {pdf_file.name}")
            rows = extract_pdf_lines(str(pdf_file))
            logger.info(f"     Extracted {len(rows)} lines")

            chunks = chunk_by_pasal(rows, pdf_file.name)
            logger.info(f"     Created {len(chunks)} chunks")
            all_chunks.extend(chunks)

        if not all_chunks:
            logger.warning(f"  No chunks created for {category}")
            results[collection_name] = 0
            continue

        # Embed all chunks
        texts = [c["teks"] for c in all_chunks]
        logger.info(f"  ๐Ÿงฎ Embedding {len(texts)} chunks...")
        embeddings = model.encode(
            texts, batch_size=16, show_progress_bar=True
        )

        # Build IDs and metadata
        ids = [
            f"{c['source'].replace('.pdf', '')}_{c['pasal_id']}_{i}"
            for i, c in enumerate(all_chunks)
        ]
        metadatas = [
            {
                "source": c["source"],
                "pasal": c["pasal_id"],
                "halaman_start": c["halaman_start"],
                "halaman_end": c["halaman_end"],
                "kategori": category,
            }
            for c in all_chunks
        ]

        # Upsert in batches (ChromaDB has batch limits)
        BATCH_SIZE = 100
        for batch_start in range(0, len(ids), BATCH_SIZE):
            batch_end = min(batch_start + BATCH_SIZE, len(ids))
            collection.upsert(
                ids=ids[batch_start:batch_end],
                embeddings=embeddings[batch_start:batch_end].tolist(),
                documents=texts[batch_start:batch_end],
                metadatas=metadatas[batch_start:batch_end],
            )

        count = collection.count()
        results[collection_name] = count
        logger.info(f"  โœ… {collection_name}: {count} total chunks in ChromaDB")

    # Summary
    logger.info("=" * 60)
    logger.info("INGESTION SUMMARY")
    logger.info("=" * 60)
    total = 0
    for coll_name, count in results.items():
        logger.info(f"  {coll_name}: {count} chunks")
        total += count
    logger.info(f"  TOTAL: {total} chunks across {len(results)} collections")
    logger.info("=" * 60)

    return results


# โ”€โ”€โ”€ Standalone Test โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€


def main():
    """Standalone entry point for testing ingestion."""
    results = ingest_all_regulations()

    # Verify by querying each collection
    client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
    print("\n๐Ÿ“Š Verification โ€” Collection counts:")
    for category, collection_name in CATEGORY_MAP.items():
        try:
            collection = client.get_collection(collection_name)
            count = collection.count()
            print(f"  {collection_name}: {count} chunks โœ…")

            # Peek at first chunk
            peek = collection.peek(limit=1)
            if peek["documents"]:
                doc_preview = peek["documents"][0][:100]
                print(f"    Preview: {doc_preview}...")
        except Exception as e:
            print(f"  {collection_name}: ERROR โ€” {e}")

    print("\nโœ… Ingest complete!")


if __name__ == "__main__":
    main()