MalikShehram's picture
Update app.py
2415524 verified
Raw History Blame Contribute Delete
2.15 kB
"""
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()