forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
68 lines (50 loc) · 2.14 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json, logging
import aiohttp, asyncio
async def reecho_ai_api(text):
url = 'https://v1.reecho.ai/api/tts/simple-generate'
# reecho_ai = self.config.get("reecho_ai")
reecho_ai = {
"Authorization": "sk-xxx",
"model": "reecho-neural-voice-001",
"randomness": 97,
"stability_boost": 40,
"voiceId": "b4b885c3-89a7-46d4-badb-015a55bb3a91",
"text": "你好"
}
headers = {
"Authorization": f"Bearer {reecho_ai['Authorization']}",
"Content-Type": "application/json"
}
params = {
"model": reecho_ai['model'],
'randomness': reecho_ai['randomness'],
'stability_boost': reecho_ai['stability_boost'],
'voiceId': reecho_ai['voiceId'],
'text': text
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=params) as response:
ret = await response.json()
print(ret)
file_url = ret["data"]["audio"]
print(file_url)
return None
async with session.get(file_url) as response:
if response.status == 200:
content = await response.read()
# voice_tmp_path = os.path.join(self.audio_out_path, 'reecho_ai_' + self.common.get_bj_time(4) + '.wav')
file_name = 'reecho_ai_' + self.common.get_bj_time(4) + '.wav'
voice_tmp_path = self.common.get_new_audio_path(self.audio_out_path, file_name)
with open(voice_tmp_path, 'wb') as file:
file.write(content)
return voice_tmp_path
else:
print(f'reecho.ai下载音频失败: {response.status}')
return None
except aiohttp.ClientError as e:
print(f'reecho.ai请求失败: {e}')
except Exception as e:
print(f'reecho.ai未知错误: {e}')
return None
asyncio.run(reecho_ai_api("你好"))