Skip to content

Commit

Permalink
fix: fix the defect of empty line to speech error
Browse files Browse the repository at this point in the history
--bug=1050760 --user=王孝刚 【应用】文本转语音使用豆包模型时,用户问题文本中间有空行,会报错 https://www.tapd.cn/57709429/s/1636230
  • Loading branch information
wxg0103 committed Dec 24, 2024
1 parent 0372681 commit 32e5c81
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions apps/common/util/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions ui/src/api/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,10 @@ const getPlatformConfig: (application_id: string, type: string) => Promise<Resul
const updatePlatformConfig: (
application_id: string,
type: string,
data: any
) => Promise<Result<any>> = (application_id, type, data) => {
return post(`/platform/${application_id}/${type}`, data)
data: any,
loading?: Ref<boolean>
) => Promise<Result<any>> = (application_id, type, data, loading) => {
return post(`/platform/${application_id}/${type}`, data, undefined, loading)
}
/**
* 更新平台状态
Expand Down
11 changes: 7 additions & 4 deletions ui/src/views/application/component/AccessSettingDrawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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('保存失败,请检查输入或稍后再试')
}
Expand Down

0 comments on commit 32e5c81

Please sign in to comment.