Spaces:
Running
๐๏ธ 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: โ
- backend/app.py +16 -1
- backend/server_config.py +4 -1
|
@@ -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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
|
@@ -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
|