Skip to content

Commit 7d79be9

Browse files
author
赵嘉琦
committed
feat:豆神一期,添加音色,查询音色列表
1 parent f9e6c69 commit 7d79be9

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
import pytest
3+
from zhipuai.api_resource.tts import TTSApi
4+
5+
# 请根据实际情况填写
6+
BASE_URL_LIST = "http://localhost:9203"
7+
BASE_URL_ADD = "http://localhost:9203"
8+
API_TOKEN = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX3R5cGUiOiJTRVJWSUNFIiwidXNlcl9pZCI6MywiYXBpX2tleSI6IjM0ZDhiM2ExNDQ2OTQ0MmY5MTJkNzg5MTJjZjZlMjU1IiwidXNlcl9rZXkiOiIyNzQyNjM0My1iYjI0LTQ4ZDktODVjNy01ODFmMjUwNzBiMzMiLCJjdXN0b21lcl9pZCI6IjEwMDAwMyIsInVzZXJuYW1lIjoiY3BjMTk4NiJ9.3MsaFjRAyArDp2WZsVbvKGPrVjkKvGB5EfBafprcFogbDeZv4s9VCQt4wUaR4FVon1tiL_pnqtMaI6qGGXs0Qg"
9+
AUDIO_FILE_PATH = r"C:\Users\zjq18\Downloads\北京今天的天气.wav"
10+
11+
@pytest.mark.integration
12+
def test_list_voices():
13+
tts_api = TTSApi(base_url=BASE_URL_LIST, api_key=API_TOKEN)
14+
resp = tts_api.list_voices()
15+
assert resp.code == 200
16+
assert resp.success
17+
assert isinstance(resp.data, list)
18+
if resp.data:
19+
assert hasattr(resp.data[0], "voiceId")
20+
21+
@pytest.mark.integration
22+
def test_add_voice():
23+
if not os.path.exists(AUDIO_FILE_PATH):
24+
pytest.skip("音频文件不存在,跳过上传测试")
25+
tts_api = TTSApi(base_url=BASE_URL_ADD, api_key=API_TOKEN)
26+
resp = tts_api.add_voice(
27+
voice_name="磁性嗓音123",
28+
voice_text="今天北京的天气怎么样?",
29+
file_path=AUDIO_FILE_PATH
30+
)
31+
assert resp.code == 200
32+
assert resp.success
33+
assert resp.data.voiceName == "磁性嗓音123"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .tts import TTSApi

zhipuai/api_resource/tts/tts.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import requests
2+
from typing import Optional
3+
from zhipuai.types.tts.voice import VoiceListResponse, VoiceAddResponse
4+
5+
class TTSApi:
6+
def __init__(self, base_url: str, api_key: Optional[str] = None):
7+
self.base_url = base_url.rstrip('/')
8+
self.api_key = api_key
9+
10+
def list_voices(self) -> VoiceListResponse:
11+
url = f"{self.base_url}/v4/voice/list"
12+
headers = {}
13+
if self.api_key:
14+
headers["Authorization"] = f"Bearer {self.api_key}"
15+
16+
resp = requests.post(url,headers=headers)
17+
resp.raise_for_status()
18+
return VoiceListResponse.parse_obj(resp.json())
19+
20+
def add_voice(self, voice_name: str, voice_text: str, file_path: str) -> VoiceAddResponse:
21+
url = f"{self.base_url}/v4/voice/add"
22+
headers = {}
23+
if self.api_key:
24+
headers["Authorization"] = f"Bearer {self.api_key}"
25+
files = {
26+
"voiceName": (None, voice_name),
27+
"voiceText": (None, voice_text),
28+
"file": open(file_path, "rb"),
29+
}
30+
resp = requests.post(url, headers=headers, files=files)
31+
resp.raise_for_status()
32+
return VoiceAddResponse.parse_obj(resp.json())

zhipuai/types/tts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .voice import Voice, VoiceListResponse, VoiceAddResponse

zhipuai/types/tts/voice.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
from pydantic import BaseModel
3+
4+
class Voice(BaseModel):
5+
id: int
6+
voiceId: str
7+
voiceName: str
8+
voiceType: str
9+
voiceStatus: str
10+
voiceText: str
11+
createTime: str
12+
updateTime: str
13+
14+
class VoiceListResponse(BaseModel):
15+
code: int
16+
msg: str
17+
data: List[Voice]
18+
success: bool
19+
20+
class VoiceAddResponse(BaseModel):
21+
code: int
22+
msg: str
23+
data: Voice
24+
success: bool

0 commit comments

Comments
 (0)