""" 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()