docker 下部署 纯CPU的Nway ASR & TTS
首先先下载包
需要包私我
将下载的包解压,进入目录创建下面三个相关文件
1、准备下面三个文件
准备Dockerfile
FROM ubuntu:22.04
# 设置非交互式安装
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
ca-certificates \
libgcc-s1 \
python3 \
python3-pip \
python3-venv \
net-tools \
&& rm -rf /var/lib/apt/lists/*
# 设置工作目录
WORKDIR /app
# 复制应用程序文件
COPY . .
# 安装Python依赖(添加requests)
RUN pip3 install flask werkzeug requests
# 设置权限
RUN chmod +x nasr-server nasr-client ntts-server ntts-client start_services.sh
# 设置环境变量
ENV LD_LIBRARY_PATH=/app
ENV PYTHONPATH=/app
EXPOSE 8089 8090 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD netstat -tuln | grep -q -E ':(8089|8090|8080)' || exit 1
CMD ["./start_services.sh"]
2、启动脚本
#!/bin/bash
echo "=== 启动脚本开始 ==="
echo "当前目录: $(pwd)"
echo "文件列表:"
ls -la
echo "=== 检查可执行文件 ==="
file nasr-server ntts-server
echo "=== 设置环境变量 ==="
export LD_LIBRARY_PATH=/app
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
echo "=== 启动ASR服务 ==="
./nasr-server &
ASR_PID=$!
echo "ASR服务PID: $ASR_PID"
echo "=== 启动TTS服务 ==="
./ntts-server &
TTS_PID=$!
echo "TTS服务PID: $TTS_PID"
echo "=== 等待服务启动 ==="
sleep 5
echo "=== 检查端口监听 ==="
netstat -tuln | grep -E ':(8089|8090)'
echo "=== 启动HTTP网关 ==="
python3 http_gateway.py &
GATEWAY_PID=$!
echo "HTTP网关PID: $GATEWAY_PID"
echo "=== 最终端口检查 ==="
sleep 3
netstat -tuln | grep -E ':(8089|8090|8080)'
echo "=== 所有服务已启动 ==="
# 等待服务运行
wait
3、python 应用
from flask import Flask, request, jsonify, send_file
import requests
import os
import tempfile
import uuid
from werkzeug.utils import secure_filename
app = Flask(__name__)
# 服务配置
ASR_SERVER = "http://localhost:8089"
TTS_SERVER = "http://localhost:8090"
@app.route('/api/health', methods=['GET'])
def health_check():
"""健康检查接口"""
return jsonify({
"status": "healthy",
"services": {
"asr": "running",
"tts": "running"
}
})
@app.route('/api/asr/transcribe', methods=['POST'])
def transcribe_audio():
"""语音识别接口"""
if 'audio' not in request.files:
return jsonify({
"error": "No audio file provided"}), 400
audio_file = request.files['audio']
# 保存临时文件
temp_dir = tempfile.gettempdir()
filename = secure_filename(f"{uuid.uuid4()}.wav")
filepath = os.path.join(temp_dir, filename)
audio_file.save(filepath)
try:
# 调用ASR服务
result = os.popen(f"./nasr-client 127.0.0.1:8089 {filepath}").read().strip()
return jsonify({
"text": result,
"status": "success"
})
except Exception as e:
return jsonify({
"error": str(e)}), 500
finally:
# 清理临时文件
if os.path.exists(filepath):
os.remove(filepath)
@app.route('/api/tts/synthesize', methods=['POST'])
def synthesize_speech():
"""语音合成接口"""
data = request.get_json()
if not data or 'text' not in data:
return jsonify({
"error": "No text provided"}), 400
text = data['text']
output_filename = data.get('filename', f"{uuid.uuid4()}.wav")
try:
# 调用TTS服务
output_path = f"/tmp/{output_filename}"
command = f'./ntts-client 127.0.0.1:8090 --input "{text}" --output {output_path}'
os.system(command)
if os.path.exists(output_path):
return send_file(output_path, as_attachment=True, download_name=output_filename)
else:
return jsonify({
"error": "TTS synthesis failed"}), 500
except Exception as e:
return jsonify({
"error": str(e)}), 500
@app.route('/api/tts/synthesize_base64', methods=['POST'])
def synthesize_speech_base64():
"""语音合成接口(返回base64)"""
import base64
data = request.get_json()
if not data or 'text' not in data:
return jsonify({
"error": "No text provided"}), 400
text = data['text']
try:
# 调用TTS服务
temp_file = f"/tmp/{uuid.uuid4()}.wav"
command = f'./ntts-client 127.0.0.1:8090 --input "{text}" --output {temp_file}'
os.system(command)
if os.path.exists(temp_file):
with open(temp_file, 'rb') as f:
audio_data = f.read()
os.remove(temp_file)
return jsonify({
"audio_base64": base64.b64encode(audio_data).decode('utf-8'),
"status": "success"
})
else:
return jsonify({
"error": "TTS synthesis failed"}), 500
except Exception as e:
return jsonify({
"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)
4、构建并运行容器
# 构建镜像
docker build -t nway-audio-ai .
# 运行新容器
docker run -d \
-p 8080:8080 \
-p 8089:8089 \
-p 8090:8090 \
--name nway-audio-ai \
nway-audio-ai
# 查看日志
docker logs -f nway-audio-ai
最后测试:
# 测试HTTP网关
curl http://localhost:8080/api/health
# 测试ASR服务
curl -X POST http://localhost:8080/api/asr/transcribe \
-F "audio=@test.wav"
# 测试TTS服务
curl -X POST http://localhost:8080/api/tts/synthesize \
-H "Content-Type: application/json" \
-d '{"text": "测试语音合成"}'
docker 下部署 纯CPU的Nway ASR & TTS 到此结束!