File size: 2,682 Bytes
7190fd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d93056a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ce54e4
 
 
 
d93056a
 
 
 
2ce54e4
 
 
d93056a
 
 
 
 
 
 
 
7190fd0
d93056a
 
 
 
 
 
 
 
7190fd0
d93056a
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/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