MogensR commited on
Commit
f8cdc0a
Β·
1 Parent(s): 49413f6
VideoBackgroundReplacer2/Dockerfile CHANGED
@@ -1,11 +1,13 @@
1
  # ===============================
2
  # Hugging Face Space β€” Stable Dockerfile
3
- # CUDA 12.1.1 + PyTorch 2.5.1 (cu121) + Streamlit 1.32.0
4
- # SAM2 installed from source; MatAnyone via pip (repo)
5
  # ===============================
6
 
7
- # Set Python version explicitly
8
  ARG PYTHON_VERSION=3.10
 
 
9
  FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
10
 
11
  # ---- Environment (runtime hygiene) ----
@@ -26,11 +28,6 @@ ENV DEBIAN_FRONTEND=noninteractive \
26
  STREAMLIT_SERVER_HEADLESS=true \
27
  STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
28
 
29
- # ---- System cleanup ----
30
- RUN rm -rf /var/lib/apt/lists/* && \
31
- apt-get clean && \
32
- rm -rf /tmp/* /var/tmp/*
33
-
34
  # ---- Non-root user ----
35
  RUN useradd -m -u 1000 user
36
  ENV HOME=/home/user
@@ -38,15 +35,17 @@ WORKDIR $HOME/app
38
 
39
  # ---- System deps ----
40
  RUN apt-get update && apt-get install -y --no-install-recommends \
41
- git ffmpeg wget curl \
42
  python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python3-pip python${PYTHON_VERSION}-venv \
43
  build-essential gcc g++ pkg-config \
44
  libffi-dev libssl-dev libc6-dev \
45
  libgl1-mesa-glx libglib2.0-0 libsm6 libxext6 libxrender1 libgomp1 \
46
- && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION} 1 \
47
- && update-alternatives --set python3 /usr/bin/python${PYTHON_VERSION} \
48
- && ln -sf /usr/bin/python${PYTHON_VERSION} /usr/bin/python \
49
- && rm -rf /var/lib/apt/lists/*
 
 
50
 
51
  # ---- Python bootstrap ----
52
  RUN python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel
@@ -54,7 +53,7 @@ RUN python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel
54
  # ---- Install PyTorch (CUDA 12.1 wheels) ----
55
  RUN python3 -m pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cu121 \
56
  torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 \
57
- && python3 - <<'PY'
58
  import torch
59
  print("PyTorch:", torch.__version__)
60
  print("CUDA available:", torch.cuda.is_available())
@@ -62,18 +61,28 @@ print("torch.version.cuda:", getattr(torch.version, "cuda", None))
62
  PY
63
 
64
  # ---- Copy deps first (better caching) ----
 
 
65
  COPY --chown=user:user requirements.txt ./
66
 
67
- # ---- Install remaining Python deps with force reinstall ----
68
- RUN pip install --no-cache-dir --upgrade pip && \
69
- pip install --no-cache-dir -r requirements.txt --force-reinstall
 
 
 
 
 
70
 
71
  # ---- MatAnyone (pip install from repo with retry) ----
72
  RUN echo "Installing MatAnyone..." && \
73
  (python3 -m pip install --no-cache-dir -v git+https://github.com/pq-yang/MatAnyone@main#egg=matanyone || \
74
  (echo "Retrying MatAnyone..." && \
75
  python3 -m pip install --no-cache-dir -v git+https://github.com/pq-yang/MatAnyone@main#egg=matanyone)) && \
76
- python3 -c "import matanyone; print('MatAnyone import OK')"
 
 
 
77
 
78
  # ---- App code ----
79
  COPY --chown=user:user . .
@@ -93,6 +102,36 @@ ENV PYTHONPATH=/home/user/app:/home/user/app/third_party:/home/user/app/third_pa
93
  TF_CPP_MIN_LOG_LEVEL=2 \
94
  SAM2_CHECKPOINT=/home/user/app/checkpoints/sam2_hiera_large.pt
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  # ---- Create writable dirs (caches + checkpoints) ----
97
  RUN mkdir -p /home/user/app/checkpoints /home/user/app/.hf /home/user/app/.torch && \
98
  chown -R user:user /home/user/app && \
@@ -102,17 +141,17 @@ RUN mkdir -p /home/user/app/checkpoints /home/user/app/.hf /home/user/app/.torch
102
  chmod +x /home/user/app/app.py || true
103
 
104
  # ---- Healthcheck ----
105
- HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD \
106
- ["python3","-c","import torch; print('torch', torch.__version__, '| cuda', getattr(torch.version,'cuda',None), '| ok=', torch.cuda.is_available())"]
107
 
108
  # ---- Runtime ----
109
  USER user
110
  EXPOSE 8501
111
 
112
- # Streamlit server command
113
  CMD ["sh", "-c", "\
114
  echo '===========================================' && \
115
- echo '=== MYAVATAR STREAMLIT CONTAINER STARTUP ===' && \
116
  echo '===========================================' && \
117
  echo 'Timestamp:' $(date) && \
118
  echo 'Current directory:' $(pwd) && \
@@ -120,7 +159,6 @@ CMD ["sh", "-c", "\
120
  echo 'User ID:' $(id) && \
121
  echo '' && \
122
  echo '=== FILE SYSTEM CHECK ===' && \
123
- echo 'Files in app directory:' && \
124
  ls -la && \
125
  echo '' && \
126
  echo '=== PYTHON ENVIRONMENT ===' && \
@@ -135,7 +173,7 @@ CMD ["sh", "-c", "\
135
  echo 'Testing Python imports...' && \
136
  python3 -B -c 'import streamlit; print(\"βœ… Streamlit:\", streamlit.__version__)' && \
137
  python3 -B -c 'import torch; print(\"βœ… Torch:\", torch.__version__)' && \
138
- echo 'Testing app.py import...' && \
139
  python3 -B -c 'import sys; sys.path.insert(0, \".\"); import app; print(\"βœ… app.py imports successfully\")' && \
140
  echo 'βœ… All checks passed!'; \
141
  else \
@@ -145,4 +183,4 @@ CMD ["sh", "-c", "\
145
  echo '' && \
146
  echo '=== STARTING STREAMLIT SERVER ===' && \
147
  streamlit run --server.port=8501 --server.address=0.0.0.0 app.py \
148
- "]
 
1
  # ===============================
2
  # Hugging Face Space β€” Stable Dockerfile
3
+ # CUDA 12.1.1 + PyTorch 2.5.1 (cu121) + Streamlit 1.32+
4
+ # SAM2 from source; MatAnyone via pip (repo)
5
  # ===============================
6
 
7
+ # ---- Build args (cache buster) ----
8
  ARG PYTHON_VERSION=3.10
9
+ ARG BUILD_TS
10
+
11
  FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
12
 
13
  # ---- Environment (runtime hygiene) ----
 
28
  STREAMLIT_SERVER_HEADLESS=true \
29
  STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
30
 
 
 
 
 
 
31
  # ---- Non-root user ----
32
  RUN useradd -m -u 1000 user
33
  ENV HOME=/home/user
 
35
 
36
  # ---- System deps ----
37
  RUN apt-get update && apt-get install -y --no-install-recommends \
38
+ git ffmpeg wget curl ca-certificates \
39
  python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python3-pip python${PYTHON_VERSION}-venv \
40
  build-essential gcc g++ pkg-config \
41
  libffi-dev libssl-dev libc6-dev \
42
  libgl1-mesa-glx libglib2.0-0 libsm6 libxext6 libxrender1 libgomp1 \
43
+ # WebRTC deps for aiortc / streamlit-webrtc (Jammy uses libvpx6)
44
+ libopus0 libvpx6 libsrtp2-1 \
45
+ && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION} 1 \
46
+ && update-alternatives --set python3 /usr/bin/python${PYTHON_VERSION} \
47
+ && ln -sf /usr/bin/python${PYTHON_VERSION} /usr/bin/python \
48
+ && rm -rf /var/lib/apt/lists/*
49
 
50
  # ---- Python bootstrap ----
51
  RUN python3 -m pip install --no-cache-dir --upgrade pip setuptools wheel
 
53
  # ---- Install PyTorch (CUDA 12.1 wheels) ----
54
  RUN python3 -m pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cu121 \
55
  torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 \
56
+ && python3 - <<'PY'
57
  import torch
58
  print("PyTorch:", torch.__version__)
59
  print("CUDA available:", torch.cuda.is_available())
 
61
  PY
62
 
63
  # ---- Copy deps first (better caching) ----
64
+ # Cache-buster echo shows up in build logs to prove fresh layer
65
+ ENV BUILD_TS_ENV=${BUILD_TS}
66
  COPY --chown=user:user requirements.txt ./
67
 
68
+ # ---- Guard & print requirements.txt (diagnostics) ----
69
+ RUN echo "BUILD_TS=${BUILD_TS_ENV}" && \
70
+ echo "=== requirements.txt (as copied) ===" && ls -l requirements.txt && echo "----" && sed -n '1,200p' requirements.txt && echo "---- END ----" && \
71
+ # HARD GUARD: fail if the bad pin exists anywhere in the context copy
72
+ (! grep -R "annotated-types==0\.60\.0" -n .) || (echo "❌ Found forbidden pin annotated-types==0.60.0" && exit 1)
73
+
74
+ # ---- Install remaining Python deps ----
75
+ RUN pip install --no-cache-dir -r requirements.txt
76
 
77
  # ---- MatAnyone (pip install from repo with retry) ----
78
  RUN echo "Installing MatAnyone..." && \
79
  (python3 -m pip install --no-cache-dir -v git+https://github.com/pq-yang/MatAnyone@main#egg=matanyone || \
80
  (echo "Retrying MatAnyone..." && \
81
  python3 -m pip install --no-cache-dir -v git+https://github.com/pq-yang/MatAnyone@main#egg=matanyone)) && \
82
+ python3 - <<'PY'
83
+ import matanyone
84
+ print("βœ… MatAnyone import OK:", getattr(matanyone, "__version__", "unknown"))
85
+ PY
86
 
87
  # ---- App code ----
88
  COPY --chown=user:user . .
 
102
  TF_CPP_MIN_LOG_LEVEL=2 \
103
  SAM2_CHECKPOINT=/home/user/app/checkpoints/sam2_hiera_large.pt
104
 
105
+ # ---- Optional: fetch SAM2 checkpoint if missing ----
106
+ RUN python3 - <<'PY'
107
+ import os, pathlib, urllib.request
108
+ ckpt = os.environ.get("SAM2_CHECKPOINT","/home/user/app/checkpoints/sam2_hiera_large.pt")
109
+ pathlib.Path(ckpt).parent.mkdir(parents=True, exist_ok=True)
110
+ if not os.path.exists(ckpt):
111
+ url = "https://dl.fbaipublicfiles.com/segment_anything_2/checkpoints/sam2_hiera_large.pt"
112
+ try:
113
+ print("Downloading SAM2 checkpoint:", url)
114
+ urllib.request.urlretrieve(url, ckpt)
115
+ print("Saved to:", ckpt)
116
+ except Exception as e:
117
+ print("⚠️ Could not auto-download SAM2 checkpoint:", e)
118
+ else:
119
+ print("SAM2 checkpoint already present at", ckpt)
120
+ PY
121
+
122
+ # ---- Quick sanity imports (catch cv2/mediapipe surprises early) ----
123
+ RUN python3 - <<'PY'
124
+ import cv2, sys
125
+ print("βœ… OpenCV", cv2.__version__)
126
+ import mediapipe as mp
127
+ print("βœ… MediaPipe", mp.__version__)
128
+ import annotated_types, pydantic, pydantic_core
129
+ print("βœ… annotated_types", annotated_types.__version__)
130
+ print("βœ… pydantic", pydantic.__version__, "| pydantic-core", pydantic_core.__version__)
131
+ import torch
132
+ print("βœ… torch", torch.__version__, "| cuda avail =", torch.cuda.is_available())
133
+ PY
134
+
135
  # ---- Create writable dirs (caches + checkpoints) ----
136
  RUN mkdir -p /home/user/app/checkpoints /home/user/app/.hf /home/user/app/.torch && \
137
  chown -R user:user /home/user/app && \
 
141
  chmod +x /home/user/app/app.py || true
142
 
143
  # ---- Healthcheck ----
144
+ HEALTHCHECK --interval=30s --timeout=8s --retries=3 CMD \
145
+ ["python3","-c","import torch; import sys; print('torch', torch.__version__, '| cuda', getattr(torch.version,'cuda',None), '| ok=', torch.cuda.is_available())"]
146
 
147
  # ---- Runtime ----
148
  USER user
149
  EXPOSE 8501
150
 
151
+ # ---- Startup: verify imports, then run Streamlit ----
152
  CMD ["sh", "-c", "\
153
  echo '===========================================' && \
154
+ echo '=== STREAMLIT CONTAINER STARTUP ===' && \
155
  echo '===========================================' && \
156
  echo 'Timestamp:' $(date) && \
157
  echo 'Current directory:' $(pwd) && \
 
159
  echo 'User ID:' $(id) && \
160
  echo '' && \
161
  echo '=== FILE SYSTEM CHECK ===' && \
 
162
  ls -la && \
163
  echo '' && \
164
  echo '=== PYTHON ENVIRONMENT ===' && \
 
173
  echo 'Testing Python imports...' && \
174
  python3 -B -c 'import streamlit; print(\"βœ… Streamlit:\", streamlit.__version__)' && \
175
  python3 -B -c 'import torch; print(\"βœ… Torch:\", torch.__version__)' && \
176
+ python3 -B -c 'import cv2; import mediapipe as mp; print(\"βœ… cv2 & mediapipe OK\")' && \
177
  python3 -B -c 'import sys; sys.path.insert(0, \".\"); import app; print(\"βœ… app.py imports successfully\")' && \
178
  echo 'βœ… All checks passed!'; \
179
  else \
 
183
  echo '' && \
184
  echo '=== STARTING STREAMLIT SERVER ===' && \
185
  streamlit run --server.port=8501 --server.address=0.0.0.0 app.py \
186
+ "]
VideoBackgroundReplacer2/requirements.txt CHANGED
@@ -1,11 +1,13 @@
1
- # Update requirements.txt with correct versions
2
- cat > requirements.txt << 'EOL'
3
- # Core Dependencies
 
 
4
  numpy>=1.24.0,<2.0.0
5
  Pillow>=10.0.0,<11.0.0
6
  protobuf>=4.25.0,<5.0.0
7
 
8
- # Image/Video Processing
9
  opencv-python-headless>=4.8.0,<5.0.0
10
  imageio>=2.25.0,<3.0.0
11
  imageio-ffmpeg>=0.4.7,<1.0.0
@@ -13,10 +15,10 @@ moviepy>=1.0.3,<2.0.0
13
  decord>=0.6.0,<0.7.0
14
  scikit-image>=0.19.3,<0.22.0
15
 
16
- # MediaPipe
17
  mediapipe>=0.10.0,<0.11.0
18
 
19
- # SAM2 Dependencies
20
  hydra-core>=1.3.2,<2.0.0
21
  omegaconf>=2.3.0,<3.0.0
22
  einops>=0.6.0,<0.9.0
@@ -25,28 +27,28 @@ pyyaml>=6.0.0,<7.0.0
25
  matplotlib>=3.5.0,<4.0.0
26
  iopath>=0.1.10,<0.2.0
27
 
28
- # MatAnyone Dependencies
29
- kornia>=0.7.0,<0.8.0
30
  tqdm>=4.60.0,<5.0.0
31
 
32
- # Streamlit UI
33
  streamlit>=1.32.0,<2.0.0
34
  streamlit-webrtc>=0.50.0,<1.0.0
35
 
36
- # Web stack
37
  fastapi>=0.104.0,<1.0.0
38
  uvicorn>=0.24.0,<1.0.0
39
  httpx>=0.25.0,<1.0.0
40
  anyio>=4.0.0,<5.0.0
41
  orjson>=3.10.0,<4.0.0
42
 
43
- # Pydantic (with fixed versions for Python 3.10)
44
  pydantic>=2.6.0,<2.9.0
45
  pydantic-core>=2.20.0,<2.22.0
46
  annotated-types>=0.6.0,<0.8.0
47
  typing-extensions>=4.0.0,<5.0.0
48
 
49
- # Helpers and Utilities
50
  huggingface-hub>=0.20.0,<1.0.0
51
  ffmpeg-python>=0.2.0,<1.0.0
52
  psutil>=5.8.0,<7.0.0
@@ -54,6 +56,3 @@ requests>=2.25.0,<3.0.0
54
  scikit-learn>=1.3.0,<2.0.0
55
  loguru>=0.6.0,<1.0.0
56
  python-multipart>=0.0.5,<1.0.0
57
- EOL
58
-
59
- echo "requirements.txt has been updated with fixed dependency versions."
 
1
+ # ===============================
2
+ # Requirements for HF Space (Py3.10 + Torch 2.5.1 cu121)
3
+ # ===============================
4
+
5
+ # --- Core Dependencies ---
6
  numpy>=1.24.0,<2.0.0
7
  Pillow>=10.0.0,<11.0.0
8
  protobuf>=4.25.0,<5.0.0
9
 
10
+ # --- Image / Video Processing ---
11
  opencv-python-headless>=4.8.0,<5.0.0
12
  imageio>=2.25.0,<3.0.0
13
  imageio-ffmpeg>=0.4.7,<1.0.0
 
15
  decord>=0.6.0,<0.7.0
16
  scikit-image>=0.19.3,<0.22.0
17
 
18
+ # --- MediaPipe ---
19
  mediapipe>=0.10.0,<0.11.0
20
 
21
+ # --- SAM2 Dependencies ---
22
  hydra-core>=1.3.2,<2.0.0
23
  omegaconf>=2.3.0,<3.0.0
24
  einops>=0.6.0,<0.9.0
 
27
  matplotlib>=3.5.0,<4.0.0
28
  iopath>=0.1.10,<0.2.0
29
 
30
+ # --- MatAnyone Dependencies ---
31
+ kornia>=0.7.2,<0.8.0 # 0.7.2 is stable with Torch 2.5.x
32
  tqdm>=4.60.0,<5.0.0
33
 
34
+ # --- Streamlit UI ---
35
  streamlit>=1.32.0,<2.0.0
36
  streamlit-webrtc>=0.50.0,<1.0.0
37
 
38
+ # --- Web stack ---
39
  fastapi>=0.104.0,<1.0.0
40
  uvicorn>=0.24.0,<1.0.0
41
  httpx>=0.25.0,<1.0.0
42
  anyio>=4.0.0,<5.0.0
43
  orjson>=3.10.0,<4.0.0
44
 
45
+ # --- Pydantic (Py3.10 safe pins) ---
46
  pydantic>=2.6.0,<2.9.0
47
  pydantic-core>=2.20.0,<2.22.0
48
  annotated-types>=0.6.0,<0.8.0
49
  typing-extensions>=4.0.0,<5.0.0
50
 
51
+ # --- Helpers / Utilities ---
52
  huggingface-hub>=0.20.0,<1.0.0
53
  ffmpeg-python>=0.2.0,<1.0.0
54
  psutil>=5.8.0,<7.0.0
 
56
  scikit-learn>=1.3.0,<2.0.0
57
  loguru>=0.6.0,<1.0.0
58
  python-multipart>=0.0.5,<1.0.0