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

一、简介

准备用基于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 是配音员 上面自己选

相关推荐
SKYDROID云卓小助手2 小时前
无人设备遥控器之数字图传技术
运维·服务器·单片机·嵌入式硬件·fpga开发
努力努力再努力wz2 小时前
【Linux进阶系列】:线程(上)
java·linux·运维·服务器·数据结构·c++·redis
2301_803554522 小时前
面试后查缺补漏--cmake,makefiles,g++,gcc(自写精华版)
linux·运维·服务器
102400243 小时前
ubuntu系统中 jupyter Kernel 频繁崩溃原因
linux·运维·服务器
深圳市恒讯科技3 小时前
使用站群服务器做SEO,如何避免被搜索引擎判定为“站群作弊”?
服务器·搜索引擎·php
LilySesy3 小时前
ABAP+如果在join的时候需要表1的字段某几位等于表2的字段的某几位,需要怎么做?
服务器·前端·数据库·sap·abap·alv
大唐荣华3 小时前
工业制造领域的ODM、OEM、EMS、JDM、CM、OBM都是啥
运维·产品运营·制造
z10_143 小时前
海外住宅ip怎么区分干净程度以及怎么选择海外住宅ip
服务器·网络·网络协议·tcp/ip
R-G-B4 小时前
【P7】docker镜像发布和部署
运维·docker·容器·docker镜像·docker镜像发布和部署·镜像发布和部署·docker镜像发布
岸边的风4 小时前
FileRise 让文件管理变简单,搭配cpolar实现远程自由访问
服务器