Repository navigation
Expand file tree
/
Copy pathmain_terminal.py
More file actions
103 lines (85 loc) · 3.34 KB
/
Copy pathmain_terminal.py
File metadata and controls
103 lines (85 loc) · 3.34 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
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
import os
from typing import Optional, List
from src.core.indexer import RegulatoryIndexer
from src.core.reasoner import TreeRAGReasoner
from src.config import Config
def _ensure_data_directories() -> None:
os.makedirs(Config.RAW_DATA_DIR, exist_ok=True)
os.makedirs(Config.INDEX_DIR, exist_ok=True)
def _find_pdf_files() -> List[str]:
pdf_files = []
for filename in os.listdir(Config.RAW_DATA_DIR):
if filename.lower().endswith('.pdf'):
pdf_files.append(filename)
return sorted(pdf_files)
def _select_pdf(pdf_files: List[str]) -> Optional[str]:
if not pdf_files:
print(f"⚠️ No PDF files found in: {Config.RAW_DATA_DIR}/")
print("📁 Please place a PDF file in the directory.")
return None
if len(pdf_files) == 1:
print(f"📄 Found PDF: {pdf_files[0]}")
return pdf_files[0]
print(f"\n📚 Found {len(pdf_files)} PDF files:")
for i, pdf in enumerate(pdf_files, 1):
print(f" {i}. {pdf}")
while True:
try:
choice = input(f"\nSelect PDF (1-{len(pdf_files)}): ").strip()
idx = int(choice) - 1
if 0 <= idx < len(pdf_files):
return pdf_files[idx]
else:
print(f"Please enter a number between 1 and {len(pdf_files)}")
except ValueError:
print("Please enter a valid number")
except KeyboardInterrupt:
print("Cancelled")
return None
def _process_document(pdf_path: str, pdf_filename: str, index_filename: str) -> bool:
print("\n--- 🏗️ Phase 1: Creating Knowledge Tree ---")
try:
indexer = RegulatoryIndexer()
text = indexer.extract_text(pdf_path)
tree = indexer.create_index("Medical Device Guideline", text)
indexer.save_index(tree, index_filename)
return True
except Exception as e:
print(f"❌ Error during indexing: {e}")
return False
def _run_consultation(index_filename: str) -> None:
print("\n--- 🧠 Phase 2: AI Consultant Ready ---")
try:
reasoner = TreeRAGReasoner(index_filename)
while True:
q = input("\nQ (종료: q): ").strip()
if q.lower() in ['q', 'quit', 'exit']:
print("👋 Goodbye!")
break
if not q:
print("Please enter a question.")
continue
print("\nThinking...", end="", flush=True)
ans = reasoner.query(q)
print(f"\r[Answer]:\n{ans}")
except FileNotFoundError as e:
print(f"❌ Index file not found: {e}")
except Exception as e:
print(f"❌ Error during consultation: {e}")
def main() -> None:
_ensure_data_directories()
pdf_files = _find_pdf_files()
pdf_filename = _select_pdf(pdf_files)
if not pdf_filename:
return
pdf_path = os.path.join(Config.RAW_DATA_DIR, pdf_filename)
index_filename = pdf_filename.replace('.pdf', '_index.json')
index_path = os.path.join(Config.INDEX_DIR, index_filename)
if not os.path.exists(index_path):
if not _process_document(pdf_path, pdf_filename, index_filename):
return
else:
print(f"\n--- ✅ Found existing index: {index_filename} ---")
_run_consultation(index_filename)
if __name__ == "__main__":
main()