Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

feat: add GPU support via Dockerfile.gpu and docker-compose.gpu.yml

SUMDDOS39d5894992ce
diff --git a/Dockerfile b/Dockerfile
index 3f48af37..4216326a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,33 +1,48 @@
-# Use NVIDIA CUDA runtime as parent image (includes CUDA 12.1 + cuDNN 8)
-FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
-
-# Avoid interactive timezone prompt
-ENV DEBIAN_FRONTEND=noninteractive
+# Use an official Python runtime as a parent image
+FROM python:3.11-slim-bullseye
# Set the working directory in the container
WORKDIR /MoneyPrinterTurbo
+
+# 设置/MoneyPrinterTurbo目录权限为777
RUN chmod 777 /MoneyPrinterTurbo
ENV PYTHONPATH="/MoneyPrinterTurbo"
-# Install Python 3.11 and system dependencies
-RUN apt-get update && apt-get install -y --no-install-recommends \
- software-properties-common \
- git \
- imagemagick \
- ffmpeg \
- curl \
- && add-apt-repository ppa:deadsnakes/ppa \
- && apt-get update && apt-get install -y --no-install-recommends \
- python3.11 \
- python3.11-venv \
- python3.11-dev \
- python3-pip \
- && ln -sf /usr/bin/python3.11 /usr/bin/python3 \
- && ln -sf /usr/bin/python3.11 /usr/bin/python \
- && python3.11 -m pip install --upgrade pip \
- && apt-get remove -y python3-blinker || true \
- && rm -rf /var/lib/apt/lists/*
+# Install system dependencies with domestic mirrors first for stability
+RUN echo "deb http://mirrors.aliyun.com/debian bullseye main" > /etc/apt/sources.list && \
+ echo "deb http://mirrors.aliyun.com/debian-security bullseye-security main" >> /etc/apt/sources.list && \
+ ( \
+ for i in 1 2 3; do \
+ echo "Attempt $i: Using Aliyun mirror"; \
+ apt-get update && apt-get install -y --no-install-recommends \
+ git \
+ imagemagick \
+ ffmpeg && break || \
+ echo "Attempt $i failed, retrying..."; \
+ if [ $i -eq 3 ]; then \
+ echo "Aliyun mirror failed, switching to Tsinghua mirror"; \
+ sed -i 's/mirrors.aliyun.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
+ sed -i 's/mirrors.aliyun.com\/debian-security/mirrors.tuna.tsinghua.edu.cn\/debian-security/g' /etc/apt/sources.list && \
+ ( \
+ apt-get update && apt-get install -y --no-install-recommends \
+ git \
+ imagemagick \
+ ffmpeg || \
+ ( \
+ echo "Tsinghua mirror failed, switching to default Debian mirror"; \
+ sed -i 's/mirrors.tuna.tsinghua.edu.cn/deb.debian.org/g' /etc/apt/sources.list && \
+ sed -i 's/mirrors.tuna.tsinghua.edu.cn\/debian-security/security.debian.org/g' /etc/apt/sources.list; \
+ apt-get update && apt-get install -y --no-install-recommends \
+ git \
+ imagemagick \
+ ffmpeg; \
+ ); \
+ ); \
+ fi; \
+ sleep 5; \
+ done \
+ ) && rm -rf /var/lib/apt/lists/*
# Fix security policy for ImageMagick
RUN sed -i '/<policy domain="path" rights="none" pattern="@\*"/d' /etc/ImageMagick-6/policy.xml
@@ -35,15 +50,10 @@ RUN sed -i '/<policy domain="path" rights="none" pattern="@\*"/d' /etc/ImageMagi
# Copy only the requirements.txt first to leverage Docker cache
COPY requirements.txt ./
-# Install Python dependencies
-RUN python3 -m pip install --no-cache-dir \
- -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com \
- --retries 3 --timeout 60 -r requirements.txt || \
- python3 -m pip install --no-cache-dir \
- -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ --trusted-host mirrors.tuna.tsinghua.edu.cn \
- --retries 3 --timeout 60 -r requirements.txt || \
- python3 -m pip install --no-cache-dir \
- --retries 3 --timeout 60 -r requirements.txt
+# Install Python dependencies with domestic mirrors first and retry logic
+RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --retries 3 --timeout 60 -r requirements.txt || \
+ pip install --no-cache-dir -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ --trusted-host mirrors.tuna.tsinghua.edu.cn --retries 3 --timeout 60 -r requirements.txt || \
+ pip install --no-cache-dir --retries 3 --timeout 60 -r requirements.txt
# Now copy the rest of the codebase into the image
COPY . .
@@ -53,3 +63,12 @@ EXPOSE 8501
# Command to run the application
CMD ["streamlit", "run", "./webui/Main.py","--browser.serverAddress=127.0.0.1","--server.enableCORS=True","--browser.gatherUsageStats=False"]
+
+# 1. Build the Docker image using the following command
+# docker build -t moneyprinterturbo .
+
+# 2. Run the Docker container using the following command
+## For Linux or MacOS:
+# docker run -v $(pwd)/config.toml:/MoneyPrinterTurbo/config.toml -v $(pwd)/storage:/MoneyPrinterTurbo/storage -p 8501:8501 moneyprinterturbo
+## For Windows:
+# docker run -v ${PWD}/config.toml:/MoneyPrinterTurbo/config.toml -v ${PWD}/storage:/MoneyPrinterTurbo/storage -p 8501:8501 moneyprinterturbo
\ No newline at end of file
diff --git a/Dockerfile.gpu b/Dockerfile.gpu
new file mode 100644
index 00000000..3f48af37
--- /dev/null
+++ b/Dockerfile.gpu
@@ -0,0 +1,55 @@
+# Use NVIDIA CUDA runtime as parent image (includes CUDA 12.1 + cuDNN 8)
+FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
+
+# Avoid interactive timezone prompt
+ENV DEBIAN_FRONTEND=noninteractive
+
+# Set the working directory in the container
+WORKDIR /MoneyPrinterTurbo
+RUN chmod 777 /MoneyPrinterTurbo
+
+ENV PYTHONPATH="/MoneyPrinterTurbo"
+
+# Install Python 3.11 and system dependencies
+RUN apt-get update && apt-get install -y --no-install-recommends \
+ software-properties-common \
+ git \
+ imagemagick \
+ ffmpeg \
+ curl \
+ && add-apt-repository ppa:deadsnakes/ppa \
+ && apt-get update && apt-get install -y --no-install-recommends \
+ python3.11 \
+ python3.11-venv \
+ python3.11-dev \
+ python3-pip \
+ && ln -sf /usr/bin/python3.11 /usr/bin/python3 \
+ && ln -sf /usr/bin/python3.11 /usr/bin/python \
+ && python3.11 -m pip install --upgrade pip \
+ && apt-get remove -y python3-blinker || true \
+ && rm -rf /var/lib/apt/lists/*
+
+# Fix security policy for ImageMagick
+RUN sed -i '/<policy domain="path" rights="none" pattern="@\*"/d' /etc/ImageMagick-6/policy.xml
+
+# Copy only the requirements.txt first to leverage Docker cache
+COPY requirements.txt ./
+
+# Install Python dependencies
+RUN python3 -m pip install --no-cache-dir \
+ -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com \
+ --retries 3 --timeout 60 -r requirements.txt || \
+ python3 -m pip install --no-cache-dir \
+ -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ --trusted-host mirrors.tuna.tsinghua.edu.cn \
+ --retries 3 --timeout 60 -r requirements.txt || \
+ python3 -m pip install --no-cache-dir \
+ --retries 3 --timeout 60 -r requirements.txt
+
+# Now copy the rest of the codebase into the image
+COPY . .
+
+# Expose the port the app runs on
+EXPOSE 8501
+
+# Command to run the application
+CMD ["streamlit", "run", "./webui/Main.py","--browser.serverAddress=127.0.0.1","--server.enableCORS=True","--browser.gatherUsageStats=False"]
diff --git a/docker-compose.gpu.yml b/docker-compose.gpu.yml
new file mode 100644
index 00000000..a15ea01a
--- /dev/null
+++ b/docker-compose.gpu.yml
@@ -0,0 +1,27 @@
+# GPU override file
+# Usage: docker compose -f docker-compose.yml -f docker-compose.gpu.yml up -d
+#
+# Prerequisites:
+# 1. NVIDIA GPU driver installed on host
+# 2. NVIDIA Container Toolkit installed
+# 3. Verify with: docker info | grep nvidia
+#
+# This file overrides the default docker-compose.yml to:
+# - Build with Dockerfile.gpu (NVIDIA CUDA base image)
+# - Attach GPU device to the api service
+services:
+ webui:
+ build:
+ context: .
+ dockerfile: Dockerfile.gpu
+ api:
+ build:
+ context: .
+ dockerfile: Dockerfile.gpu
+ deploy:
+ resources:
+ reservations:
+ devices:
+ - driver: nvidia
+ count: 1
+ capabilities: [gpu]
diff --git a/docker-compose.yml b/docker-compose.yml
index 73793f4a..acad4626 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,6 +1,6 @@
x-common-volumes: &common-volumes
- ./:/MoneyPrinterTurbo
- - ./storage:/MoneyPrinterTurbo/storage
+
services:
webui:
build:
@@ -21,11 +21,4 @@ services:
- "8080:8080"
command: [ "python3", "main.py" ]
volumes: *common-volumes
- restart: always
- deploy:
- resources:
- reservations:
- devices:
- - driver: nvidia
- count: 1
- capabilities: [gpu]
+ restart: always
\ No newline at end of file
diff --git a/docs/GPU_DOCKER_DEPLOYMENT.md b/docs/GPU_DOCKER_DEPLOYMENT.md
index e7aa0a55..506e2038 100644
--- a/docs/GPU_DOCKER_DEPLOYMENT.md
+++ b/docs/GPU_DOCKER_DEPLOYMENT.md
@@ -1,103 +1,84 @@
# GPU Docker Deployment Guide
-本文档介绍如何将 MoneyPrinterTurbo 的 Docker 部署改造为 GPU 加速模式,使 `faster-whisper` 在 GPU 上运行,大幅提升字幕生成速度。
+本文档介绍如何使用 GPU 加速 `faster-whisper` 字幕生成,大幅提升处理速度。
## 为什么要 GPU 加速
-MoneyPrinterTurbo 中唯一的深度学习环节是 **faster-whisper 语音识别**(将生成的音频转为带时间戳的字幕)。
+MoneyPrinterTurbo 中唯一的深度学习环节是 **faster-whisper 语音识别**(将音频转为带时间戳的字幕)。
-- **CPU 模式**(默认):`large-v3` 模型生成字幕较慢,受限于 CPU 算力
-- **GPU 模式**:利用 NVIDIA GPU + CUDA 加速,字幕生成速度提升 **5-10 倍**
+- **CPU 模式**(默认):`large-v3` 模型生成字幕较慢
+- **GPU 模式**:利用 NVIDIA GPU + CUDA 加速,速度提升 **5-10 倍**
> 注意:项目的其他环节(脚本生成、音频合成、视频剪辑)不涉及深度学习,GPU 只加速字幕生成。
-## 前提条件
+## 部署方式
-### 1. 硬件要求
+本项目提供两种 Docker 部署方式,**默认 CPU 部署不受任何影响**:
-- NVIDIA GPU(建议 6GB 以上显存)
-- `large-v3` 模型在 GPU 上 `float16` 精度约占用 1.5GB 显存
+### CPU 部署(默认,零变化)
-### 2. 软件要求
+```bash
+docker compose up -d
+```
-- **NVIDIA 驱动**:最新版即可(可在 PowerShell 中运行 `nvidia-smi` 确认)
-- **Docker Desktop**:确保已安装
-- **NVIDIA Container Toolkit**:Docker 中需要 `nvidia` runtime(运行 `docker info` 查看 Runtimes 列表中是否有 `nvidia`)
+使用原有的 `Dockerfile`(`python:3.11-slim-bullseye`),无需 GPU。
-### 3. 环境验证
+### GPU 部署(有 NVIDIA GPU 的用户)
```bash
-# 确认 NVIDIA 驱动正常
-nvidia-smi
-
-# 确认 Docker 支持 GPU
-docker info | findstr nvidia
+docker compose -f docker-compose.yml -f docker-compose.gpu.yml up -d
```
-如果 `docker info` 中没有看到 `nvidia` runtime,需要先安装 [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)。
+使用 `Dockerfile.gpu`(`nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04`)并为 api 服务挂载 GPU。
-## 修改内容
+## GPU 部署前提条件
-本次改造涉及 **3 个文件**:
+### 1. 硬件要求
-### 1. Dockerfile
+- NVIDIA GPU(建议 6GB 以上显存)
+- `large-v3` 模型在 GPU 上 `float16` 精度约占用 1.5GB 显存
-将基础镜像从纯 CPU 的 Python 镜像替换为带 CUDA 运行时的 NVIDIA 镜像,并安装 Python 3.11。
+### 2. 软件要求
-**核心变更:**
+- **NVIDIA 驱动**:最新版即可,运行 `nvidia-smi` 确认
+- **Docker Desktop**
+- **NVIDIA Container Toolkit**:运行 `docker info` 查看 Runtimes 列表中是否有 `nvidia`
-```dockerfile
-# 原来:纯 CPU 镜像
-FROM python:3.11-slim-bullseye
+### 3. 环境验证
-# 改为:带 CUDA 12.1 + cuDNN 8 的运行时镜像
-FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
-```
+```bash
+# 确认 NVIDIA 驱动正常
+nvidia-smi
-主要改动点:
-- 基础镜像切换为 `nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04`
-- 新增安装 Python 3.11(CUDA 镜像基于 Ubuntu 22.04,自带 Python 3.10 但项目需要 3.11)
-- 通过 `add-apt-repository ppa:deadsnakes/ppa` 安装 Python 3.11
-- 移除系统自带的 `python3-blinker` 包(避免 pip 安装冲突)
-- 使用 `python3 -m pip` 代替 `pip` 命令
-
-### 2. docker-compose.yml
-
-为 `api` 服务挂载 GPU 设备。
-
-**核心变更:**
-
-```yaml
-services:
- api:
- # ... 其他配置不变 ...
- deploy:
- resources:
- reservations:
- devices:
- - driver: nvidia
- count: 1
- capabilities: [gpu]
+# 确认 Docker 支持 GPU(Runtimes 中应包含 nvidia)
+docker info | findstr nvidia
```
-> `webui` 服务是纯前端(Streamlit),不需要 GPU,保持不变。
+如果没有 `nvidia` runtime,需要先安装 [NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)。
-### 3. config.toml
+## 配置 Whisper 使用 GPU
-配置 Whisper 使用 GPU 模式。
-
-**核心变更:**
+在 `config.toml` 中设置:
```toml
-subtitle_provider = "whisper" # 启用 whisper(原来可能是 edge)
+subtitle_provider = "whisper"
[whisper]
model_size = "large-v3"
-device = "cuda" # 原来是 "cpu"
-compute_type = "float16" # 原来是 "int8",GPU 上 float16 更快更准
+device = "cuda" # 使用 GPU(CPU 用户设为 "cpu")
+compute_type = "float16" # GPU 推荐 float16(CPU 用户设为 "int8")
```
-## 部署步骤
+## 文件说明
+
+| 文件 | 用途 |
+|---|---|
+| `Dockerfile` | 默认 CPU 镜像(原有,未修改) |
+| `Dockerfile.gpu` | GPU 镜像(新增,基于 NVIDIA CUDA) |
+| `docker-compose.yml` | 默认 CPU 部署配置(原有,未修改) |
+| `docker-compose.gpu.yml` | GPU 部署覆盖配置(新增) |
+
+## GPU 部署步骤
### 第 1 步:拉取 CUDA 基础镜像
@@ -105,27 +86,19 @@ compute_type = "float16" # 原来是 "int8",GPU 上 float16 更快更
docker pull nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04
```
-> 如果使用了阿里云等镜像加速源,可能对 `nvidia/cuda` 系列镜像返回 403。请确保能从 Docker Hub 直接拉取,或配置支持 NVIDIA 镜像的加速源。
+> 如果使用了阿里云等镜像加速源,可能对 `nvidia/cuda` 返回 403。请确保能从 Docker Hub 直接拉取。
-### 第 2 步:修改配置文件
+### 第 2 步:修改 config.toml
-按上文说明修改 `Dockerfile`、`docker-compose.yml` 和 `config.toml` 三个文件。
+按上文说明设置 `subtitle_provider = "whisper"` 和 `device = "cuda"`。
-### 第 3 步:构建镜像
+### 第 3 步:构建并启动
```bash
-docker compose build --no-cache
+docker compose -f docker-compose.yml -f docker-compose.gpu.yml up -d --build
```
-### 第 4 步:启动服务
-
-```bash
-docker compose up -d
-```
-
-### 第 5 步:验证 GPU 是否生效
-
-进入 api 容器,确认 GPU 可见:
+### 第 4 步:验证 GPU 是否生效
```bash
docker exec -it moneyprinterturbo-api nvidia-smi
@@ -150,27 +123,20 @@ docker exec -it moneyprinterturbo-api nvidia-smi
阿里云镜像加速对 `nvidia/cuda` 返回 403。解决方法:
- 配置其他可用的镜像加速源
-- 或直接 `docker pull nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04` 从 Docker Hub 拉取
-
-### 问题 2:pip 安装报 `no such option: --break-system-packages`
-
-旧版 pip 不支持此参数。解决方法:
-- 使用 `python3 -m pip install` 代替 `pip install`
-- 或在安装 Python 后先升级 pip:`python3.11 -m pip install --upgrade pip`
+- 或直接 `docker pull nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04`
-### 问题 3:pip 安装报 `Cannot uninstall blinker`
+### 问题 2:pip 安装报 `Cannot uninstall blinker`
-Ubuntu 22.04 系统自带的 `blinker` 包通过 `distutils` 安装,pip 无法卸载。解决方法:
-- 在安装 Python 依赖前先 `apt-get remove -y python3-blinker`
+Ubuntu 22.04 系统自带的 `blinker` 通过 `distutils` 安装,pip 无法卸载。`Dockerfile.gpu` 已通过 `apt-get remove -y python3-blinker` 处理。
-### 问题 4:容器内 `nvidia-smi` 找不到 GPU
+### 问题 3:容器内 `nvidia-smi` 找不到 GPU
- 确认宿主机已安装 NVIDIA Container Toolkit
- 确认 `docker info` 中 Runtimes 包含 `nvidia`
-- 确认 `docker-compose.yml` 中已添加 `deploy.resources.reservations.devices` 配置
+- 确认使用了 GPU 部署命令:`docker compose -f docker-compose.yml -f docker-compose.gpu.yml up -d`
-### 问题 5:Whisper 报 CUDA 错误
+### 问题 4:Whisper 报 CUDA 错误
-- 确认 `config.toml` 中 `device = "cuda"`(注意不是 `"CPU"`,大小写敏感)
-- 确认 `compute_type = "float16"`(GPU 推荐值)
-- 确认 `subtitle_provider = "whisper"`(不是 `"edge"`)
+- 确认 `config.toml` 中 `device = "cuda"`(大小写敏感,不是 `"CPU"`)
+- 确认 `compute_type = "float16"`
+- 确认 `subtitle_provider = "whisper"`