1een commited on
Commit
8909311
·
1 Parent(s): 9317d4d
Files changed (4) hide show
  1. Dockerfile +2 -15
  2. README.md +19 -30
  3. app.py +10 -20
  4. requirements.txt +1 -3
Dockerfile CHANGED
@@ -1,23 +1,10 @@
1
  FROM python:3.10-slim
2
 
3
- ENV NUMBA_DISABLE_CACHE=1
4
- ENV MPLCONFIGDIR=/tmp
5
- ENV XDG_CACHE_HOME=/tmp
6
- ENV COQUI_TTS_HOME=/tmp/tts_cache
7
- ENV HF_HOME=/tmp/huggingface
8
- ENV TRANSFORMERS_CACHE=/tmp/huggingface/transformers
9
- ENV TORCH_HOME=/tmp/torch
10
- ENV SENTENCEPIECE_CACHE=/tmp/sentencepiece
11
- ENV PYTHON_EGG_CACHE=/tmp/python-eggs
12
- ENV HOME=/tmp
13
- ENV USER=root
14
-
15
  WORKDIR /app
16
  COPY . .
17
 
18
- RUN apt-get update && apt-get install -y espeak-ng libsndfile1 ffmpeg && \
19
- pip install --upgrade pip && \
20
  pip install --no-cache-dir -r requirements.txt
21
 
22
  EXPOSE 7860
23
- CMD env NUMBA_DISABLE_CACHE=1 uvicorn app:app --host 0.0.0.0 --port 7860
 
1
  FROM python:3.10-slim
2
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  WORKDIR /app
4
  COPY . .
5
 
6
+ RUN pip install --upgrade pip && \
 
7
  pip install --no-cache-dir -r requirements.txt
8
 
9
  EXPOSE 7860
10
+ CMD uvicorn app:app --host 0.0.0.0 --port 7860
README.md CHANGED
@@ -9,62 +9,51 @@ pinned: false
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
11
 
12
- # 中文语音合成 API(TTS)
13
 
14
- 本项目基于 [Coqui TTS](https://github.com/coqui-ai/TTS) 和 FastAPI,提供中文文本转语音(Text-to-Speech, TTS)API 服务,支持 Docker 一键部署
15
 
16
  ## 功能说明
17
- - 支持本转语音,使用 baker/tacotron2-DDC-GST 模型
18
- - 提供 RESTful API 接口,返回语音 wav 文件
 
19
  - 适合语音播报、语音助手等场景。
20
 
21
  ## 依赖环境
22
- - Python 3.10
23
- - TTS==0.22.0
24
  - fastapi
25
  - uvicorn
26
- - numba>=0.58.1
27
- - torch==2.5.1
28
- - 需系统依赖:espeak-ng、libsndfile1、ffmpeg
29
 
30
  ## 快速开始
31
 
32
- ### 1. Docker 部署
33
  ```bash
34
- docker build -t tts-api .
35
- docker run -d -p 7860:7860 tts-api
36
  ```
37
 
38
- ### 2. 本地运行
39
- 1. 安装系统依赖:
40
- ```bash
41
- sudo apt-get update && sudo apt-get install -y espeak-ng libsndfile1 ffmpeg
42
- ```
43
- 2. 安装 Python 依赖:
44
- ```bash
45
- pip install -r requirements.txt
46
- ```
47
- 3. 启动服务:
48
- ```bash
49
- uvicorn app:app --host 0.0.0.0 --port 7860
50
- ```
51
 
52
  ## API 用法
53
 
54
  ### POST /synthesize
55
  - **请求体**:JSON
56
  ```json
57
- { "text": "你好,世界!" }
58
  ```
59
- - **返回**:音频件(audio/wav)
 
60
 
61
  ### 示例
62
  ```bash
63
  curl -X POST "http://localhost:7860/synthesize" \
64
  -H "Content-Type: application/json" \
65
- -d '{"text": "你好,世界!"}' --output output.wav
66
  ```
67
 
68
  ## 参考
69
- - [Coqui TTS 官方文档](https://tts.readthedocs.io/zh/latest/)
70
- - [HuggingFace 模型库](https://huggingface.co/coqui/)
 
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
11
 
12
+ # 基于 Edge-TTS 的中文语音合成 API
13
 
14
+ 本项目基于 [edge-tts](https://github.com/rany2/edge-tts) 和 FastAPI,提供中文及多语言文本转语音(Text-to-Speech, TTS)API 服务,支持自定义发音人,输出 mp3 文件
15
 
16
  ## 功能说明
17
+ - 支持多语言文本转语音,默认中文女声(zh-CN-XiaoxiaoNeural)
18
+ - 可通过参数自定义 Microsoft Edge TTS 支持的 voice
19
+ - 提供 RESTful API 接口,返回 mp3 音频文件。
20
  - 适合语音播报、语音助手等场景。
21
 
22
  ## 依赖环境
23
+ - Python 3.8+
24
+ - edge-tts
25
  - fastapi
26
  - uvicorn
 
 
 
27
 
28
  ## 快速开始
29
 
30
+ ### 1. 安装依赖
31
  ```bash
32
+ pip install -r requirements.txt
 
33
  ```
34
 
35
+ ### 2. 启动服务
36
+ ```bash
37
+ uvicorn app:app --host 0.0.0.0 --port 7860
38
+ ```
 
 
 
 
 
 
 
 
 
39
 
40
  ## API 用法
41
 
42
  ### POST /synthesize
43
  - **请求体**:JSON
44
  ```json
45
+ { "text": "你好,世界!", "voice": "zh-CN-XiaoxiaoNeural" }
46
  ```
47
+ - `voice` 可选,默认 zh-CN-XiaoxiaoNeural。可用 voice 参考 edge-tts 官方档。
48
+ - **返回**:音频文件(audio/mpeg,mp3 格式)
49
 
50
  ### 示例
51
  ```bash
52
  curl -X POST "http://localhost:7860/synthesize" \
53
  -H "Content-Type: application/json" \
54
+ -d '{"text": "你好,世界!", "voice": "zh-CN-XiaoxiaoNeural"}' --output output.mp3
55
  ```
56
 
57
  ## 参考
58
+ - [edge-tts 官方文档](https://github.com/rany2/edge-tts)
59
+ - [可用 voice 列表](https://github.com/rany2/edge-tts#voices)
app.py CHANGED
@@ -1,32 +1,22 @@
1
  import os
2
- os.environ["NUMBA_DISABLE_CACHE"] = "1"
3
- os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
4
- os.environ["MPLCONFIGDIR"] = "/tmp"
5
- os.environ["XDG_CACHE_HOME"] = "/tmp"
6
- os.environ["COQUI_TTS_HOME"] = "/tmp/tts_cache"
7
- os.environ["HF_HOME"] = "/tmp/huggingface"
8
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface/transformers"
9
- os.environ["TORCH_HOME"] = "/tmp/torch"
10
- os.environ["SENTENCEPIECE_CACHE"] = "/tmp/sentencepiece"
11
- os.environ["PYTHON_EGG_CACHE"] = "/tmp/python-eggs"
12
-
13
- from TTS.api import TTS
14
  from fastapi import FastAPI
15
  from pydantic import BaseModel
16
  from fastapi.responses import FileResponse
17
- import uvicorn
18
- import uuid
19
 
20
  app = FastAPI()
21
 
22
- # 加载中文 Tacotron2 模型
23
- tts = TTS(model_name="tts_models/zh-CN/baker/tacotron2-DDC-GST", progress_bar=False)
24
-
25
  class TTSRequest(BaseModel):
26
  text: str
 
27
 
28
  @app.post("/synthesize")
29
  def synthesize(req: TTSRequest):
30
- output_path = f"/tmp/{uuid.uuid4().hex}.wav"
31
- tts.tts_to_file(text=req.text, file_path=output_path)
32
- return FileResponse(output_path, media_type="audio/wav")
 
 
 
 
1
  import os
2
+ import uuid
3
+ import asyncio
 
 
 
 
 
 
 
 
 
 
4
  from fastapi import FastAPI
5
  from pydantic import BaseModel
6
  from fastapi.responses import FileResponse
7
+ import edge_tts
 
8
 
9
  app = FastAPI()
10
 
 
 
 
11
  class TTSRequest(BaseModel):
12
  text: str
13
+ voice: str = "zh-CN-XiaoxiaoNeural" # 默认中文女声
14
 
15
  @app.post("/synthesize")
16
  def synthesize(req: TTSRequest):
17
+ output_path = f"/tmp/{uuid.uuid4().hex}.mp3"
18
+ async def run_tts():
19
+ communicate = edge_tts.Communicate(req.text, req.voice)
20
+ await communicate.save(output_path)
21
+ asyncio.run(run_tts())
22
+ return FileResponse(output_path, media_type="audio/mpeg")
requirements.txt CHANGED
@@ -1,5 +1,3 @@
1
- TTS==0.22.0
2
  fastapi
3
  uvicorn
4
- numba>=0.58.1
5
- torch==2.5.1
 
1
+ edge-tts
2
  fastapi
3
  uvicorn