自己搭建[文本转语音]服务器

一、简介

准备用基于esp8266的nodemcu开发板做一个天气时钟。

一步一步记录代码编写过程。

竹壳天气时钟

Bamboo shell weather clock

使用基于esp8266的NodeMCU制作。

计划用竹子做最后成品的外壳,所以才有了这个名称。

上一篇文章已经完成了时钟和天气的显示,下一步要做语音控制报时和报天气,今天记录一下我使用 三丰云 免费云服务器 搭建TTS的过程。三丰云 https://www.sanfengyun.com 我已经用了一段时间,感觉还是很不错的,运行也很稳定。

二、TTS服务器搭建

在云服务器上安装Ubuntu Linux,安装docker-ce docker-ce-cli

然后添加docker代理,复制后粘贴到shell终端即可

bash 复制代码
sudo tee /etc/docker/daemon.json <<-"EOF"
{
    "registry-mirrors": ["https://dockerpull.com"]
}
EOF

然后建立一个tts文件夹并进入,以下代码复制后粘贴到shell终端即可

bash 复制代码
tee ~/tts/gunicorn.conf <<-'EOF'
# gunicorn.conf
# 并行工作进程数
workers = 4
# 指定每个工作者的线程数
threads = 2
# 监听内网端口5000
bind='0.0.0.0:2020'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式协程
worker_class = 'gevent'
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = 'gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = 'gunicorn_acess.log'
errorlog = 'gunicorn_error.log'
# 设置日志记录水平
loglevel = 'debug'
EOF
tee ~/tts/Dockerfile <<-'EOF'
FROM python:3.8.4
COPY requirements.txt ./
RUN pip install  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . /flask_project/
WORKDIR /flask_project/
EOF
tee ~/tts/dockermake.sh <<-'EOF'
docker build -t python_tts .
EOF
tee ~/tts/requirements.txt <<-'EOF'
Flask
flask_cors
cos-python-sdk-v5
gunicorn
gevent
edge-tts
EOF
tee ~/tts/edge-tts.py <<-'EOF'
'''
原项目作者:https://github.com/lyz1810/edge-tts
'''
import logging
import os
import re
import sys
import uuid
from flask import Flask, request, send_file
from qcloud_cos import CosConfig, CosS3Client
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})

voiceMap = {
    "xiaoxiao": "zh-CN-XiaoxiaoNeural",
    "xiaoyi": "zh-CN-XiaoyiNeural",
    "yunjian": "zh-CN-YunjianNeural",
    "yunxi": "zh-CN-YunxiNeural",
    "yunxia": "zh-CN-YunxiaNeural",
    "yunyang": "zh-CN-YunyangNeural",
    "xiaobei": "zh-CN-liaoning-XiaobeiNeural",
    "xiaoni": "zh-CN-shaanxi-XiaoniNeural",
    "hiugaai": "zh-HK-HiuGaaiNeural",
    "hiumaan": "zh-HK-HiuMaanNeural",
    "wanlung": "zh-HK-WanLungNeural",
    "hsiaochen": "zh-TW-HsiaoChenNeural",
    "hsioayu": "zh-TW-HsiaoYuNeural",
    "yunjhe": "zh-TW-YunJheNeural",
}


def getVoiceById(voiceId):
    return voiceMap.get(voiceId)


# 删除html标签
def remove_html(string):
    regex = re.compile(r'<[^>]+>')
    return regex.sub('', string)


def createAudio(text, file_name, voiceId):
    new_text = remove_html(text)
    print(f"Text without html tags: {new_text}")
    voice = getVoiceById(voiceId)
    if not voice:
        return "error params"

    pwdPath = os.getcwd()
    filePath = pwdPath + "/" + file_name
    dirPath = os.path.dirname(filePath)
    if not os.path.exists(dirPath):
        os.makedirs(dirPath)
    if not os.path.exists(filePath):
        # 用open创建文件 兼容mac
        open(filePath, 'a').close()

    script = 'edge-tts --voice ' + voice + ' --text "' + new_text + '" --write-media ' + filePath
    os.system(script)
    return filePath  # 返回生成的音频文件路径


def getParameter(paramName):
    if request.args.__contains__(paramName):
        return request.args[paramName]
    return ""

@app.route('/dealAudio',methods=['POST','GET'])
def dealAudio():
    text = getParameter('text')
    file_name = str(uuid.uuid4()) + ".mp3"
    voice = getParameter('voice')
    audio_file_path = createAudio(text, file_name, voice)
    return send_file(audio_file_path, as_attachment=True)


@app.route('/')
def index():
    return 'tts!'

if __name__ == "__main__":
EOF
tee ~/tts/run_python_tts <<-'EOF'
docker run -d -p 2020:2020 --name python_tts python_tts gunicorn edge-tts:app -c gunicorn.conf
EOF

容器制作与启动方法

bash 复制代码
#添加运行权限

chmod +x ./dockermake.sh

#运行脚本制作镜像

./dockermake.sh

#运行容器

bash run_python_tts

API:服务器ip:2020/dealAudio?text=欢迎使用tts&voice=xiaoxiao

text 是你需要转换的文本

voice 是配音员 上面自己选

相关推荐
changjiahong15 小时前
lvs总结
服务器·数据库·lvs
Echo flower15 小时前
Docker 容器中 Puppeteer 僵尸进程排查与修复
运维·docker·容器·puppeteer
骊城英雄15 小时前
彩笔运维勇闯机器学习--逻辑回归
运维·机器学习·逻辑回归
孪生质数-15 小时前
AI 应用实践篇——让大模型真正开始工作
linux·运维·服务器·人工智能·深度学习·语言模型·llama
Splashtop高性能远程控制软件15 小时前
连锁零售多门店远程运维指南:从故障响应到批量运维的四项能力
运维·零售·远程控制·splashtop
瞬间&永恒~15 小时前
【MySQL】 InnoDB 锁等待排查与并发压测实验
运维·数据库·mysql
zcmodeltech15 小时前
智能矿井沙盘模型多系统协同控制系统设计:基于STM32与Modbus RTU的感知-传输-控制一体化方案
服务器·数据库·分布式·stm32·单片机·嵌入式硬件
江湖有缘16 小时前
Docker实战 | 使用Docker部署Donetick任务与家务管理应用
运维·docker·容器
流星白龙16 小时前
【Docker】4.NameSpace空间隔离实战
java·运维·docker
有续技术17 小时前
哈斯 (Haas) 机床 IP、端口号配置内容总结
运维·服务器