261 lines
6.7 KiB
Plaintext
261 lines
6.7 KiB
Plaintext
|
|
====================================
|
|||
|
|
Windows 服务器 Whisper 服务启动指南
|
|||
|
|
====================================
|
|||
|
|
|
|||
|
|
【前置条件】
|
|||
|
|
1. Python 已安装(推荐 Python 3.8+)
|
|||
|
|
2. 已安装依赖:pip install flask openai-whisper
|
|||
|
|
|
|||
|
|
【快速启动步骤】
|
|||
|
|
|
|||
|
|
步骤1: 定位 Whisper 服务目录
|
|||
|
|
---------------------------------------
|
|||
|
|
请告诉我你的 Whisper 服务在哪个目录,例如:
|
|||
|
|
- D:\whisper-service\
|
|||
|
|
- C:\Program Files\whisper\
|
|||
|
|
- 或其他位置
|
|||
|
|
|
|||
|
|
步骤2: 启动服务
|
|||
|
|
---------------------------------------
|
|||
|
|
方式1:使用命令行
|
|||
|
|
1. 打开 PowerShell 或 CMD
|
|||
|
|
2. 进入 Whisper 服务目录
|
|||
|
|
cd D:\whisper-service
|
|||
|
|
|
|||
|
|
3. 启动服务
|
|||
|
|
python app.py
|
|||
|
|
|
|||
|
|
或指定完整路径:
|
|||
|
|
python D:\whisper-service\app.py
|
|||
|
|
|
|||
|
|
方式2:使用启动脚本
|
|||
|
|
创建 start_whisper.bat 文件:
|
|||
|
|
---------------------------------------
|
|||
|
|
@echo off
|
|||
|
|
cd /d D:\whisper-service
|
|||
|
|
echo 正在启动 Whisper 服务...
|
|||
|
|
python app.py
|
|||
|
|
pause
|
|||
|
|
---------------------------------------
|
|||
|
|
|
|||
|
|
双击运行 start_whisper.bat
|
|||
|
|
|
|||
|
|
步骤3: 确认启动成功
|
|||
|
|
---------------------------------------
|
|||
|
|
看到以下信息说明启动成功:
|
|||
|
|
✓ "Running on http://0.0.0.0:5001"
|
|||
|
|
✓ "Running on http://127.0.0.1:5001"
|
|||
|
|
✓ "Whisper 模型加载完成"
|
|||
|
|
|
|||
|
|
不要关闭这个窗口!保持服务运行。
|
|||
|
|
|
|||
|
|
步骤4: 测试连接
|
|||
|
|
---------------------------------------
|
|||
|
|
新开一个 PowerShell/CMD 窗口:
|
|||
|
|
|
|||
|
|
方式1:使用 curl(推荐)
|
|||
|
|
curl http://localhost:5001/health
|
|||
|
|
|
|||
|
|
预期返回:
|
|||
|
|
{"status":"ok"}
|
|||
|
|
|
|||
|
|
方式2:使用浏览器
|
|||
|
|
在浏览器访问:http://localhost:5001/health
|
|||
|
|
|
|||
|
|
方式3:运行测试脚本
|
|||
|
|
双击运行:Windows快速测试Whisper.bat
|
|||
|
|
|
|||
|
|
步骤5: 测试 Java 应用连接
|
|||
|
|
---------------------------------------
|
|||
|
|
1. 确保 Java 应用已启动
|
|||
|
|
2. 访问:http://localhost:8080/test/whisper/check
|
|||
|
|
3. 应该看到:
|
|||
|
|
{
|
|||
|
|
"code": 200,
|
|||
|
|
"msg": "连接成功",
|
|||
|
|
"data": {
|
|||
|
|
"available": true,
|
|||
|
|
"status": "运行中"
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
【常见问题】
|
|||
|
|
|
|||
|
|
问题1:找不到 python 命令
|
|||
|
|
---------------------------------------
|
|||
|
|
解决方案:
|
|||
|
|
1. 检查 Python 是否已安装
|
|||
|
|
python --version
|
|||
|
|
|
|||
|
|
2. 如果未安装,下载安装:
|
|||
|
|
https://www.python.org/downloads/
|
|||
|
|
|
|||
|
|
3. 安装时勾选 "Add Python to PATH"
|
|||
|
|
|
|||
|
|
问题2:提示找不到 flask 或 whisper 模块
|
|||
|
|
---------------------------------------
|
|||
|
|
安装依赖:
|
|||
|
|
pip install flask
|
|||
|
|
pip install openai-whisper
|
|||
|
|
|
|||
|
|
或一起安装:
|
|||
|
|
pip install flask openai-whisper
|
|||
|
|
|
|||
|
|
问题3:端口 5001 被占用
|
|||
|
|
---------------------------------------
|
|||
|
|
查找占用进程:
|
|||
|
|
netstat -ano | findstr :5001
|
|||
|
|
|
|||
|
|
结束进程:
|
|||
|
|
taskkill /PID <进程ID> /F
|
|||
|
|
|
|||
|
|
或修改 Whisper 服务端口(需同时修改 Java 配置)
|
|||
|
|
|
|||
|
|
问题4:Whisper 服务目录不存在
|
|||
|
|
---------------------------------------
|
|||
|
|
如果没有 Whisper 服务代码,可以创建:
|
|||
|
|
|
|||
|
|
1. 创建目录:
|
|||
|
|
mkdir D:\whisper-service
|
|||
|
|
cd D:\whisper-service
|
|||
|
|
|
|||
|
|
2. 创建 app.py 文件(参考下面的示例代码)
|
|||
|
|
|
|||
|
|
3. 安装依赖:
|
|||
|
|
pip install flask openai-whisper
|
|||
|
|
|
|||
|
|
4. 启动服务:
|
|||
|
|
python app.py
|
|||
|
|
|
|||
|
|
【Whisper 服务示例代码】
|
|||
|
|
---------------------------------------
|
|||
|
|
如果需要创建 Whisper 服务,保存以下内容为 app.py:
|
|||
|
|
|
|||
|
|
from flask import Flask, request, jsonify
|
|||
|
|
import whisper
|
|||
|
|
import os
|
|||
|
|
import tempfile
|
|||
|
|
|
|||
|
|
app = Flask(__name__)
|
|||
|
|
|
|||
|
|
# 加载模型
|
|||
|
|
print("正在加载 Whisper 模型...")
|
|||
|
|
model = whisper.load_model("base")
|
|||
|
|
print("✓ Whisper 模型加载完成")
|
|||
|
|
|
|||
|
|
@app.route('/health', methods=['GET'])
|
|||
|
|
def health():
|
|||
|
|
return jsonify({"status": "ok"})
|
|||
|
|
|
|||
|
|
@app.route('/recognize', methods=['POST'])
|
|||
|
|
def recognize():
|
|||
|
|
try:
|
|||
|
|
file = request.files['file']
|
|||
|
|
language = request.form.get('language', 'zh')
|
|||
|
|
|
|||
|
|
# 保存临时文件
|
|||
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp:
|
|||
|
|
file.save(tmp.name)
|
|||
|
|
temp_path = tmp.name
|
|||
|
|
|
|||
|
|
# 识别
|
|||
|
|
result = model.transcribe(temp_path, language=language)
|
|||
|
|
text = result['text']
|
|||
|
|
|
|||
|
|
# 删除临时文件
|
|||
|
|
os.remove(temp_path)
|
|||
|
|
|
|||
|
|
return jsonify({
|
|||
|
|
"code": 200,
|
|||
|
|
"msg": "识别成功",
|
|||
|
|
"data": {"text": text}
|
|||
|
|
})
|
|||
|
|
except Exception as e:
|
|||
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|||
|
|
|
|||
|
|
@app.route('/evaluate', methods=['POST'])
|
|||
|
|
def evaluate():
|
|||
|
|
try:
|
|||
|
|
file = request.files['file']
|
|||
|
|
standard_text = request.form.get('text', '')
|
|||
|
|
|
|||
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp:
|
|||
|
|
file.save(tmp.name)
|
|||
|
|
temp_path = tmp.name
|
|||
|
|
|
|||
|
|
result = model.transcribe(temp_path, language='zh')
|
|||
|
|
recognized_text = result['text']
|
|||
|
|
os.remove(temp_path)
|
|||
|
|
|
|||
|
|
# 简单评分
|
|||
|
|
score = calculate_similarity(recognized_text, standard_text)
|
|||
|
|
|
|||
|
|
return jsonify({
|
|||
|
|
"code": 200,
|
|||
|
|
"msg": "评测成功",
|
|||
|
|
"data": {
|
|||
|
|
"text": recognized_text,
|
|||
|
|
"score": int(score * 100),
|
|||
|
|
"accuracy": int(score * 100),
|
|||
|
|
"fluency": int(score * 100),
|
|||
|
|
"completeness": int(score * 100),
|
|||
|
|
"pronunciation": int(score * 100)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
except Exception as e:
|
|||
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|||
|
|
|
|||
|
|
def calculate_similarity(text1, text2):
|
|||
|
|
text1 = text1.replace(' ', '')
|
|||
|
|
text2 = text2.replace(' ', '')
|
|||
|
|
if len(text2) == 0:
|
|||
|
|
return 0
|
|||
|
|
matched = sum(1 for c in text1 if c in text2)
|
|||
|
|
return matched / len(text2)
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
print("🚀 Whisper 服务启动中...")
|
|||
|
|
print("📍 地址: http://0.0.0.0:5001")
|
|||
|
|
print("✓ 服务就绪")
|
|||
|
|
app.run(host='0.0.0.0', port=5001, debug=False)
|
|||
|
|
|
|||
|
|
---------------------------------------
|
|||
|
|
|
|||
|
|
【后台运行服务】
|
|||
|
|
|
|||
|
|
如果需要让 Whisper 服务在后台运行:
|
|||
|
|
|
|||
|
|
方式1:使用 start 命令
|
|||
|
|
start /b python app.py
|
|||
|
|
|
|||
|
|
方式2:创建 Windows 服务(推荐生产环境)
|
|||
|
|
使用 NSSM (Non-Sucking Service Manager):
|
|||
|
|
1. 下载 NSSM: https://nssm.cc/download
|
|||
|
|
2. 安装服务:
|
|||
|
|
nssm install WhisperService "C:\Python39\python.exe" "D:\whisper-service\app.py"
|
|||
|
|
3. 启动服务:
|
|||
|
|
nssm start WhisperService
|
|||
|
|
|
|||
|
|
方式3:使用任务计划程序
|
|||
|
|
1. 打开"任务计划程序"
|
|||
|
|
2. 创建基本任务
|
|||
|
|
3. 触发器:系统启动时
|
|||
|
|
4. 操作:启动程序 python.exe,参数:D:\whisper-service\app.py
|
|||
|
|
|
|||
|
|
【日志查看】
|
|||
|
|
|
|||
|
|
如果服务启动失败,查看错误信息:
|
|||
|
|
1. 保持命令行窗口打开,查看输出
|
|||
|
|
2. 或将输出重定向到文件:
|
|||
|
|
python app.py > whisper.log 2>&1
|
|||
|
|
|
|||
|
|
【获取帮助】
|
|||
|
|
|
|||
|
|
如果遇到问题,请提供:
|
|||
|
|
1. Whisper 服务目录路径
|
|||
|
|
2. 启动时的完整错误信息
|
|||
|
|
3. Python 版本:python --version
|
|||
|
|
4. 端口检查结果:netstat -ano | findstr :5001
|
|||
|
|
|
|||
|
|
====================================
|