jamiya / scripts /run.sh
jameszokah's picture
Update Dockerfile and application imports: modify PYTHONPATH in Dockerfile and run script to include additional app directory, enhance model imports in audiobook_routes.py and migrations/env.py, and add missing imports in main.py.
857bebe
raw
history blame
2.21 kB
#!/bin/bash
set -e
# Initialize environment variables with defaults if not set
export PORT=${PORT:-7860}
export HOST=${HOST:-"0.0.0.0"}
export WORKERS=${WORKERS:-1}
export LOG_LEVEL=${LOG_LEVEL:-"info"}
export DATABASE_URL=${DATABASE_URL:-"sqlite:///app/storage/audiobooks.db"}
# Set Python path to include both app directories
export PYTHONPATH="/app:/app/app:${PYTHONPATH:-}"
# Create storage directories if they don't exist
mkdir -p /app/storage/audio
mkdir -p /app/storage/text
mkdir -p /app/storage/temp
# Initialize database if using SQLite
if [[ "$DATABASE_URL" == sqlite* ]]; then
echo "Using SQLite database at: $DATABASE_URL"
# Create the database directory if it doesn't exist
DB_DIR=$(dirname "${DATABASE_URL#sqlite:///}")
mkdir -p "$DB_DIR"
# Initialize alembic if not already initialized
if [ ! -d "migrations" ]; then
echo "Initializing alembic..."
alembic init migrations
fi
# Create initial migration if none exists
if [ ! -d "migrations/versions" ] || [ -z "$(ls -A migrations/versions)" ]; then
echo "Creating initial migration..."
alembic revision --autogenerate -m "Initial migration"
fi
fi
# Wait for database to be ready (if using external database)
if [ ! -z "$DATABASE_URL" ] && [[ "$DATABASE_URL" != sqlite* ]]; then
host=$(echo $DATABASE_URL | awk -F[@//] '{print $4}' | cut -d: -f1)
port=$(echo $DATABASE_URL | awk -F[@//] '{print $4}' | cut -d: -f2 | cut -d/ -f1)
echo "Waiting for database at $host:$port..."
while ! nc -z $host $port; do
sleep 1
done
echo "Database is ready!"
fi
# Run database migrations
echo "Running database migrations..."
PYTHONPATH="/app:/app/app:${PYTHONPATH:-}" alembic upgrade head || {
echo "Warning: Database migration failed. This might be expected for first run."
echo "Continuing with application startup..."
}
echo "Starting CSM-1B TTS API server..."
echo "Host: $HOST"
echo "Port: $PORT"
echo "Workers: $WORKERS"
echo "Log Level: $LOG_LEVEL"
# Start the FastAPI application using uvicorn
exec uvicorn app.main:app \
--host "$HOST" \
--port "$PORT" \
--workers "$WORKERS" \
--log-level "$LOG_LEVEL"