guoyu/Test/python/服务器部署说明.md
2025-12-11 23:28:07 +08:00

4.5 KiB
Raw Blame History

Whisper 语音识别服务 - 服务器部署指南

一、环境要求

  • Python 3.8+
  • 2GB+ 内存
  • 2GB+ 硬盘空间(用于 Whisper 模型)
  • ffmpeg

二、安装步骤(内网服务器)

1 安装 ffmpeg

CentOS/RHEL 服务器

sudo yum install -y epel-release
sudo yum install -y ffmpeg

Ubuntu/Debian 服务器

sudo apt update
sudo apt install -y ffmpeg

Windows 服务器

# 下载 ffmpeg
# https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip
# 解压到 C:\ffmpeg
# 添加环境变量C:\ffmpeg\bin

验证安装:

ffmpeg -version

2 安装 Python 依赖(使用清华镜像)

方式一:自动安装(推荐)

Linux 服务器:

chmod +x install_whisper.sh
./install_whisper.sh

Windows 服务器:

install_whisper.bat

方式二:手动安装

# 配置镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 安装依赖
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

3 首次运行(下载模型)

python whisper_server.py

首次运行时,会自动下载 Whisper base 模型(约 140MB

下载位置:

  • Linux: ~/.cache/whisper/
  • Windows: C:\Users\用户名\.cache\whisper\

⚠️ 内网环境注意: 如果服务器无法联网下载模型,需要离线下载

# 在有网络的机器上下载模型
python -c "import whisper; whisper.load_model('base')"

# 将模型文件复制到服务器
# 模型文件base.pt约 140MB
# 位置:~/.cache/whisper/base.pt

4 启动服务

前台运行(测试用)

python whisper_server.py

后台运行(生产环境)

Linux - 使用 nohup

nohup python whisper_server.py > whisper.log 2>&1 &

Linux - 使用 systemd推荐 创建服务文件 /etc/systemd/system/whisper.service

[Unit]
Description=Whisper Speech Recognition Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/path/to/project/Test/python
ExecStart=/usr/bin/python3 whisper_server.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

启动服务:

sudo systemctl daemon-reload
sudo systemctl start whisper
sudo systemctl enable whisper  # 开机自启
sudo systemctl status whisper   # 查看状态

Windows - 使用 NSSM推荐

# 下载 NSSMhttps://nssm.cc/download
nssm install Whisper "C:\Python\python.exe" "C:\path\to\whisper_server.py"
nssm start Whisper

三、验证服务

1. 健康检查

curl http://服务器IP:5001/health

返回:

{
  "status": "ok",
  "service": "Whisper语音识别服务",
  "model": "base"
}

2. 测试识别

curl -X POST -F "file=@test.mp3" -F "language=zh" http://服务器IP:5001/recognize

四、Java 后端配置

修改 LocalWhisperService.java 中的服务地址:

private static final String WHISPER_URL = "http://服务器IP:5001";

重启 Java 应用。


五、防火墙配置

CentOS/RHEL

sudo firewall-cmd --permanent --add-port=5001/tcp
sudo firewall-cmd --reload

Ubuntu/Debian

sudo ufw allow 5001/tcp

Windows

netsh advfirewall firewall add rule name="Whisper Service" dir=in action=allow protocol=TCP localport=5001

六、性能优化建议

1. 使用更小的模型(更快)

编辑 whisper_server.py 第 48 行:

whisper_model = whisper.load_model("tiny")  # 最快,准确度降低

2. 使用更大的模型(更准确)

whisper_model = whisper.load_model("small")  # 准确度提升,速度降低

3. GPU 加速(如果有显卡)

whisper_model = whisper.load_model("base", device="cuda")

七、常见问题

Q1: 模型下载失败?

A: 使用离线下载,手动复制模型文件到 ~/.cache/whisper/

Q2: 识别速度慢?

A: 换用 tiny 模型,或使用 GPU

Q3: 内存不足?

A: 至少 2GB 内存,关闭其他服务

Q4: 无法访问服务?

A: 检查防火墙、端口、IP 地址配置


八、卸载

# 停止服务
sudo systemctl stop whisper

# 卸载 Python 包
pip uninstall openai-whisper flask flask-cors

# 删除模型文件
rm -rf ~/.cache/whisper/

联系支持

如有问题,请检查日志文件 whisper.log