feat: update Dockerfile and README for TTS service setup and usage instructions

This commit is contained in:
王锦强
2025-03-09 13:49:32 +08:00
parent e309375c49
commit 9cf173946d
5 changed files with 107 additions and 26 deletions

1
.gitignore vendored
View File

@@ -25,3 +25,4 @@ go.work.sum
# env file
.env
.idea/

View File

@@ -1,7 +1,7 @@
# 使用官方 Golang 镜像作为构建环境
FROM golang:1.22-alpine as builder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
FROM golang:1.22-alpine AS builder
ENV CGO_ENABLED=0
ENV GOPROXY=https://goproxy.cn,direct
# 设置工作目录
WORKDIR /app
@@ -16,17 +16,24 @@ RUN go mod download
COPY . .
# 构建 Go 应用程序
RUN go build -o main . && ls
RUN go build -o main ./cmd/api/main.go
# 使用 scratch 作为基础镜像
# 使用 alpine 作为基础镜像
FROM alpine
# 安装 ffmpeg 和 CA 证书
RUN apk update --no-cache && \
apk add --no-cache ca-certificates ffmpeg tzdata && \
rm -rf /var/cache/apk/*
# 从 builder 阶段复制可执行文件到当前阶段
COPY --from=builder /app/main .
COPY --from=builder /app/templates ./templates
RUN apk update --no-cache && apk add --no-cache ca-certificates
ENV TZ Asia/Shanghai
COPY --from=builder /app/web ./web
COPY --from=builder /app/configs ./configs
# 设置时区
ENV TZ=Asia/Shanghai
# 暴露端口
EXPOSE 8080

View File

@@ -1,9 +1,97 @@
# docker 部署
# TTS 服务
一个简单易用的文本转语音 (TTS) 服务,基于 Microsoft Azure 语音服务,提供高质量的语音合成能力。
## 功能特点
- 支持多种语言和声音
- 可调节语速和语调
- 支持多种输出音频格式
- 兼容 OpenAI TTS API
- 支持长文本自动分割与合并
- 提供 Web UI 和 RESTful API
## 快速开始
### Docker 部署
```shell
docker run -d -p 8080:8080 --name=tts zuoban/zb-tts
```
部署完成后,访问 `http://localhost:8080` 使用 Web 界面,或通过 `http://localhost:8080/api-doc` 查看 API 文档。
# cloudflare worker 部署
[worker.js](https://raw.githubusercontent.com/zuoban/tts/main/templates/worker.js)
### Cloudflare Worker 部署
1. 创建一个新的 Cloudflare Worker
2. 复制以下脚本内容到 Worker
[worker.js](https://raw.githubusercontent.com/zuoban/tts/main/web/templates/worker.js)
## API 使用示例
### 基础 API
```shell
# 基础文本转语音
curl "http://localhost:8080/tts?t=你好,世界&v=zh-CN-XiaoxiaoNeural"
# 调整语速和语调
curl "http://localhost:8080/tts?t=你好,世界&v=zh-CN-XiaoxiaoNeural&r=20&p=10"
# 使用情感风格
curl "http://localhost:8080/tts?t=今天天气真好&v=zh-CN-XiaoxiaoNeural&s=cheerful"
```
### OpenAI 兼容 API
```shell
curl -X POST "http://localhost:8080/v1/audio/speech" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "你好,世界!",
"voice": "zh-CN-XiaoxiaoNeural"
}'
```
## 配置选项
您可以通过环境变量或配置文件自定义 TTS 服务:
```shell
# 使用自定义端口
docker run -d -p 9000:9000 -e PORT=9000 --name=tts zuoban/zb-tts
# 使用配置文件
docker run -d -p 8080:8080 -v /path/to/config.yaml:/app/config.yaml --name=tts zuoban/zb-tts
```
## 本地构建与运行
要从源码构建和运行:
```shell
# 克隆仓库
git clone https://github.com/zuoban/tts.git
cd tts
# 构建
go build -o tts ./cmd/api
# 运行
./tts
```
## 支持的音频格式
- MP3: `audio-24khz-48kbitrate-mono-mp3`(默认)
- MP3: `audio-24khz-96kbitrate-mono-mp3`
- MP3: `audio-24khz-160kbitrate-mono-mp3`
- WAV: `riff-24khz-16bit-mono-pcm`
- OGG: `ogg-24khz-16bit-mono-opus`
更多格式请参考 API 文档。
## 许可证
MIT

View File

@@ -66,8 +66,6 @@ func (a *App) Start() error {
case err := <-errChan:
return err
case <-quit:
log.Println("接收到退出信号,正在优雅关闭...")
// 创建一个超时上下文用于优雅关闭
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

13
main.go
View File

@@ -1,13 +0,0 @@
package main
import (
"tts/routes"
)
func main() {
router := routes.SetupRouter()
err := router.Run(":8080")
if err != nil {
panic(err)
}
}