-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_metadata.py
More file actions
73 lines (58 loc) · 2.29 KB
/
Copy pathgenerate_metadata.py
File metadata and controls
73 lines (58 loc) · 2.29 KB
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
import json
import typer
from pathlib import Path
from loguru import logger
from extract.pdf_reader import pdf_to_text
from transform.segmenter import split_by_articles
from transform.chunker import chunk_text
from storage.models import Chunk
app = typer.Typer()
def _estimate_page(chunk_text: str, pages: list[dict]) -> int:
probe = " ".join(chunk_text.split()[:10])
for page in pages:
if probe in page["text"]:
return page["page"]
return 1
@app.command()
def main(
pdf_dir: str = typer.Option("data", help="Directory containing PDF files"),
store_dir: str = typer.Option("vector_store", help="Output directory for metadata.json"),
):
pdf_files = list(Path(pdf_dir).glob("*.pdf"))
if not pdf_files:
logger.error(f"No PDFs found in {pdf_dir}")
raise typer.Exit(1)
for path in pdf_files:
logger.info(f"Processing {path.name}")
result = pdf_to_text(str(path))
pages, raw_text = result["pages"], result["raw_text"]
logger.info(f" Extracted {len(pages)} pages")
sections = split_by_articles(raw_text)
logger.info(f" Found {len(sections)} sections")
chunks: list[Chunk] = []
chunk_index = 0
for section in sections:
for text in chunk_text(section["text"]):
if not text.strip():
continue
chunks.append(Chunk(
chunk_index=chunk_index,
source=path.name,
page=_estimate_page(text, pages),
livre=section.get("livre"),
titre=section.get("titre"),
chapitre=section.get("chapitre"),
section=section.get("section"),
article_ref=section.get("article_ref"),
text=text,
))
chunk_index += 1
logger.info(f" Generated {len(chunks)} chunks")
out = Path(store_dir)
out.mkdir(parents=True, exist_ok=True)
metadata_path = out / "metadata.json"
with open(metadata_path, "w", encoding="utf-8") as f:
json.dump([c.model_dump() for c in chunks], f, ensure_ascii=False, indent=2)
logger.success(f"Saved {len(chunks)} chunks → {metadata_path}")
if __name__ == "__main__":
app()