File size: 2,146 Bytes
2415524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Backend initialization script
Run this FIRST to build the vector store from PDFs
"""
import os
import sys
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

from document_processor import PDFProcessor, add_contextual_metadata
from rag_engine import RAGEngine
from config import VECTOR_STORE_DIR, INDEX_NAME

def main():
    print("=" * 60)
    print("Microflow Cytometry RAG - Backend Initialization")
    print("=" * 60)
    
    # Check API key
    api_key = os.environ.get("GROQ_API_KEY")
    if not api_key:
        print("\n❌ ERROR: GROQ_API_KEY not found!")
        print("Please set it in your environment or .env file:")
        print("  export GROQ_API_KEY='your-key-here'")
        print("\nGet a free key at: https://console.groq.com/keys")
        sys.exit(1)
    
    print(f"\n✓ Groq API key loaded")
    
    # Check for existing vector store
    index_path = VECTOR_STORE_DIR / INDEX_NAME
    force_rebuild = "--rebuild" in sys.argv
    
    if index_path.exists() and not force_rebuild:
        print(f"\n✓ Vector store already exists at: {index_path}")
        print("  Use --rebuild to force rebuild")
    else:
        print("\n📄 Processing PDF documents...")
        processor = PDFProcessor()
        docs = processor.process_all_documents()
        
        if not docs:
            print("❌ No documents were processed. Check your papers/ folder.")
            sys.exit(1)
        
        print(f"\n✓ Processed {len(docs)} document chunks")
        
        print("\n🏷️  Adding contextual metadata...")
        docs = add_contextual_metadata(docs)
        
        print("\n🔢 Building vector embeddings (this may take a few minutes)...")
        engine = RAGEngine()
        engine.build_vector_store(docs)
        print("✓ Vector store built and saved")
    
    print("\n" + "=" * 60)
    print("✓ Initialization complete!")
    print("=" * 60)
    print("\nNext step: Run the frontend with:")
    print("  python frontend.py")
    print("\nOr launch with GPU sharing (ZeroGPU):")
    print("  gradio frontend.py")
    print()

if __name__ == "__main__":
    main()