Skip to content
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

fix: fix the defect of empty line to speech error #1898

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two small issues and one optimization suggestion in the provided code:

Issues:

  1. Missing return Statement: The function _remove_empty_lines is missing a return statement at the end. This should be added to properly return the cleaned string.

  2. Potential Memory Leak: The function currently uses list comprehensions to create new lists (full_text) and ' '.join(...). While this approach avoids modifying strings directly, it might not lead to efficient memory usage with very large datasets because each operation creates new objects.

Optimization Suggestion:

Instead of using list comprehensions for operations like filtering or joining strings, consider using generators when possible. Generators can reduce memory consumption since they yield results one at a time rather than creating intermediate lists.

def _remove_empty_lines(text):
    # Use generator expression to filter out empty lines
    non_empty_lines = (line for line in text.split('\n') if line.strip())
    return ''.join(non_empty_lines)

These changes will help ensure that the function behaves correctly and efficiently.

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)
}
/**
* 更新平台状态
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided function updatePlatformConfig has a couple of issues:

  1. Missing Parameters:

    • The signature for updatePlatformConfig does not accept the data parameter when it is followed by an optional loading parameter.
  2. Incorrect Argument Passing:

    • In the call to the post method within the implementation, there is no explicit value passed for the fourth argument (headers). It should be either undefined or explicitly set if needed.

Here's how you can fix these issues:

const updatePlatformConfig: (
  application_id: string,
  type: string,
  data: any,
  loading?: Ref<boolean>
) => Promise<Result<any>> = (application_id, type, data, loading?) => {
  // Ensure that headers remain consistent with the post request
  return post(`/platform/${application_id}/${type}`, data, undefined, loading);
}

By adding the missing parameters and ensuring that all necessary arguments are correctly passed, the code will now work as expected without errors. This modification maintains clarity and correctness in the function definition and usage.

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('保存失败,请检查输入或稍后再试')
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code looks generally correct. However, there is an improvement opportunity to manage the loading state more explicitly:

  1. Instead of using global loading variable, pass it as a parameter to the API call. This makes testing easier and keeps components decoupled from shared state.
applicationApi.updatePlatformConfig(
  id,
  configType.value,
  form[configType.value],
  loading // Passing the loading flag
).then(() => {
  MsgSuccess('配置保存成功')
  closeDrawer()
  emit('refresh')
}).catch((error) => {
  msgError(`保存失败,错误信息: ${error.message}`); // Handling errors separately
});

This change enhances reusability and maintainability by isolating the loading state within each component.

Expand Down
Loading