forked from tonyfant/PageIndex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_documents.py
More file actions
61 lines (44 loc) · 1.6 KB
/
Copy pathindex_documents.py
File metadata and controls
61 lines (44 loc) · 1.6 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
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
current_dir = Path.cwd()
sys.path.insert(0, str(current_dir))
from pageindex import PageIndexClient
from pageindex.utils import ConfigLoader
PDF_FOLDER = current_dir / "files_pdf"
WORKSPACE = current_dir / "workspace"
LOG_DIR = current_dir / "logs"
# Assicurati che le cartelle esistano
PDF_FOLDER.mkdir(parents=True, exist_ok=True)
WORKSPACE.mkdir(parents=True, exist_ok=True)
def run_local_indexing():
# Inizializza il client SOLO con il workspace.
# La chiave API verrà presa in automatico dal file .env grazie a utils.py
client = PageIndexClient(workspace=str(WORKSPACE))
print(f"model: {client.model}")
print(f"documents directory: {PDF_FOLDER}")
print(f"workspace: {WORKSPACE}")
pdf_files = list(PDF_FOLDER.glob("*.pdf"))
if not pdf_files:
print(f"no pdfs in {PDF_FOLDER}")
return
for pdf_path in pdf_files:
already_indexed = False
for doc_id, info in client.documents.items():
if info.get('doc_name') == pdf_path.name:
print(f"[{pdf_path.name}] already indexded ID: {doc_id}")
already_indexed = True
break
if already_indexed:
continue
print(f"indexing: {pdf_path.name}...")
try:
doc_id = client.index(str(pdf_path))
print(f"done, doc id: {doc_id}")
except Exception as e:
print(f"error {pdf_path.name}: {e}")
print(f" {len(client.documents)} documents loaded")
if __name__ == "__main__":
run_local_indexing()