From 32e5c8195ac5f7ad19d129b2f5f522d248eaca49 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 24 Dec 2024 16:28:46 +0800 Subject: [PATCH] fix: fix the defect of empty line to speech error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1050760 --user=王孝刚 【应用】文本转语音使用豆包模型时,用户问题文本中间有空行,会报错 https://www.tapd.cn/57709429/s/1636230 --- apps/common/util/common.py | 4 ++++ .../impl/aliyun_bai_lian_model_provider/model/tts.py | 2 ++ .../impl/azure_model_provider/model/tts.py | 2 ++ .../impl/openai_model_provider/model/tts.py | 2 ++ .../impl/volcanic_engine_model_provider/model/tts.py | 2 ++ .../impl/xf_model_provider/model/tts.py | 3 +++ .../impl/xinference_model_provider/model/tts.py | 3 ++- ui/src/api/application.ts | 7 ++++--- .../application/component/AccessSettingDrawer.vue | 11 +++++++---- 9 files changed, 28 insertions(+), 8 deletions(-) diff --git a/apps/common/util/common.py b/apps/common/util/common.py index f442dbceeb3..deeef54f153 100644 --- a/apps/common/util/common.py +++ b/apps/common/util/common.py @@ -211,3 +211,7 @@ def split_and_transcribe(file_path, model, max_segment_length_ms=59000, audio_fo if isinstance(text, str): full_text.append(text) return ' '.join(full_text) + + +def _remove_empty_lines(text): + return '\n'.join(line for line in text.split('\n') if line.strip()) diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py index 1dbee97d3e3..9bfe38f428e 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py @@ -3,6 +3,7 @@ import dashscope from dashscope.audio.tts_v2 import * +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -37,6 +38,7 @@ def check_auth(self): def text_to_speech(self, text): dashscope.api_key = self.api_key synthesizer = SpeechSynthesizer(model=self.model, **self.params) + text = _remove_empty_lines(text) audio = synthesizer.call(text) if type(audio) == str: print(audio) diff --git a/apps/setting/models_provider/impl/azure_model_provider/model/tts.py b/apps/setting/models_provider/impl/azure_model_provider/model/tts.py index 83ed6627547..b029f1fcacb 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/azure_model_provider/model/tts.py @@ -5,6 +5,7 @@ from openai import OpenAI, AzureOpenAI from common.config.tokenizer_manage_config import TokenizerManage +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -58,6 +59,7 @@ def text_to_speech(self, text): api_key=self.api_key, api_version=self.api_version ) + text = _remove_empty_lines(text) with client.audio.speech.with_streaming_response.create( model=self.model, input=text, diff --git a/apps/setting/models_provider/impl/openai_model_provider/model/tts.py b/apps/setting/models_provider/impl/openai_model_provider/model/tts.py index 6e9aa2c6e02..0eeab1d7cce 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/openai_model_provider/model/tts.py @@ -3,6 +3,7 @@ from openai import OpenAI from common.config.tokenizer_manage_config import TokenizerManage +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -51,6 +52,7 @@ def text_to_speech(self, text): base_url=self.api_base, api_key=self.api_key ) + text = _remove_empty_lines(text) with client.audio.speech.with_streaming_response.create( model=self.model, input=text, diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py index ec39f22c040..8ef806474dd 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py @@ -18,6 +18,7 @@ import ssl import websockets +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -95,6 +96,7 @@ def text_to_speech(self, text): "operation": "xxx" } } + text = _remove_empty_lines(text) return asyncio.run(self.submit(request_json, text)) diff --git a/apps/setting/models_provider/impl/xf_model_provider/model/tts.py b/apps/setting/models_provider/impl/xf_model_provider/model/tts.py index 1d677b7c32a..1db844df12a 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/xf_model_provider/model/tts.py @@ -18,6 +18,7 @@ import ssl import websockets +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -102,6 +103,8 @@ def text_to_speech(self, text): # 使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"” # self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")} + text = _remove_empty_lines(text) + async def handle(): async with websockets.connect(self.create_url(), max_size=1000000000, ssl=ssl_context) as ws: # 发送 full client request diff --git a/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py b/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py index 14206124db4..3a9be27dbc2 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py @@ -3,6 +3,7 @@ from openai import OpenAI from common.config.tokenizer_manage_config import TokenizerManage +from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech @@ -52,7 +53,7 @@ def text_to_speech(self, text): api_key=self.api_key ) # ['中文女', '中文男', '日语男', '粤语女', '英文女', '英文男', '韩语女'] - + text = _remove_empty_lines(text) with client.audio.speech.with_streaming_response.create( model=self.model, input=text, diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts index 5b6712ce8d0..2870d96b03c 100644 --- a/ui/src/api/application.ts +++ b/ui/src/api/application.ts @@ -429,9 +429,10 @@ const getPlatformConfig: (application_id: string, type: string) => Promise Promise> = (application_id, type, data) => { - return post(`/platform/${application_id}/${type}`, data) + data: any, + loading?: Ref +) => Promise> = (application_id, type, data, loading) => { + return post(`/platform/${application_id}/${type}`, data, undefined, loading) } /** * 更新平台状态 diff --git a/ui/src/views/application/component/AccessSettingDrawer.vue b/ui/src/views/application/component/AccessSettingDrawer.vue index b5d90b7a2d1..a2522556e6f 100644 --- a/ui/src/views/application/component/AccessSettingDrawer.vue +++ b/ui/src/views/application/component/AccessSettingDrawer.vue @@ -222,10 +222,13 @@ const submit = async () => { formRef.value?.validate(async (valid) => { if (valid) { try { - await applicationApi.updatePlatformConfig(id, configType.value, form[configType.value]) - MsgSuccess('配置保存成功') - closeDrawer() - emit('refresh') + applicationApi + .updatePlatformConfig(id, configType.value, form[configType.value], loading) + .then(() => { + MsgSuccess('配置保存成功') + closeDrawer() + emit('refresh') + }) } catch { MsgError('保存失败,请检查输入或稍后再试') }