#!/bin/bash set -e echo "=== Anime Generator API - Starting ===" # --- Persistent storage setup --- if [ -d "/data" ]; then echo "Using persistent storage at /data" mkdir -p /data/workdir/projects ln -sfn /data/workdir /app/workdir mkdir -p /data/genshin_voices /data/animevox_voices ln -sfn /data/genshin_voices /app/data/genshin_voices ln -sfn /data/animevox_voices /app/data/animevox_voices else echo "No persistent storage — using ephemeral /app" mkdir -p /app/workdir/projects mkdir -p /app/data/genshin_voices mkdir -p /app/data/animevox_voices fi # --- Start Uvicorn FIRST (must be healthy within 30 min) --- # Voice setup runs in background AFTER the server is up. echo "=== Starting Uvicorn on port 7860 ===" python -m uvicorn app.main:app --host 0.0.0.0 --port 7860 & UVICORN_PID=$! # Wait for server to be ready echo "Waiting for server to be ready..." for i in $(seq 1 30); do if curl -s http://localhost:7860/api/health > /dev/null 2>&1; then echo "Server is healthy!" break fi sleep 1 done # --- Background voice setup (won't block health check) --- ( echo "=== Background: Voice setup starting ===" # Clear stale model cache if needed if [ -n "$HF_TOKEN" ]; then CACHE_DIR="$HOME/.cache/huggingface/hub" if [ -d "$CACHE_DIR" ]; then HAS_CLONING=$(python -c " from pocket_tts import TTSModel m = TTSModel.load_model() print(m.has_voice_cloning) " 2>/dev/null || echo "False") if [ "$HAS_CLONING" = "False" ]; then echo "Clearing stale model cache for voice cloning..." rm -rf "$CACHE_DIR/models--kyutai--pocket-tts"* fi fi fi # Genshin voices GENSHIN_COUNT=$(find /app/data/genshin_voices -name "voice_state.safetensors" 2>/dev/null | wc -l) if [ "$GENSHIN_COUNT" -lt 5 ]; then echo "Background: Downloading Genshin voice data..." python scripts/setup_genshin_voices.py || echo "WARNING: Genshin voice setup failed" else echo "Background: Genshin voices already set up ($GENSHIN_COUNT characters)" fi # AnimeVox voices ANIMEVOX_COUNT=$(find /app/data/animevox_voices -name "voice_state.safetensors" 2>/dev/null | wc -l) if [ "$ANIMEVOX_COUNT" -lt 5 ]; then echo "Background: Downloading AnimeVox voice data..." echo "y" | python scripts/setup_animevox.py || echo "WARNING: AnimeVox voice setup failed" else echo "Background: AnimeVox voices already set up ($ANIMEVOX_COUNT characters)" fi echo "=== Background: Voice setup complete ===" ) & # Keep Uvicorn in foreground wait $UVICORN_PID