feat: Docker 构建预置 ASR 模型,支持离线使用
- Dockerfile 构建时预下载 faster-whisper 模型到镜像 - docker-compose 增加 ASR_MODEL_CACHE_DIR 环境变量 - asr_service 支持从缓存目录加载本地模型,无需运行时联网下载 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -28,6 +28,11 @@ RUN pip install --no-cache-dir --upgrade pip && \
|
||||
# 复制应用代码(先复制代码,避免覆盖环境变量文件)
|
||||
COPY . .
|
||||
|
||||
# 构建时预下载 faster-whisper 模型到镜像,云端运行时直接使用本地模型无需再下载
|
||||
ARG ASR_MODEL_SIZE=small
|
||||
RUN mkdir -p /app/models/whisper && \
|
||||
python -c "from faster_whisper import WhisperModel; WhisperModel('${ASR_MODEL_SIZE}', device='cpu', compute_type='int8', download_root='/app/models/whisper')"
|
||||
|
||||
# 复制生产环境配置文件到镜像中
|
||||
# 将 .env.production 复制为 .env,供应用运行时使用
|
||||
# 注意:构建前请确保 .env.production 文件存在
|
||||
|
||||
@@ -64,6 +64,7 @@ services:
|
||||
environment:
|
||||
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/life_echo
|
||||
- REDIS_URL=redis://redis:6379/0
|
||||
- ASR_MODEL_CACHE_DIR=/app/models/whisper
|
||||
restart: always
|
||||
depends_on:
|
||||
postgres:
|
||||
|
||||
@@ -16,6 +16,8 @@ logger = logging.getLogger(__name__)
|
||||
ASR_MODEL_SIZE = os.getenv("ASR_MODEL_SIZE", "small")
|
||||
ASR_DEVICE = os.getenv("ASR_DEVICE", "auto") # auto, cpu, cuda
|
||||
ASR_COMPUTE_TYPE = os.getenv("ASR_COMPUTE_TYPE", "auto") # auto, int8, float16, float32
|
||||
# 镜像内预置模型目录,设置后直接使用本地模型不联网下载(与 Dockerfile 中 download_root 一致)
|
||||
ASR_MODEL_CACHE_DIR = os.getenv("ASR_MODEL_CACHE_DIR")
|
||||
|
||||
|
||||
class ASRService:
|
||||
@@ -58,10 +60,14 @@ class ASRService:
|
||||
else:
|
||||
compute_type = "int8" # CPU 使用 int8 量化,速度更快
|
||||
|
||||
download_root = ASR_MODEL_CACHE_DIR if ASR_MODEL_CACHE_DIR else None
|
||||
local_files_only = bool(ASR_MODEL_CACHE_DIR)
|
||||
self.model = WhisperModel(
|
||||
ASR_MODEL_SIZE,
|
||||
device=device,
|
||||
compute_type=compute_type
|
||||
compute_type=compute_type,
|
||||
download_root=download_root,
|
||||
local_files_only=local_files_only,
|
||||
)
|
||||
|
||||
self._model_loaded = True
|
||||
|
||||
Reference in New Issue
Block a user