-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
113 lines (96 loc) · 4.27 KB
/
Copy pathsetup.py
File metadata and controls
113 lines (96 loc) · 4.27 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
104
105
106
107
108
109
110
111
112
113
import os
import sys
import subprocess
import requests
from pathlib import Path
# Fix python path to allow importing from src
sys.path.append(str(Path(__file__).resolve().parent))
def check_dependencies():
"""Verify system-level dependencies are accessible."""
print("[Setup] Checking system binaries...")
# Check Tesseract OCR
from config.config import TESSERACT_CMD
if not os.path.exists(TESSERACT_CMD):
print(f"[Warning] Tesseract binary not found at '{TESSERACT_CMD}'. OCR capabilities for scanned documents will be unavailable.")
print("Install it via Homebrew: 'brew install tesseract'")
else:
print(" -> Tesseract OCR: FOUND")
# Check Ollama connection
from config.config import OLLAMA_BASE_URL
try:
res = requests.get(f"{OLLAMA_BASE_URL}/api/tags", timeout=2.0)
if res.status_code == 200:
print(" -> Ollama LLM Service: FOUND & RUNNING")
else:
print(f"[Warning] Ollama returned status {res.status_code}. Make sure Ollama server is running.")
except Exception:
print("[Warning] Ollama service not running. Please launch Ollama and run 'ollama serve'.")
def pull_ollama_model():
"""Pull default model if it's missing on Ollama server."""
from config.config import OLLAMA_BASE_URL, DEFAULT_LLM_MODEL
try:
print(f"[Setup] Checking if default model '{DEFAULT_LLM_MODEL}' is pulled on Ollama...")
res = requests.get(f"{OLLAMA_BASE_URL}/api/tags", timeout=2.0)
models = [m["name"] for m in res.json().get("models", [])]
# Check for phi3 or model variants
if not any(DEFAULT_LLM_MODEL in m for m in models):
print(f"[Setup] Default model '{DEFAULT_LLM_MODEL}' not found. Attempting to pull model via Ollama...")
# We run a subprocess call to pull model
subprocess.run(["ollama", "pull", DEFAULT_LLM_MODEL], check=True)
print(f" -> Pulled '{DEFAULT_LLM_MODEL}' successfully.")
else:
print(f" -> Model '{DEFAULT_LLM_MODEL}': ALREADY PULLED")
except Exception as e:
print(f"[Warning] Could not automatically pull model '{DEFAULT_LLM_MODEL}': {e}.")
print(f"Please run 'ollama pull {DEFAULT_LLM_MODEL}' in a terminal.")
def pre_index_documents():
"""Submit existing PDFs in data/raw/ to the auto-ingestion folder and wait for indexing to complete."""
from src.ingestion.ingestion_queue import IngestionTaskQueue
from config.config import RAW_DIR, UPLOADED_DOCS_DIR
import shutil
import time
# 1. Copy active PDFs to uploaded_documents
if RAW_DIR.exists():
pdf_files = list(RAW_DIR.glob("*.pdf"))
print(f"[Setup] Submitting {len(pdf_files)} active PDFs to Auto-Ingestion folder...")
for file_path in pdf_files:
dest_file = UPLOADED_DOCS_DIR / file_path.name
if not dest_file.exists():
shutil.copy2(str(file_path), str(dest_file))
print(f" -> Copied '{file_path.name}' to uploaded_documents/")
# 2. Wait for queue worker to finish processing
queue_mgr = IngestionTaskQueue()
print("[Setup] Waiting for background indexing to complete...")
while True:
with queue_mgr.lock:
all_done = True
for task in queue_mgr.manifest.values():
if task["status"] in ["queued", "processing"]:
all_done = False
break
if all_done:
break
print(" -> Indexing in progress... waiting 2s")
time.sleep(2)
print("[Setup] Indexing complete. Manifest and Qdrant DB are in sync.")
queue_mgr.close()
def main():
print("="*60)
print(" LOCAL RAG PIPELINE ENVIRONMENT SETUP ")
print("="*60)
# 1. System checks
check_dependencies()
# 2. Model pull
pull_ollama_model()
# 3. Compile & Index samples
pre_index_documents()
# 4. Launch Web application
print("\n[Setup] Launching Streamlit Web App...")
cmd = ["python3.12", "-m", "streamlit", "run", "app.py"]
print(f"Running command: {' '.join(cmd)}")
try:
subprocess.run(cmd)
except KeyboardInterrupt:
print("\n[Setup] Stopped Streamlit server.")
if __name__ == "__main__":
main()