Skip to content

Commit d9c6b6b

Browse files
authored
fix: model i18n error (#2055)
1 parent 1389c58 commit d9c6b6b

File tree

74 files changed

+612
-404
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+612
-404
lines changed

apps/common/util/common.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
import hashlib
1010
import importlib
1111
import io
12+
import mimetypes
1213
import re
1314
import shutil
14-
import mimetypes
1515
from functools import reduce
1616
from typing import Dict, List
1717

1818
from django.core.files.uploadedfile import InMemoryUploadedFile
1919
from django.db.models import QuerySet
20+
from django.utils.translation import gettext as __
2021
from pydub import AudioSegment
2122

2223
from ..exception.app_exception import AppApiException
2324
from ..models.db_model_manage import DBModelManage
24-
from django.utils.translation import gettext_lazy as _
25+
2526

2627
def sub_array(array: List, item_num=10):
2728
result = []
@@ -215,9 +216,9 @@ def split_and_transcribe(file_path, model, max_segment_length_ms=59000, audio_fo
215216

216217
def _remove_empty_lines(text):
217218
if not isinstance(text, str):
218-
raise AppApiException(500, _('Text-to-speech node, the text content must be of string type'))
219+
raise AppApiException(500, __('Text-to-speech node, the text content must be of string type'))
219220
if not text:
220-
raise AppApiException(500, _('Text-to-speech node, the text content cannot be empty'))
221+
raise AppApiException(500, __('Text-to-speech node, the text content cannot be empty'))
221222
result = '\n'.join(line for line in text.split('\n') if line.strip())
222223
return markdown_to_plain_text(result)
223224

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/embedding.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"""
99
from typing import Dict
1010

11+
from django.utils.translation import gettext as __
12+
1113
from common import forms
1214
from common.exception.app_exception import AppApiException
1315
from common.forms import BaseForm
1416
from setting.models_provider.base_model_provider import ValidCode, BaseModelCredential
1517
from setting.models_provider.impl.aliyun_bai_lian_model_provider.model.embedding import AliyunBaiLianEmbedding
16-
from django.utils.translation import gettext_lazy as _
1718

1819

1920
class AliyunBaiLianEmbeddingCredential(BaseForm, BaseModelCredential):
@@ -23,21 +24,23 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
2324
model_type_list = provider.get_model_type_list()
2425
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
2526
raise AppApiException(ValidCode.valid_error.value,
26-
_('{model_type} Model type is not supported').format(model_type=model_type))
27+
__('{model_type} Model type is not supported').format(model_type=model_type))
2728
for key in ['dashscope_api_key']:
2829
if key not in model_credential:
2930
if raise_exception:
30-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
31+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
3132
else:
3233
return False
3334
try:
3435
model: AliyunBaiLianEmbedding = provider.get_model(model_type, model_name, model_credential)
35-
model.embed_query(_('Hello'))
36+
model.embed_query(__('Hello'))
3637
except Exception as e:
3738
if isinstance(e, AppApiException):
3839
raise e
3940
if raise_exception:
40-
raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e)))
41+
raise AppApiException(ValidCode.valid_error.value,
42+
__('Verification failed, please check whether the parameters are correct: {error}').format(
43+
error=str(e)))
4144
else:
4245
return False
4346
return True

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/image.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
@date:2024/7/11 18:41
77
@desc:
88
"""
9-
import base64
10-
import os
119
from typing import Dict
1210

11+
from django.utils.translation import gettext_lazy as _, gettext as __
1312
from langchain_core.messages import HumanMessage
1413

1514
from common import forms
1615
from common.exception.app_exception import AppApiException
1716
from common.forms import BaseForm, TooltipLabel
1817
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
19-
from django.utils.translation import gettext_lazy as _
2018

2119

2220
class QwenModelParams(BaseForm):
@@ -45,11 +43,11 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
4543
model_type_list = provider.get_model_type_list()
4644
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
4745
raise AppApiException(ValidCode.valid_error.value,
48-
_('{model_type} Model type is not supported').format(model_type=model_type))
46+
__('{model_type} Model type is not supported').format(model_type=model_type))
4947
for key in ['api_key']:
5048
if key not in model_credential:
5149
if raise_exception:
52-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
50+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
5351
else:
5452
return False
5553
try:
@@ -62,7 +60,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
6260
raise e
6361
if raise_exception:
6462
raise AppApiException(ValidCode.valid_error.value,
65-
_('Verification failed, please check whether the parameters are correct: {error}').format(
63+
__('Verification failed, please check whether the parameters are correct: {error}').format(
6664
error=str(e)))
6765
else:
6866
return False

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from common.exception.app_exception import AppApiException
88
from common.forms import BaseForm, TooltipLabel
99
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
10-
from django.utils.translation import gettext_lazy as _
10+
from django.utils.translation import gettext_lazy as _, gettext as __
1111

1212

1313
class BaiLianLLMModelParams(BaseForm):
@@ -36,23 +36,23 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
3636
model_type_list = provider.get_model_type_list()
3737
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
3838
raise AppApiException(ValidCode.valid_error.value,
39-
_('{model_type} Model type is not supported').format(model_type=model_type))
39+
__('{model_type} Model type is not supported').format(model_type=model_type))
4040

4141
for key in ['api_base', 'api_key']:
4242
if key not in model_credential:
4343
if raise_exception:
44-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
44+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
4545
else:
4646
return False
4747
try:
4848
model = provider.get_model(model_type, model_name, model_credential, **model_params)
49-
model.invoke([HumanMessage(content=_('Hello'))])
49+
model.invoke([HumanMessage(content=__('Hello'))])
5050
except Exception as e:
5151
if isinstance(e, AppApiException):
5252
raise e
5353
if raise_exception:
5454
raise AppApiException(ValidCode.valid_error.value,
55-
_('Verification failed, please check whether the parameters are correct: {error}').format(
55+
__('Verification failed, please check whether the parameters are correct: {error}').format(
5656
error=str(e)))
5757
else:
5858
return False

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
"""
99
from typing import Dict
1010

11+
from django.utils.translation import gettext as __
1112
from langchain_core.documents import Document
1213

1314
from common import forms
1415
from common.exception.app_exception import AppApiException
1516
from common.forms import BaseForm
1617
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
1718
from setting.models_provider.impl.aliyun_bai_lian_model_provider.model.reranker import AliyunBaiLianReranker
18-
from django.utils.translation import gettext_lazy as _
1919

2020

2121
class AliyunBaiLianRerankerCredential(BaseForm, BaseModelCredential):
@@ -24,22 +24,22 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
2424
raise_exception=False):
2525
if not model_type == 'RERANKER':
2626
raise AppApiException(ValidCode.valid_error.value,
27-
_('{model_type} Model type is not supported').format(model_type=model_type))
27+
__('{model_type} Model type is not supported').format(model_type=model_type))
2828
for key in ['dashscope_api_key']:
2929
if key not in model_credential:
3030
if raise_exception:
31-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
31+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
3232
else:
3333
return False
3434
try:
3535
model: AliyunBaiLianReranker = provider.get_model(model_type, model_name, model_credential)
36-
model.compress_documents([Document(page_content=_('Hello'))], _('Hello'))
36+
model.compress_documents([Document(page_content=__('Hello'))], __('Hello'))
3737
except Exception as e:
3838
if isinstance(e, AppApiException):
3939
raise e
4040
if raise_exception:
4141
raise AppApiException(ValidCode.valid_error.value,
42-
_('Verification failed, please check whether the parameters are correct: {error}').format(
42+
__('Verification failed, please check whether the parameters are correct: {error}').format(
4343
error=str(e)))
4444
else:
4545
return False

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/stt.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from typing import Dict
44

5+
from django.utils.translation import gettext as __
6+
57
from common import forms
68
from common.exception.app_exception import AppApiException
79
from common.forms import BaseForm
810
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
9-
from django.utils.translation import gettext_lazy as _
1011

1112

1213
class AliyunBaiLianSTTModelCredential(BaseForm, BaseModelCredential):
@@ -17,12 +18,12 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
1718
model_type_list = provider.get_model_type_list()
1819
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
1920
raise AppApiException(ValidCode.valid_error.value,
20-
_('{model_type} Model type is not supported').format(model_type=model_type))
21+
__('{model_type} Model type is not supported').format(model_type=model_type))
2122

2223
for key in ['api_key']:
2324
if key not in model_credential:
2425
if raise_exception:
25-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
26+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
2627
else:
2728
return False
2829
try:
@@ -33,7 +34,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
3334
raise e
3435
if raise_exception:
3536
raise AppApiException(ValidCode.valid_error.value,
36-
_('Verification failed, please check whether the parameters are correct: {error}').format(
37+
__('Verification failed, please check whether the parameters are correct: {error}').format(
3738
error=str(e)))
3839
else:
3940
return False

apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tti.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
"""
99
from typing import Dict
1010

11+
from django.utils.translation import gettext_lazy as _, gettext as __
12+
1113
from common import forms
1214
from common.exception.app_exception import AppApiException
1315
from common.forms import BaseForm, TooltipLabel
1416
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
15-
from django.utils.translation import gettext_lazy as _
1617

1718

1819
class QwenModelParams(BaseForm):
@@ -63,11 +64,11 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
6364
model_type_list = provider.get_model_type_list()
6465
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
6566
raise AppApiException(ValidCode.valid_error.value,
66-
_('{model_type} Model type is not supported').format(model_type=model_type))
67+
__('{model_type} Model type is not supported').format(model_type=model_type))
6768
for key in ['api_key']:
6869
if key not in model_credential:
6970
if raise_exception:
70-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
71+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
7172
else:
7273
return False
7374
try:
@@ -79,7 +80,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
7980
raise e
8081
if raise_exception:
8182
raise AppApiException(ValidCode.valid_error.value,
82-
_('Verification failed, please check whether the parameters are correct: {error}').format(
83+
__('Verification failed, please check whether the parameters are correct: {error}').format(
8384
error=str(e)))
8485
else:
8586
return False

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from common.exception.app_exception import AppApiException
77
from common.forms import BaseForm, TooltipLabel
88
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
9-
from django.utils.translation import gettext_lazy as _
9+
from django.utils.translation import gettext_lazy as _, gettext as __
1010

1111

1212
class AliyunBaiLianTTSModelGeneralParams(BaseForm):
@@ -51,12 +51,12 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
5151
model_type_list = provider.get_model_type_list()
5252
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
5353
raise AppApiException(ValidCode.valid_error.value,
54-
_('{model_type} Model type is not supported').format(model_type=model_type))
54+
__('{model_type} Model type is not supported').format(model_type=model_type))
5555

5656
for key in ['api_key']:
5757
if key not in model_credential:
5858
if raise_exception:
59-
raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
59+
raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key))
6060
else:
6161
return False
6262
try:
@@ -67,7 +67,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
6767
raise e
6868
if raise_exception:
6969
raise AppApiException(ValidCode.valid_error.value,
70-
_('Verification failed, please check whether the parameters are correct: {error}').format(
70+
__('Verification failed, please check whether the parameters are correct: {error}').format(
7171
error=str(e)))
7272
else:
7373
return False

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from typing import Dict
44

55
from dashscope import ImageSynthesis
6+
from django.utils.translation import gettext as __
67
from langchain_community.chat_models import ChatTongyi
78
from langchain_core.messages import HumanMessage
89

910
from setting.models_provider.base_model_provider import MaxKBBaseModel
1011
from setting.models_provider.impl.base_tti import BaseTextToImage
11-
from django.utils.translation import gettext_lazy as _
12+
1213

1314
class QwenTextToImageModel(MaxKBBaseModel, BaseTextToImage):
1415
api_key: str
@@ -39,7 +40,7 @@ def is_cache_model(self):
3940

4041
def check_auth(self):
4142
chat = ChatTongyi(api_key=self.api_key, model_name='qwen-max')
42-
chat.invoke([HumanMessage([{"type": "text", "text": _('Hello')}])])
43+
chat.invoke([HumanMessage([{"type": "text", "text": __('Hello')}])])
4344

4445
def generate_image(self, prompt: str, negative_prompt: str = None):
4546
# api_base='https://dashscope.aliyuncs.com/compatible-mode/v1',

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import dashscope
44
from dashscope.audio.tts_v2 import *
5+
from django.utils.translation import gettext as __
56

67
from common.util.common import _remove_empty_lines
78
from setting.models_provider.base_model_provider import MaxKBBaseModel
89
from setting.models_provider.impl.base_tts import BaseTextToSpeech
9-
from django.utils.translation import gettext_lazy as _
10+
1011

1112
class AliyunBaiLianTextToSpeech(MaxKBBaseModel, BaseTextToSpeech):
1213
api_key: str
@@ -33,7 +34,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
3334
)
3435

3536
def check_auth(self):
36-
self.text_to_speech(_('Hello'))
37+
self.text_to_speech(__('Hello'))
3738

3839
def text_to_speech(self, text):
3940
dashscope.api_key = self.api_key

0 commit comments

Comments
 (0)