JatsTheAIGen commited on
Commit
4e3f60e
·
1 Parent(s): 8f4d405

Fix database path issue for Docker/HF Spaces - use /tmp for writable directory

Browse files
Files changed (3) hide show
  1. Dockerfile +2 -0
  2. config.py +3 -2
  3. src/context_manager.py +13 -2
Dockerfile CHANGED
@@ -32,6 +32,8 @@ EXPOSE 7860
32
  # Set environment variables
33
  ENV PYTHONUNBUFFERED=1
34
  ENV PORT=7860
 
 
35
 
36
  # Health check
37
  HEALTHCHECK --interval=30s --timeout=30s --start-period=120s --retries=3 \
 
32
  # Set environment variables
33
  ENV PYTHONUNBUFFERED=1
34
  ENV PORT=7860
35
+ ENV OMP_NUM_THREADS=1
36
+ ENV MKL_NUM_THREADS=1
37
 
38
  # Health check
39
  HEALTHCHECK --interval=30s --timeout=30s --start-period=120s --retries=3 \
config.py CHANGED
@@ -17,8 +17,9 @@ class Settings(BaseSettings):
17
  cache_ttl: int = int(os.getenv("CACHE_TTL", "3600"))
18
 
19
  # Database settings
20
- db_path: str = os.getenv("DB_PATH", "sessions.db")
21
- faiss_index_path: str = os.getenv("FAISS_INDEX_PATH", "embeddings.faiss")
 
22
 
23
  # Session settings
24
  session_timeout: int = int(os.getenv("SESSION_TIMEOUT", "3600"))
 
17
  cache_ttl: int = int(os.getenv("CACHE_TTL", "3600"))
18
 
19
  # Database settings
20
+ # Use /tmp for writable location in Docker containers
21
+ db_path: str = os.getenv("DB_PATH", "/tmp/sessions.db")
22
+ faiss_index_path: str = os.getenv("FAISS_INDEX_PATH", "/tmp/embeddings.faiss")
23
 
24
  # Session settings
25
  session_timeout: int = int(os.getenv("SESSION_TIMEOUT", "3600"))
src/context_manager.py CHANGED
@@ -6,6 +6,8 @@ import uuid
6
  import hashlib
7
  import threading
8
  import time
 
 
9
  from contextlib import contextmanager
10
  from datetime import datetime, timedelta
11
  from typing import Dict, Optional, List
@@ -49,7 +51,7 @@ class TransactionManager:
49
  conn.close()
50
 
51
  class EfficientContextManager:
52
- def __init__(self, llm_router=None):
53
  self.session_cache = {} # In-memory for active sessions
54
  self._session_cache = {} # Enhanced in-memory cache with timestamps
55
  self.cache_config = {
@@ -58,7 +60,16 @@ class EfficientContextManager:
58
  "compression": "gzip",
59
  "eviction_policy": "LRU"
60
  }
61
- self.db_path = "sessions.db"
 
 
 
 
 
 
 
 
 
62
  self.llm_router = llm_router # For generating context summaries
63
  logger.info(f"Initializing ContextManager with DB path: {self.db_path}")
64
  self.transaction_manager = TransactionManager(self.db_path)
 
6
  import hashlib
7
  import threading
8
  import time
9
+ import os
10
+ from pathlib import Path
11
  from contextlib import contextmanager
12
  from datetime import datetime, timedelta
13
  from typing import Dict, Optional, List
 
51
  conn.close()
52
 
53
  class EfficientContextManager:
54
+ def __init__(self, llm_router=None, db_path=None):
55
  self.session_cache = {} # In-memory for active sessions
56
  self._session_cache = {} # Enhanced in-memory cache with timestamps
57
  self.cache_config = {
 
60
  "compression": "gzip",
61
  "eviction_policy": "LRU"
62
  }
63
+ # Use provided db_path or get from config/env, default to /tmp for Docker
64
+ if db_path is None:
65
+ from config import settings
66
+ db_path = settings.db_path
67
+ self.db_path = db_path
68
+ # Ensure directory exists
69
+ db_dir = os.path.dirname(self.db_path)
70
+ if db_dir and not os.path.exists(db_dir):
71
+ os.makedirs(db_dir, exist_ok=True)
72
+ logger.info(f"Created database directory: {db_dir}")
73
  self.llm_router = llm_router # For generating context summaries
74
  logger.info(f"Initializing ContextManager with DB path: {self.db_path}")
75
  self.transaction_manager = TransactionManager(self.db_path)