Skip to content

Commit d7fa9d3

Browse files
committed
edge_tts 支持对语速的控制
1 parent 8ba4a84 commit d7fa9d3

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

gpt_server/openai_api_protocol/custom_api_protocol.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class SpeechRequest(BaseModel):
3030
)
3131
speed: Optional[float] = Field(
3232
default=1.0,
33-
description="The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.",
33+
description="The speed of the generated audio. Select a value from 0.25 to 5.0. 1.0 is the default.",
34+
ge=0,
35+
le=5,
3436
)
3537

3638

gpt_server/serving/openai_api_server.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
- Chat Completions. (Reference: https://platform.openai.com/docs/api-reference/chat)
44
- Completions. (Reference: https://platform.openai.com/docs/api-reference/completions)
55
- Embeddings. (Reference: https://platform.openai.com/docs/api-reference/embeddings)
6-
7-
Usage:
8-
python3 -m fastchat.serve.openai_api_server
6+
- Moderations. (Reference: https://platform.openai.com/docs/api-reference/moderations)
7+
- Audio. (Reference: https://platform.openai.com/docs/api-reference/audio)
98
"""
109

1110
import asyncio
@@ -721,7 +720,14 @@ async def speech(request: SpeechRequest):
721720
)
722721
filename = f"{uuid.uuid4()}.mp3"
723722
output_path = os.path.join(OUTPUT_DIR, filename)
724-
communicate = edge_tts.Communicate(text=request.input, voice=request.voice)
723+
rate = 1.0
724+
if request.speed >= 1:
725+
rate = f"+{int((request.speed - 1) * 100)}%"
726+
else:
727+
rate = f"-{int((1-request.speed) * 100)}%"
728+
communicate = edge_tts.Communicate(
729+
text=request.input, voice=request.voice, rate=rate
730+
)
725731
await communicate.save(output_path)
726732
return FileResponse(output_path, media_type="audio/mpeg", filename=filename)
727733

gpt_server/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Tuple
22

3-
__version__ = "0.3.2"
3+
__version__ = "0.3.5"
44
short_version = __version__
55

66

tests/test_tts.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
from pathlib import Path
22
from openai import OpenAI
3+
import asyncio
4+
import edge_tts
5+
from rich import print
36

4-
# 新版本 opnai
5-
client = OpenAI(api_key="EMPTY", base_url="http://localhost:8082/v1")
6-
speech_file_path = Path(__file__).parent / "speech.mp3"
7-
response = client.audio.speech.create(
8-
model="edge_tts",
9-
voice="zh-CN-YunxiNeural",
10-
input="你好啊,我是人工智能。",
11-
)
12-
response.write_to_file(speech_file_path)
7+
8+
async def main():
9+
list_voices = await edge_tts.list_voices()
10+
zh_list_voices = [i["ShortName"] for i in list_voices if "zh-CN" in i["ShortName"]]
11+
print(f"支持以下中文voice: \n{zh_list_voices}")
12+
# 新版本 opnai
13+
client = OpenAI(api_key="EMPTY", base_url="http://localhost:8082/v1")
14+
speech_file_path = Path(__file__).parent / "speech.mp3"
15+
response = client.audio.speech.create(
16+
model="edge_tts",
17+
voice="zh-CN-YunxiNeural",
18+
input="你好啊,我是人工智能。",
19+
speed=1.0,
20+
)
21+
response.write_to_file(speech_file_path)
22+
23+
24+
if __name__ == "__main__":
25+
asyncio.run(main())

0 commit comments

Comments
 (0)