Skip to content

Commit

Permalink
fix: application i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
shaohuzhang1 committed Jan 23, 2025
1 parent 5ebcad7 commit 91d1d84
Show file tree
Hide file tree
Showing 5 changed files with 874 additions and 592 deletions.
46 changes: 25 additions & 21 deletions apps/application/serializers/chat_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.db import transaction, models
from django.db.models import QuerySet, Q
from django.http import StreamingHttpResponse
from django.utils.translation import gettext_lazy as _, gettext
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
from rest_framework import serializers
from rest_framework.utils.formatting import lazy_format
Expand All @@ -45,7 +46,6 @@
from setting.models import Model
from setting.models_provider import get_model_credential
from smartdoc.conf import PROJECT_DIR
from django.utils.translation import gettext_lazy as _, gettext as __

chat_cache = caches['chat_cache']

Expand Down Expand Up @@ -224,11 +224,13 @@ def stream_response():
worksheet = workbook.active
worksheet.title = 'Sheet1'

headers = [__('Conversation ID'), __('summary'), __('User Questions'), __('Problem after optimization'),
__('answer'), __('User feedback'),
__('Reference segment number'),
__('Section title + content'),
__('Annotation'), __('Consuming tokens'), __('Time consumed (s)'), __('Question Time')]
headers = [gettext('Conversation ID'), gettext('summary'), gettext('User Questions'),
gettext('Problem after optimization'),
gettext('answer'), gettext('User feedback'),
gettext('Reference segment number'),
gettext('Section title + content'),
gettext('Annotation'), gettext('Consuming tokens'), gettext('Time consumed (s)'),
gettext('Question Time')]
for col_idx, header in enumerate(headers, 1):
cell = worksheet.cell(row=1, column=col_idx)
cell.value = header
Expand Down Expand Up @@ -272,7 +274,7 @@ def is_valid(self, *, raise_exception=False):
user_id = self.data.get('user_id')
application_id = self.data.get('application_id')
if not QuerySet(Application).filter(id=application_id, user_id=user_id).exists():
raise AppApiException(500, __('Application does not exist'))
raise AppApiException(500, gettext('Application does not exist'))

def open(self):
self.is_valid(raise_exception=True)
Expand All @@ -291,7 +293,8 @@ def open_work_flow(self, application):
'-create_time')[0:1].first()
if work_flow_version is None:
raise AppApiException(500,
__("The application has not been published. Please use it after publishing."))
gettext(
"The application has not been published. Please use it after publishing."))
chat_cache.set(chat_id,
ChatInfo(chat_id, [],
[],
Expand Down Expand Up @@ -373,7 +376,7 @@ def get_user_id(self):
if 'id' in self.data and self.data.get('id') is not None:
application = QuerySet(Application).filter(id=self.data.get('id')).first()
if application is None:
raise AppApiException(500, __("Application does not exist"))
raise AppApiException(500, gettext("Application does not exist"))
return application.user_id
return self.data.get('user_id')

Expand Down Expand Up @@ -426,9 +429,9 @@ def is_valid(self, *, current_role=None, raise_exception=False):
application_access_token = QuerySet(ApplicationAccessToken).filter(
application_id=self.data.get('application_id')).first()
if application_access_token is None:
raise AppApiException(500, __('Application authentication information does not exist'))
raise AppApiException(500, gettext('Application authentication information does not exist'))
if not application_access_token.show_source and current_role == RoleConstants.APPLICATION_ACCESS_TOKEN.value:
raise AppApiException(500, __('Displaying knowledge sources is not enabled'))
raise AppApiException(500, gettext('Displaying knowledge sources is not enabled'))

def get_chat_record(self):
chat_record_id = self.data.get('chat_record_id')
Expand All @@ -446,7 +449,7 @@ def one(self, current_role: RoleConstants, with_valid=True):
self.is_valid(current_role=current_role, raise_exception=True)
chat_record = self.get_chat_record()
if chat_record is None:
raise AppApiException(500, __("Conversation does not exist"))
raise AppApiException(500, gettext("Conversation does not exist"))
return ChatRecordSerializer.Query.reset_chat_record(chat_record)

class Query(serializers.Serializer):
Expand Down Expand Up @@ -522,12 +525,13 @@ def vote(self, with_valid=True):
self.is_valid(raise_exception=True)
if not try_lock(self.data.get('chat_record_id')):
raise AppApiException(500,
__("Voting on the current session minutes, please do not send repeated requests"))
gettext(
"Voting on the current session minutes, please do not send repeated requests"))
try:
chat_record_details_model = QuerySet(ChatRecord).get(id=self.data.get('chat_record_id'),
chat_id=self.data.get('chat_id'))
if chat_record_details_model is None:
raise AppApiException(500, __("Non-existent conversation chat_record_id"))
raise AppApiException(500, gettext("Non-existent conversation chat_record_id"))
vote_status = self.data.get("vote_status")
if chat_record_details_model.vote_status == VoteChoices.UN_VOTE:
if vote_status == VoteChoices.STAR:
Expand All @@ -544,7 +548,7 @@ def vote(self, with_valid=True):
chat_record_details_model.vote_status = VoteChoices.UN_VOTE
chat_record_details_model.save()
else:
raise AppApiException(500, __("Already voted, please cancel first and then vote again"))
raise AppApiException(500, gettext("Already voted, please cancel first and then vote again"))
finally:
un_lock(self.data.get('chat_record_id'))
return True
Expand Down Expand Up @@ -575,7 +579,7 @@ def get(self, with_valid=True):
chat_id = self.data.get('chat_id')
chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_id).first()
if chat_record is None:
raise AppApiException(500, __('Conversation record does not exist'))
raise AppApiException(500, gettext('Conversation record does not exist'))
if chat_record.improve_paragraph_id_list is None or len(chat_record.improve_paragraph_id_list) == 0:
return []

Expand All @@ -602,7 +606,7 @@ def is_valid(self, *, raise_exception=False):
super().is_valid(raise_exception=True)
if not QuerySet(Document).filter(id=self.data.get('document_id'),
dataset_id=self.data.get('dataset_id')).exists():
raise AppApiException(500, __("The document id is incorrect"))
raise AppApiException(500, gettext("The document id is incorrect"))

@staticmethod
def post_embedding_paragraph(chat_record, paragraph_id, dataset_id):
Expand All @@ -621,7 +625,7 @@ def improve(self, instance: Dict, with_valid=True):
chat_id = self.data.get('chat_id')
chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_id).first()
if chat_record is None:
raise AppApiException(500, __('Conversation record does not exist'))
raise AppApiException(500, gettext('Conversation record does not exist'))

document_id = self.data.get("document_id")
dataset_id = self.data.get("dataset_id")
Expand Down Expand Up @@ -670,7 +674,7 @@ def delete(self, with_valid=True):
paragraph_id = self.data.get('paragraph_id')
chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_id).first()
if chat_record is None:
raise AppApiException(500, __('不存在的对话记录'))
raise AppApiException(500, gettext('Conversation record does not exist'))
if not chat_record.improve_paragraph_id_list.__contains__(uuid.UUID(paragraph_id)):
message = lazy_format(
_('The paragraph id is wrong. The current conversation record does not exist. [{paragraph_id}] paragraph id'),
Expand All @@ -693,7 +697,7 @@ class PostImprove(serializers.Serializer):
def is_valid(self, *, raise_exception=False):
super().is_valid(raise_exception=True)
if not Document.objects.filter(id=self.data['document_id'], dataset_id=self.data['dataset_id']).exists():
raise AppApiException(500, __("The document id is incorrect"))
raise AppApiException(500, gettext("The document id is incorrect"))

@staticmethod
def post_embedding_paragraph(paragraph_ids, dataset_id):
Expand All @@ -712,7 +716,7 @@ def post_improve(self, instance: Dict):
# 获取所有聊天记录
chat_record_list = list(ChatRecord.objects.filter(chat_id__in=chat_ids))
if len(chat_record_list) < len(chat_ids):
raise AppApiException(500, __("Conversation records that do not exist"))
raise AppApiException(500, gettext("Conversation records that do not exist"))

# 批量创建段落和问题映射
paragraphs = []
Expand Down
6 changes: 3 additions & 3 deletions apps/common/util/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db.models import QuerySet
from django.utils.translation import gettext as __
from django.utils.translation import gettext as _
from pydub import AudioSegment

from ..exception.app_exception import AppApiException
Expand Down Expand Up @@ -216,9 +216,9 @@ def split_and_transcribe(file_path, model, max_segment_length_ms=59000, audio_fo

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

Expand Down
Loading

0 comments on commit 91d1d84

Please sign in to comment.