# #!/usr/bin/env python3 # """ # Render.com deployment launcher for RAG Chatbot # Repository structure: rag_app/ is the git root # """ # import os # import sys # import time # from pathlib import Path # print("🚀 Starting Render deployment setup...") # print(f"📁 Current directory: {os.getcwd()}") # print(f"📂 Contents: {os.listdir('.')}") # # Since we're already in rag_app/, no need to change directories # # Just ensure current directory is in Python path # sys.path.insert(0, os.getcwd()) # def setup_for_render(): # """Setup vector stores and environment for Render deployment""" # print("🔧 Setting up vector stores for Render...") # # Ensure directories exist (relative to rag_app/) # required_dirs = [ # "./vector_stores", # "./docs", # "./docs/mes", # "./docs/technical", # "./docs/general" # ] # for directory in required_dirs: # os.makedirs(directory, exist_ok=True) # exists = "✅" if os.path.exists(directory) else "❌" # print(f"{exists} Directory: {directory}") # # Check existing vector stores # store_configs = [ # ("MES Manual", "docs/mes", "./vector_stores/mes_db"), # ("Technical Docs", "docs/technical", "./vector_stores/tech_db"), # ("General Docs", "docs/general", "./vector_stores/general_db") # ] # stores_to_build = [] # for name, doc_path, persist_dir in store_configs: # if os.path.exists(persist_dir) and os.listdir(persist_dir): # print(f"✅ {name} vector store already exists") # else: # stores_to_build.append((name, doc_path, persist_dir)) # print(f"🔧 {name} vector store needs building") # # Build missing vector stores # if stores_to_build: # print(f"🏗️ Building {len(stores_to_build)} vector store(s)...") # # Add timeout to prevent Render build timeout # start_time = time.time() # MAX_BUILD_TIME = 600 # 10 minutes max for free tier # try: # # Import your vector store utilities # from utils.vector_store import build_vector_store # print("✅ Vector store utilities imported successfully") # for name, doc_path, persist_dir in stores_to_build: # # Check build time limit # elapsed = time.time() - start_time # if elapsed > MAX_BUILD_TIME: # print( # f"⏰ Build time limit reached ({elapsed:.1f}s), creating empty stores") # os.makedirs(persist_dir, exist_ok=True) # continue # print(f"📚 Building {name} (elapsed: {elapsed:.1f}s)...") # # Check if documents exist # if os.path.exists(doc_path): # doc_files = list(Path(doc_path).rglob("*")) # doc_files = [f for f in doc_files if f.is_file( # ) and not f.name.startswith('.')] # if doc_files: # print( # f"📄 Found {len(doc_files)} document(s) for {name}") # try: # build_vector_store( # doc_path=doc_path, # persist_directory=persist_dir # ) # print(f"✅ {name} built successfully") # except Exception as e: # print(f"❌ Error building {name}: {str(e)}") # os.makedirs(persist_dir, exist_ok=True) # else: # print(f"⚠️ No documents found in {doc_path}") # os.makedirs(persist_dir, exist_ok=True) # else: # print(f"⚠️ Document path not found: {doc_path}") # os.makedirs(persist_dir, exist_ok=True) # except ImportError as e: # print(f"❌ Could not import vector store utilities: {e}") # print("📁 Creating empty vector store directories as fallback...") # for name, doc_path, persist_dir in stores_to_build: # os.makedirs(persist_dir, exist_ok=True) # except Exception as e: # print(f"❌ Unexpected error during vector store setup: {e}") # print("📁 Creating empty directories as fallback...") # for name, doc_path, persist_dir in stores_to_build: # os.makedirs(persist_dir, exist_ok=True) # else: # print("✅ All vector stores already exist!") # print("🎉 Vector store setup completed!") # def start_server(): # """Start the FastAPI server""" # print("🌐 Starting FastAPI server...") # try: # # Import your FastAPI app from api/main.py # from api.main import app # print("✅ Successfully imported FastAPI app from api.main") # import uvicorn # # Render uses PORT environment variable # port = int(os.environ.get("PORT", 7860)) # host = "0.0.0.0" # print(f"🚀 Starting server on {host}:{port}") # print(f"🔗 Health check will be available at: /{''}") # # Start the server # uvicorn.run( # app, # host=host, # port=port, # log_level="info", # access_log=True # ) # except ImportError as e: # print(f"❌ Could not import FastAPI app: {e}") # print("📂 Current directory contents:") # for item in os.listdir('.'): # print(f" - {item}") # print("🔍 Looking for api/main.py...") # if os.path.exists('api/main.py'): # print("✅ api/main.py exists") # else: # print("❌ api/main.py not found") # sys.exit(1) # except Exception as e: # print(f"❌ Error starting server: {e}") # sys.exit(1) # if __name__ == "__main__": # print("=" * 60) # print("🎯 RAG Chatbot - Render Deployment") # print("📁 Repository root: rag_app/") # print("=" * 60) # # Setup phase # setup_for_render() # # Server start phase # start_server() # #!/usr/bin/env python3 # """ # HF Spaces deployment launcher for RAG Chatbot # Repository structure: rag_app/ is the git root # """ # import os # import sys # import time # from pathlib import Path # import os # # HF Spaces only allows writing to /tmp # os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache" # os.environ["HF_HOME"] = "/tmp/hf_cache" # os.makedirs("/tmp/hf_cache", exist_ok=True) # print("🚀 Starting HF Spaces deployment setup...") # print(f"📁 Current directory: {os.getcwd()}") # print(f"📂 Contents: {os.listdir('.')}") # # Ensure current directory is in Python path # sys.path.insert(0, os.getcwd()) # # HF Spaces writable path for ephemeral storage # TMP_VECTOR_STORE_ROOT = "/tmp/vector_stores" # def setup_for_spaces(): # """Setup vector stores and environment for HF Spaces""" # print("🔧 Setting up vector stores for HF Spaces...") # # Ensure docs folders exist in repo # required_dirs = [ # "./docs", # "./docs/mes", # "./docs/technical", # "./docs/general" # ] # for directory in required_dirs: # os.makedirs(directory, exist_ok=True) # exists = "✅" if os.path.exists(directory) else "❌" # print(f"{exists} Directory: {directory}") # # Map of vector stores (persist dirs now point to /tmp) # store_configs = [ # ("MES Manual", "docs/mes", os.path.join(TMP_VECTOR_STORE_ROOT, "mes_db")), # ("Technical Docs", "docs/technical", # os.path.join(TMP_VECTOR_STORE_ROOT, "tech_db")), # ("General Docs", "docs/general", # os.path.join(TMP_VECTOR_STORE_ROOT, "general_db")), # ] # stores_to_build = [] # for name, doc_path, persist_dir in store_configs: # # Check if store already exists in repo or in /tmp # if os.path.exists(persist_dir) and os.listdir(persist_dir): # print(f"✅ {name} vector store already exists in {persist_dir}") # else: # stores_to_build.append((name, doc_path, persist_dir)) # print(f"🔧 {name} vector store needs building in {persist_dir}") # # Build missing vector stores # if stores_to_build: # print(f"🏗️ Building {len(stores_to_build)} vector store(s)...") # start_time = time.time() # MAX_BUILD_TIME = 600 # seconds # try: # from utils.vector_store import build_vector_store # print("✅ Vector store utilities imported successfully") # for name, doc_path, persist_dir in stores_to_build: # elapsed = time.time() - start_time # if elapsed > MAX_BUILD_TIME: # print( # f"⏰ Build time limit reached ({elapsed:.1f}s), creating empty store at {persist_dir}") # os.makedirs(persist_dir, exist_ok=True) # continue # if os.path.exists(doc_path): # doc_files = [f for f in Path(doc_path).rglob( # "*") if f.is_file() and not f.name.startswith('.')] # if doc_files: # print( # f"📄 Found {len(doc_files)} document(s) for {name}") # try: # build_vector_store( # doc_path=doc_path, persist_directory=persist_dir) # print(f"✅ {name} built successfully") # except Exception as e: # print(f"❌ Error building {name}: {str(e)}") # os.makedirs(persist_dir, exist_ok=True) # else: # print(f"⚠️ No documents found in {doc_path}") # os.makedirs(persist_dir, exist_ok=True) # else: # print(f"⚠️ Document path not found: {doc_path}") # os.makedirs(persist_dir, exist_ok=True) # except ImportError as e: # print(f"❌ Could not import vector store utilities: {e}") # for _, _, persist_dir in stores_to_build: # os.makedirs(persist_dir, exist_ok=True) # else: # print("✅ All vector stores already exist!") # print("🎉 Vector store setup completed!") # def start_server(): # """Start the FastAPI server""" # print("🌐 Starting FastAPI server...") # try: # from api.main import app # print("✅ Successfully imported FastAPI app from api.main") # import uvicorn # port = int(os.environ.get("PORT", 7860)) # host = "0.0.0.0" # print(f"🚀 Starting server on {host}:{port}") # uvicorn.run(app, host=host, port=port, # log_level="info", access_log=True) # except ImportError as e: # print(f"❌ Could not import FastAPI app: {e}") # sys.exit(1) # except Exception as e: # print(f"❌ Error starting server: {e}") # sys.exit(1) # if __name__ == "__main__": # print("=" * 60) # print("🎯 RAG Chatbot - HF Spaces Deployment") # print("=" * 60) # # Setup phase # setup_for_spaces() # # Server start phase # start_server() # updated to adjust for HF os #!/usr/bin/env python3 """ HF Spaces deployment launcher for RAG Chatbot Repository structure: rag_app/ is the git root """ import os import sys import time from pathlib import Path # HF Spaces only allows writing to /tmp - CRITICAL: Set ALL cache directories os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache" os.environ["HF_HOME"] = "/tmp/hf_cache" os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/hf_cache" os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/tmp/sentence_transformers_cache" os.environ["TORCH_HOME"] = "/tmp/torch_cache" os.makedirs("/tmp/hf_cache", exist_ok=True) os.makedirs("/tmp/sentence_transformers_cache", exist_ok=True) os.makedirs("/tmp/torch_cache", exist_ok=True) print("🚀 Starting HF Spaces deployment setup...") print(f"📁 Current directory: {os.getcwd()}") print(f"📂 Contents: {os.listdir('.')}") print(f"🗂️ Cache directories configured:") print(f" TRANSFORMERS_CACHE: {os.environ.get('TRANSFORMERS_CACHE')}") print( f" SENTENCE_TRANSFORMERS_HOME: {os.environ.get('SENTENCE_TRANSFORMERS_HOME')}") # Ensure current directory is in Python path sys.path.insert(0, os.getcwd()) # HF Spaces writable path for ephemeral storage # TMP_VECTOR_STORE_ROOT = "/tmp/vector_stores" def setup_for_spaces(): """Setup vector stores and environment for HF Spaces""" print("🔧 Setting up vector stores for HF Spaces...") TMP_VECTOR_STORE_ROOT = "/tmp/vector_stores" os.makedirs(TMP_VECTOR_STORE_ROOT, exist_ok=True) # ✅ ensure subfolders exist for sub in ["mes_db", "tech_db", "general_db"]: os.makedirs(os.path.join(TMP_VECTOR_STORE_ROOT, sub), exist_ok=True) # Ensure docs folders exist in repo required_dirs = [ "./docs", "./docs/mes", "./docs/technical", "./docs/general" ] for directory in required_dirs: os.makedirs(directory, exist_ok=True) exists = "✅" if os.path.exists(directory) else "❌" print(f"{exists} Directory: {directory}") # Map of vector stores (persist dirs now point to /tmp) store_configs = [ ("MES Manual", "docs/mes", os.path.join(TMP_VECTOR_STORE_ROOT, "mes_db")), ("Technical Docs", "docs/technical", os.path.join(TMP_VECTOR_STORE_ROOT, "tech_db")), ("General Docs", "docs/general", os.path.join(TMP_VECTOR_STORE_ROOT, "general_db")), ] stores_to_build = [] for name, doc_path, persist_dir in store_configs: # Check if store already exists in repo or in /tmp if os.path.exists(persist_dir) and os.listdir(persist_dir): print(f"✅ {name} vector store already exists in {persist_dir}") else: stores_to_build.append((name, doc_path, persist_dir)) print(f"🔧 {name} vector store needs building in {persist_dir}") # Build missing vector stores if stores_to_build: print(f"🏗️ Building {len(stores_to_build)} vector store(s)...") start_time = time.time() MAX_BUILD_TIME = 600 # seconds try: # Import after setting environment variables from utils.vector_store import build_vector_store print("✅ Vector store utilities imported successfully") for name, doc_path, persist_dir in stores_to_build: elapsed = time.time() - start_time if elapsed > MAX_BUILD_TIME: print( f"⏰ Build time limit reached ({elapsed:.1f}s), creating empty store at {persist_dir}") os.makedirs(persist_dir, exist_ok=True) continue if os.path.exists(doc_path): doc_files = [f for f in Path(doc_path).rglob( "*") if f.is_file() and not f.name.startswith('.')] if doc_files: print( f"📄 Found {len(doc_files)} document(s) for {name}") try: build_vector_store( doc_path=doc_path, persist_directory=persist_dir) print(f"✅ {name} built successfully") except Exception as e: print(f"❌ Error building {name}: {str(e)}") os.makedirs(persist_dir, exist_ok=True) else: print(f"⚠️ No documents found in {doc_path}") os.makedirs(persist_dir, exist_ok=True) else: print(f"⚠️ Document path not found: {doc_path}") os.makedirs(persist_dir, exist_ok=True) except ImportError as e: print(f"❌ Could not import vector store utilities: {e}") for _, _, persist_dir in stores_to_build: os.makedirs(persist_dir, exist_ok=True) else: print("✅ All vector stores already exist!") print("🎉 Vector store setup completed!") def start_server(): """Start the FastAPI server""" print("🌐 Starting FastAPI server...") try: # Import after environment setup from api.main import app print("✅ Successfully imported FastAPI app from api.main") import uvicorn port = int(os.environ.get("PORT", 7860)) host = "0.0.0.0" print(f"🚀 Starting server on {host}:{port}") print(f"📡 FastAPI endpoints available at: http://{host}:{port}/") print(f"🎨 Gradio interface available at: http://{host}:{port}/gradio") uvicorn.run(app, host=host, port=port, log_level="info", access_log=True) except ImportError as e: print(f"❌ Could not import FastAPI app: {e}") sys.exit(1) except Exception as e: print(f"❌ Error starting server: {e}") sys.exit(1) if __name__ == "__main__": print("=" * 60) print("🎯 RAG Chatbot - HF Spaces Deployment") print("=" * 60) # Setup phase setup_for_spaces() # Server start phase start_server()