Automate SAM download and fix database path issues

- Fix database path mismatch: download_sample_eyes.py now uses
  ai_photo_edit.db instead of photoedit.db
- Add init_database.py script to initialize DB before eye import
- Add AUTO_DOWNLOAD_SAM=true environment variable (default: enabled)
- Update entrypoint.sh to:
  1. Initialize database first
  2. Auto-download SAM model (~375MB) on first startup
  3. Then import eyes (now works since DB exists)
- Update path detection to work in both Docker and local environments
This commit is contained in:
Claude
2026-01-25 20:28:57 +00:00
parent 70cd0d6ea3
commit df4ddc2d8c
5 changed files with 123 additions and 32 deletions
+24 -7
View File
@@ -106,17 +106,34 @@ def import_eye_to_database(db_path: Path, eye_data: dict, patch_path: str, thumb
return patch_id
def main():
# Determine paths
script_dir = Path(__file__).parent
project_root = script_dir.parent
data_dir = project_root / 'data'
db_path = data_dir / 'photoedit.db'
# Determine paths - handle both Docker and local environments
# In Docker: script is at /scripts/, data is at /app/data/
# Locally: script is at ./scripts/, data is at ./data/
docker_data_dir = Path('/app/data')
local_data_dir = Path(__file__).parent.parent / 'data'
if docker_data_dir.exists():
data_dir = docker_data_dir
else:
data_dir = local_data_dir
db_path = data_dir / 'ai_photo_edit.db'
# Check if database exists
if not db_path.exists():
print(f"Database not found at {db_path}")
print("Please start the backend first to initialize the database.")
sys.exit(1)
print("Attempting to initialize database...")
# Try to import and initialize database
try:
sys.path.insert(0, str(Path('/app')))
sys.path.insert(0, str(Path(__file__).parent.parent / 'backend'))
from app.database import init_db
init_db()
print("Database initialized successfully.")
except Exception as e:
print(f"Could not initialize database: {e}")
print("Please start the backend first to initialize the database.")
sys.exit(1)
print(f"Using database: {db_path}")
print(f"Data directory: {data_dir}")
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""
Initialize the database before other startup scripts run.
This ensures the database exists and has all required tables
before download_sample_eyes.py tries to use it.
"""
import os
import sys
from pathlib import Path
# Add backend to path - handle both Docker and local environments
# In Docker: backend is at /app/
# Locally: backend is at ./backend/
if Path('/app').exists():
sys.path.insert(0, '/app')
else:
sys.path.insert(0, str(Path(__file__).parent.parent / 'backend'))
def main():
# Import after path setup
from app.database import engine, Base, init_db
from app.models import project, user, patch
print("Initializing database...")
# Create all tables
init_db()
# Verify database was created - check both Docker and local paths
docker_db_path = Path('/app/data/ai_photo_edit.db')
local_db_path = Path('./data/ai_photo_edit.db')
if docker_db_path.exists():
print(f"✓ Database initialized at: {docker_db_path}")
elif local_db_path.exists():
print(f"✓ Database initialized at: {local_db_path}")
else:
print("⚠ Database file not found at expected locations, but tables may still be created")
print("Database initialization complete.")
if __name__ == '__main__':
main()