wu981526092 commited on
Commit
675e977
ยท
1 Parent(s): d9fe982

๐Ÿ—„๏ธ Fix SQLite database connection error in HF Spaces

Browse files

โœ… Problem solved:
โ€ข Added 'datasets/db' directory creation in ensure_directories()
โ€ข Added automatic database initialization on app startup
โ€ข Enhanced startup logging with emojis for better visibility

๐ŸŽฏ Root cause:
โ€ข HF Spaces container didn't have the datasets/db directory
โ€ข Database initialization was not called during app startup
โ€ข SQLite couldn't create database file without parent directory

โšก Fixes applied:
โ€ข server_config.py: Added datasets/db + cache + evaluation_results dirs
โ€ข app.py: Added database initialization in startup_event()
โ€ข Added proper error handling - app continues if DB init fails
โ€ข Enhanced logging with status emojis (โœ…๐Ÿ“๐Ÿ—„๏ธ๐Ÿš€โŒ)

๐Ÿš€ Result: Database will be auto-created on first startup in HF Spaces

๐Ÿงช Local testing: โœ… Passed
โ€ข Directory creation: โœ…
โ€ข Database initialization: โœ… (176KB, 10 tables)
โ€ข API connection: โœ… (0 traces found - expected for new DB)
โ€ข Application import: โœ…

Files changed (2) hide show
  1. backend/app.py +16 -1
  2. backend/server_config.py +4 -1
backend/app.py CHANGED
@@ -67,7 +67,22 @@ app.include_router(observability.router)
67
  @app.on_event("startup")
68
  async def startup_event():
69
  """Start background services on app startup"""
70
- logger.info("Starting background services...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  # scheduler_service.start() # This line is now commented out
72
 
73
  @app.on_event("shutdown")
 
67
  @app.on_event("startup")
68
  async def startup_event():
69
  """Start background services on app startup"""
70
+ logger.info("โœ… Backend server starting...")
71
+
72
+ # ๐Ÿ”ง Create necessary directories
73
+ ensure_directories()
74
+ logger.info("๐Ÿ“ Directory structure created")
75
+
76
+ # ๐Ÿ—„๏ธ Initialize database on startup
77
+ try:
78
+ from backend.database.init_db import init_database
79
+ init_database(reset=False, force=False)
80
+ logger.info("๐Ÿ—„๏ธ Database initialized successfully")
81
+ except Exception as e:
82
+ logger.error(f"โŒ Database initialization failed: {e}")
83
+ # Don't fail startup - continue with empty database
84
+
85
+ logger.info("๐Ÿš€ Backend API available at: http://0.0.0.0:7860")
86
  # scheduler_service.start() # This line is now commented out
87
 
88
  @app.on_event("shutdown")
backend/server_config.py CHANGED
@@ -33,8 +33,11 @@ def ensure_directories():
33
  """Create necessary directories if they don't exist"""
34
  directories = [
35
  "logs",
 
36
  "datasets/test_results",
37
- "datasets/knowledge_graphs"
 
 
38
  ]
39
  for directory in directories:
40
  full_path = PROJECT_ROOT / directory
 
33
  """Create necessary directories if they don't exist"""
34
  directories = [
35
  "logs",
36
+ "datasets/db", # โœ… ๆทปๅŠ ๆ•ฐๆฎๅบ“็›ฎๅฝ•
37
  "datasets/test_results",
38
+ "datasets/knowledge_graphs",
39
+ "cache",
40
+ "evaluation_results"
41
  ]
42
  for directory in directories:
43
  full_path = PROJECT_ROOT / directory