-
-
Notifications
You must be signed in to change notification settings - Fork 914
fix: 修复 CosyVoice V2,Qwen TTS 生成报错的问题 #2964
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
base: master
Are you sure you want to change the base?
Conversation
…h CosyVoice V2, Qwen TTS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `astrbot/core/provider/sources/dashscope_tts.py:115` </location>
<code_context>
+ try:
+ with urlopen(url, timeout=timeout) as response:
+ return response.read()
+ except (URLError, TimeoutError, OSError):
+ return None
+
</code_context>
<issue_to_address>
**suggestion:** Consider logging or surfacing download errors for troubleshooting.
Logging the exception details instead of silently returning None will make it easier to identify and resolve download issues.
Suggested implementation:
```python
try:
with urlopen(url, timeout=timeout) as response:
return response.read()
except (URLError, TimeoutError, OSError) as e:
logger.error(f"Failed to download audio from URL '{url}': {e}", exc_info=True)
return None
```
```python
import logging
logger = logging.getLogger(__name__)
from ..entities import ProviderType
from ..provider import TTSProvider
```
</issue_to_address>
### Comment 2
<location> `astrbot/core/provider/sources/dashscope_tts.py:91` </location>
<code_context>
def _extract_audio_from_response(self, response) -> Optional[bytes]:
output = getattr(response, "output", None)
audio_obj = getattr(output, "audio", None) if output is not None else None
if not audio_obj:
return None
data_b64 = getattr(audio_obj, "data", None)
if data_b64:
try:
return base64.b64decode(data_b64)
except (ValueError, TypeError):
return None
url = getattr(audio_obj, "url", None)
if url:
return self._download_audio_from_url(url)
return None
</code_context>
<issue_to_address>
**issue (code-quality):** Use named expression to simplify assignment and conditional [×2] ([`use-named-expression`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/use-named-expression/))
</issue_to_address>
### Comment 3
<location> `astrbot/core/provider/sources/dashscope_tts.py:154-156` </location>
<code_context>
def _format_dashscope_error(self, response) -> str:
status_code = getattr(response, "status_code", None)
code = getattr(response, "code", None)
message = getattr(response, "message", None)
parts = []
if status_code is not None:
parts.append(f"status_code={status_code}")
if code:
parts.append(f"code={code}")
if message:
parts.append(f"message={message}")
if not parts:
return ""
return " ".join(parts)
</code_context>
<issue_to_address>
**suggestion (code-quality):** We've found these issues:
- Lift code into else after jump in control flow ([`reintroduce-else`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/reintroduce-else/))
- Replace if statement with if expression ([`assign-if-exp`](https://docs.sourcery.ai/Reference/Default-Rules/refactorings/assign-if-exp/))
```suggestion
return "" if not parts else " ".join(parts)
```
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
if not parts: | ||
return "" | ||
return " ".join(parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): We've found these issues:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if not parts: | |
return "" | |
return " ".join(parts) | |
return "" if not parts else " ".join(parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要换成 aiohttp 进行异步网络请求,否则在请求时会堵塞整个bot
已经替换为aiohttp以异步下载音频。经过再次测试后运行正常。 |
2553
fixes #2553
Motivation / 动机
修复了CosyVoice V2,Qwen TTS生成报错的问题。Fixed compatability problems with CosyVoice V2, Qwen TTS.
Modifications / 改动点
只更改了
dashscope_tts.py
这一个文件。更新了API调用方式,CosyVoice与Qwen TTS使用不同格式调用。Verification Steps / 验证步骤
添加CosyVoice V2,Qwen TTS模型,先测试服务提供商可用性。然后在聊天中开启TTS语音生成功能,实际验证。
为了方便测试,可以使用下面的配置。注意替换API Key。
Screenshots or Test Results / 运行截图或测试结果
Compatibility & Breaking Changes / 兼容性与破坏性变更
Checklist / 检查清单
requirements.txt
和pyproject.toml
文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txt
andpyproject.toml
.Sourcery 总结
通过分离 CosyVoice V2 和 Qwen TTS 模型的合成流程,并添加响应解析、错误处理和正确的文件扩展名处理,修复了 dashscope TTS 提供者中的兼容性问题。
错误修复:
功能增强:
get_audio
方法,根据模型类型将请求路由到_synthesize_with_qwen_tts
或_synthesize_with_cosyvoice
。Original summary in English
Summary by Sourcery
Fix compatibility issues in dashscope TTS provider by separating synthesis flows for CosyVoice V2 and Qwen TTS models, adding response parsing, error handling, and correct file extension handling.
Bug Fixes:
Enhancements: