File size: 5,627 Bytes
2e3f13f
4fb6fe9
de66cf1
 
6b5d3ca
de66cf1
56e425c
 
 
 
 
 
de66cf1
 
 
 
 
 
 
 
86bb0f2
de66cf1
 
 
 
6b5d3ca
de66cf1
 
 
 
 
6b5d3ca
 
de66cf1
 
 
12b5e87
4fb6fe9
 
 
 
 
 
 
12b5e87
 
 
1af2a91
4fb6fe9
12b5e87
6b5d3ca
de66cf1
 
 
 
 
 
86bb0f2
de66cf1
387a013
de66cf1
8a8c3b9
12205b7
cd7ff22
2c9ce5e
86bb0f2
8a8c3b9
 
 
 
 
 
 
51afead
e0f1d78
de66cf1
86bb0f2
de66cf1
 
56e425c
 
 
de66cf1
 
 
 
91504b2
2e3f13f
 
de66cf1
 
 
 
 
56e425c
de66cf1
 
6b5d3ca
de66cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fb6fe9
 
 
 
43be60a
4fb6fe9
 
 
 
dcdd6cf
56e425c
86bb0f2
56e425c
4fb6fe9
 
 
 
 
 
de66cf1
 
2b2ba28
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Hugging Face Spaces - MrrrMe with Coqui XTTS v2 + MODULAR BACKEND
# FIXED v2.4: Proper permissions and cache setup for user 1000
FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04

# Install system dependencies
RUN apt-get update && apt-get install -y \
    bash \
    git \
    git-lfs \
    wget \
    curl \
    procps \
    python3.11 \
    python3-pip \
    python3.11-dev \
    libgl1-mesa-glx \
    libglib2.0-0 \
    ffmpeg \
    portaudio19-dev \
    libsndfile1 \
    espeak-ng \
    nginx \
    gnupg \
    && rm -rf /var/lib/apt/lists/*

# Install Node.js 20
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get install -y nodejs && \
    rm -rf /var/lib/apt/lists/*

# Set Python 3.11 as default
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1 && \
    update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1

WORKDIR /app

# ============================================================
# CRITICAL: Create cache directories with correct ownership EARLY
# ============================================================
RUN mkdir -p /data/modelscope /data/huggingface /home/user/.cache && \
    chown -R 1000:1000 /data /home/user

# ============================================================
# PERSISTENT STORAGE - Set as root user (before USER 1000 switch)
# ============================================================
ENV MODELSCOPE_CACHE=/data/modelscope
ENV MS_CACHE_HOME=/data/modelscope
ENV HF_HOME=/data/huggingface
ENV FUNASR_CACHE=/data/modelscope

# Install PyTorch with CUDA 11.8
RUN python3.11 -m pip install --no-cache-dir \
    torch==2.4.0 \
    torchvision==0.19.0 \
    torchaudio==2.4.0 \
    --index-url https://download.pytorch.org/whl/cu118

ENV COQUI_TOS_AGREED=1

# Install Python dependencies
COPY requirements_docker.txt ./
# CRITICAL: Install funasr with --no-deps to avoid gradio hell
RUN python3.11 -m pip install --no-cache-dir -r requirements_docker.txt && \
    python3.11 -m pip install --no-cache-dir funasr>=1.0.0 --no-deps


# ============================================================
# BAKE MODELS INTO IMAGE (Optimized Build)
# ============================================================
COPY download_models.py .
RUN python3.11 download_models.py && \
    rm download_models.py


# Install avatar dependencies
RUN python3.11 -m pip install --no-cache-dir \
    fastapi uvicorn python-multipart pydub websockets onnxruntime

# Copy application code
COPY --link --chown=1000:1000 mrrrme/ ./mrrrme/
COPY --link --chown=1000:1000 model/ ./model/
COPY --link --chown=1000:1000 avatar/ ./avatar/

# Create directories
RUN mkdir -p /app/weights /app/avatar/static

# Fix openface bug
RUN python3.11 -c "import os; fp='/usr/local/lib/python3.11/dist-packages/openface/multitask_model.py'; c=open(fp).read() if os.path.exists(fp) else ''; exec(\"if os.path.exists(fp) and 'import cv2' not in c:\\n    open(fp,'w').write('import cv2\\\\n'+c)\\n    print('Patched')\")"

# Build frontend
COPY avatar-frontend/package*.json ./frontend/
WORKDIR /app/frontend
RUN npm ci

COPY --link --chown=1000:1000 avatar-frontend/ ./
RUN npm run build

# Copy static files
RUN cp -r .next/static .next/standalone/.next/ && \
    cp -r public .next/standalone/ 2>/dev/null || true

WORKDIR /app

# Copy nginx config
COPY nginx.spaces.conf /etc/nginx/nginx.conf

# Generate SSL certificates
RUN mkdir -p /etc/nginx/certs && \
    openssl req -x509 -newkey rsa:4096 -nodes \
    -keyout /etc/nginx/certs/key.pem \
    -out /etc/nginx/certs/cert.pem \
    -days 365 \
    -subj "/CN=mrrrme.hf.space"

# ============================================================
# Startup script with proper environment
# ============================================================
RUN printf '#!/bin/bash\nset -e\n\n# Set HOME for user 1000\nexport HOME=/home/user\n\n# Agree to TOS\nexport COQUI_TOS_AGREED=1\n\n# ============================================================\n# CACHE DIRECTORIES - Set for runtime user\n# ============================================================\nexport MODELSCOPE_CACHE=/data/modelscope\nexport MS_CACHE_HOME=/data/modelscope\nexport HF_HOME=/data/huggingface\nexport FUNASR_CACHE=/data/modelscope\n\necho "πŸ“ Model cache: $MODELSCOPE_CACHE"\n\n# Ensure cache directories are writable\nmkdir -p $MODELSCOPE_CACHE $HF_HOME 2>/dev/null || true\n\n# Clean up old processes\npkill -f "backend_new.py" 2>/dev/null || true\npkill -f "speak_server.py" 2>/dev/null || true\npkill -f "node server.js" 2>/dev/null || true\npkill -f "nginx" 2>/dev/null || true\n\nsleep 2\n\necho "πŸš€ Starting MrrrMe..."\necho "πŸ“¦ emotion2vec will load from /data (first run downloads ~2min, then instant)"\n\n# Start services\ncd /app && python3.11 mrrrme/backend_new.py &\ncd /app/avatar && python3.11 speak_server.py &\ncd /app/frontend/.next/standalone && HOSTNAME=0.0.0.0 PORT=3001 node server.js &\n\nsleep 10\nnginx -g "daemon off;" &\n\necho "βœ… All services running!"\nwait\n' > /app/start.sh && chmod +x /app/start.sh

# ============================================================
# FINAL: Set ownership of everything to user 1000
# ============================================================
RUN chown -R 1000:1000 /app /data /home/user

USER 1000
ENV HOME=/home/user

# Re-export cache variables for user 1000
ENV MODELSCOPE_CACHE=/data/modelscope
ENV MS_CACHE_HOME=/data/modelscope
ENV HF_HOME=/data/huggingface
ENV FUNASR_CACHE=/data/modelscope

EXPOSE 7860

CMD ["/app/start.sh"]