-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(gsvi_tts): Use the correct calling method #7083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac16b46
081bc92
eb000bf
d7b0a72
4387e53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,5 @@ | ||||||||||
| import os | ||||||||||
| import urllib.parse | ||||||||||
| import uuid | ||||||||||
| from pathlib import Path | ||||||||||
|
|
||||||||||
| import aiohttp | ||||||||||
|
|
||||||||||
|
|
@@ -23,37 +22,55 @@ def __init__( | |||||||||
| provider_settings: dict, | ||||||||||
| ) -> None: | ||||||||||
| super().__init__(provider_config, provider_settings) | ||||||||||
| self.api_base = provider_config.get("api_base", "http://127.0.0.1:5000") | ||||||||||
| self.api_key = provider_config.get("api_key", "") | ||||||||||
| self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000") | ||||||||||
| self.api_base = self.api_base.removesuffix("/") | ||||||||||
| self.version = provider_config.get("version", "v4") | ||||||||||
| self.character = provider_config.get("character") | ||||||||||
| self.emotion = provider_config.get("emotion") | ||||||||||
| self.prompt_text_lang = provider_config.get("prompt_text_lang", "中文") | ||||||||||
| self.emotion = provider_config.get("emotion", "默认") | ||||||||||
| self.text_lang = provider_config.get("text_lang", "中文") | ||||||||||
|
|
||||||||||
| async def get_audio(self, text: str) -> str: | ||||||||||
| temp_dir = get_astrbot_temp_path() | ||||||||||
| path = os.path.join(temp_dir, f"gsvi_tts_{uuid.uuid4()}.wav") | ||||||||||
| params = {"text": text} | ||||||||||
| path = Path(temp_dir) / f"gsvi_tts_{uuid.uuid4()}.wav" | ||||||||||
| url = f"{self.api_base}/infer_single" | ||||||||||
|
|
||||||||||
| if self.character: | ||||||||||
| params["character"] = self.character | ||||||||||
| if self.emotion: | ||||||||||
| params["emotion"] = self.emotion | ||||||||||
| headers = {"Content-Type": "application/json"} | ||||||||||
| if self.api_key: | ||||||||||
| headers["Authorization"] = f"Bearer {self.api_key}" | ||||||||||
|
|
||||||||||
| query_parts = [] | ||||||||||
| for key, value in params.items(): | ||||||||||
| encoded_value = urllib.parse.quote(str(value)) | ||||||||||
| query_parts.append(f"{key}={encoded_value}") | ||||||||||
|
|
||||||||||
| url = f"{self.api_base}/tts?{'&'.join(query_parts)}" | ||||||||||
| data = { | ||||||||||
| "dl_url": self.api_base, | ||||||||||
| "version": self.version, | ||||||||||
| "model_name": self.character, | ||||||||||
| "prompt_text_lang": self.prompt_text_lang, | ||||||||||
| "emotion": self.emotion, | ||||||||||
| "text": text, | ||||||||||
| "text_lang": self.text_lang, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| async with aiohttp.ClientSession() as session: | ||||||||||
|
Rain-0x01-39 marked this conversation as resolved.
|
||||||||||
| async with session.get(url) as response: | ||||||||||
| async with session.post(url, json=data, headers=headers) as response: | ||||||||||
| if response.status == 200: | ||||||||||
| with open(path, "wb") as f: | ||||||||||
| f.write(await response.read()) | ||||||||||
| resp_json = await response.json() | ||||||||||
| msg = resp_json.get("msg") | ||||||||||
| audio_url = resp_json.get("audio_url") | ||||||||||
| if not msg or msg != "合成成功": | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| raise Exception(f"GSVI TTS API 合成失败: {msg}") | ||||||||||
| async with session.get(audio_url) as audio_response: | ||||||||||
| if audio_response.status == 200: | ||||||||||
| with open(path, "wb") as f: | ||||||||||
| f.write(await audio_response.read()) | ||||||||||
|
Comment on lines
+63
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在异步函数中使用了同步的文件写入操作 请在文件顶部添加
Suggested change
References
|
||||||||||
| else: | ||||||||||
| error_text = await audio_response.text() | ||||||||||
| raise Exception( | ||||||||||
| f"GSVI TTS API 下载音频失败,状态码: {audio_response.status},错误: {error_text}", | ||||||||||
| ) | ||||||||||
| else: | ||||||||||
| error_text = await response.text() | ||||||||||
| raise Exception( | ||||||||||
| f"GSVI TTS API 请求失败,状态码: {response.status},错误: {error_text}", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| return path | ||||||||||
| return str(path) | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.