Skip to content

Commit 252e292

Browse files
committed
fix: fix the defect of empty line to speech error
--bug=1050760 --user=王孝刚 【应用】文本转语音使用豆包模型时,用户问题文本中间有空行,会报错 https://www.tapd.cn/57709429/s/1636230
1 parent 0372681 commit 252e292

File tree

9 files changed

+28
-8
lines changed

9 files changed

+28
-8
lines changed

apps/common/util/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,7 @@ def split_and_transcribe(file_path, model, max_segment_length_ms=59000, audio_fo
211211
if isinstance(text, str):
212212
full_text.append(text)
213213
return ' '.join(full_text)
214+
215+
216+
def _remove_empty_lines(text):
217+
return '\n'.join(line for line in text.split('\n') if line.strip())

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dashscope
44
from dashscope.audio.tts_v2 import *
55

6+
from common.util.common import _remove_empty_lines
67
from setting.models_provider.base_model_provider import MaxKBBaseModel
78
from setting.models_provider.impl.base_tts import BaseTextToSpeech
89

@@ -37,6 +38,7 @@ def check_auth(self):
3738
def text_to_speech(self, text):
3839
dashscope.api_key = self.api_key
3940
synthesizer = SpeechSynthesizer(model=self.model, **self.params)
41+
text = _remove_empty_lines(text)
4042
audio = synthesizer.call(text)
4143
if type(audio) == str:
4244
print(audio)

apps/setting/models_provider/impl/azure_model_provider/model/tts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from openai import OpenAI, AzureOpenAI
66

77
from common.config.tokenizer_manage_config import TokenizerManage
8+
from common.util.common import _remove_empty_lines
89
from setting.models_provider.base_model_provider import MaxKBBaseModel
910
from setting.models_provider.impl.base_tts import BaseTextToSpeech
1011

@@ -58,6 +59,7 @@ def text_to_speech(self, text):
5859
api_key=self.api_key,
5960
api_version=self.api_version
6061
)
62+
text = _remove_empty_lines(text)
6163
with client.audio.speech.with_streaming_response.create(
6264
model=self.model,
6365
input=text,

apps/setting/models_provider/impl/openai_model_provider/model/tts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from openai import OpenAI
44

55
from common.config.tokenizer_manage_config import TokenizerManage
6+
from common.util.common import _remove_empty_lines
67
from setting.models_provider.base_model_provider import MaxKBBaseModel
78
from setting.models_provider.impl.base_tts import BaseTextToSpeech
89

@@ -51,6 +52,7 @@ def text_to_speech(self, text):
5152
base_url=self.api_base,
5253
api_key=self.api_key
5354
)
55+
text = _remove_empty_lines(text)
5456
with client.audio.speech.with_streaming_response.create(
5557
model=self.model,
5658
input=text,

apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ssl
1919
import websockets
2020

21+
from common.util.common import _remove_empty_lines
2122
from setting.models_provider.base_model_provider import MaxKBBaseModel
2223
from setting.models_provider.impl.base_tts import BaseTextToSpeech
2324

@@ -95,6 +96,7 @@ def text_to_speech(self, text):
9596
"operation": "xxx"
9697
}
9798
}
99+
text = _remove_empty_lines(text)
98100

99101
return asyncio.run(self.submit(request_json, text))
100102

apps/setting/models_provider/impl/xf_model_provider/model/tts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ssl
1919
import websockets
2020

21+
from common.util.common import _remove_empty_lines
2122
from setting.models_provider.base_model_provider import MaxKBBaseModel
2223
from setting.models_provider.impl.base_tts import BaseTextToSpeech
2324

@@ -102,6 +103,8 @@ def text_to_speech(self, text):
102103

103104
# 使用小语种须使用以下方式,此处的unicode指的是 utf16小端的编码方式,即"UTF-16LE"”
104105
# self.Data = {"status": 2, "text": str(base64.b64encode(self.Text.encode('utf-16')), "UTF8")}
106+
text = _remove_empty_lines(text)
107+
105108
async def handle():
106109
async with websockets.connect(self.create_url(), max_size=1000000000, ssl=ssl_context) as ws:
107110
# 发送 full client request

apps/setting/models_provider/impl/xinference_model_provider/model/tts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from openai import OpenAI
44

55
from common.config.tokenizer_manage_config import TokenizerManage
6+
from common.util.common import _remove_empty_lines
67
from setting.models_provider.base_model_provider import MaxKBBaseModel
78
from setting.models_provider.impl.base_tts import BaseTextToSpeech
89

@@ -52,7 +53,7 @@ def text_to_speech(self, text):
5253
api_key=self.api_key
5354
)
5455
# ['中文女', '中文男', '日语男', '粤语女', '英文女', '英文男', '韩语女']
55-
56+
text = _remove_empty_lines(text)
5657
with client.audio.speech.with_streaming_response.create(
5758
model=self.model,
5859
input=text,

ui/src/api/application.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,10 @@ const getPlatformConfig: (application_id: string, type: string) => Promise<Resul
429429
const updatePlatformConfig: (
430430
application_id: string,
431431
type: string,
432-
data: any
433-
) => Promise<Result<any>> = (application_id, type, data) => {
434-
return post(`/platform/${application_id}/${type}`, data)
432+
data: any,
433+
loading?: Ref<boolean>
434+
) => Promise<Result<any>> = (application_id, type, data, loading) => {
435+
return post(`/platform/${application_id}/${type}`, data, undefined, loading)
435436
}
436437
/**
437438
* 更新平台状态

ui/src/views/application/component/AccessSettingDrawer.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,13 @@ const submit = async () => {
222222
formRef.value?.validate(async (valid) => {
223223
if (valid) {
224224
try {
225-
await applicationApi.updatePlatformConfig(id, configType.value, form[configType.value])
226-
MsgSuccess('配置保存成功')
227-
closeDrawer()
228-
emit('refresh')
225+
applicationApi
226+
.updatePlatformConfig(id, configType.value, form[configType.value], loading)
227+
.then(() => {
228+
MsgSuccess('配置保存成功')
229+
closeDrawer()
230+
emit('refresh')
231+
})
229232
} catch {
230233
MsgError('保存失败,请检查输入或稍后再试')
231234
}

0 commit comments

Comments
 (0)