diff --git a/seahub/ai/apis.py b/seahub/ai/apis.py index 654f7380bc2..f8b702d2da3 100644 --- a/seahub/ai/apis.py +++ b/seahub/ai/apis.py @@ -15,7 +15,7 @@ from seahub.api2.authentication import TokenAuthentication from seahub.utils import get_file_type_and_ext, IMAGE from seahub.views import check_folder_permission -from seahub.ai.utils import image_caption, verify_ai_config, generate_summary, generate_file_tags, ocr +from seahub.ai.utils import image_caption, translate, verify_ai_config, generate_summary, generate_file_tags, ocr logger = logging.getLogger(__name__) @@ -279,3 +279,35 @@ def post(self, request): return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response(resp_json, resp.status_code) + + +class Translate(APIView): + # authentication_classes = (TokenAuthentication, SessionAuthentication) + # permission_classes = (IsAuthenticated,) + # throttle_classes = (UserRateThrottle,) + + def post(self, request): + if not verify_ai_config(): + return api_error(status.HTTP_400_BAD_REQUEST, 'AI server not configured') + + text = request.data.get('text') + lang = request.data.get('lang') + + if not text: + return api_error(status.HTTP_400_BAD_REQUEST, 'text invalid') + if not lang: + return api_error(status.HTTP_400_BAD_REQUEST, 'lang invalid') + + params = { + 'text': text, + 'lang': lang, + } + + try: + resp = translate(params) + resp_json = resp.json() + except Exception as e: + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + + return Response(resp_json, resp.status_code) diff --git a/seahub/ai/utils.py b/seahub/ai/utils.py index 6f1f79ad914..b90db4072c5 100644 --- a/seahub/ai/utils.py +++ b/seahub/ai/utils.py @@ -41,8 +41,16 @@ def generate_file_tags(params): resp = requests.post(url, json=params, headers=headers, timeout=30) return resp + def ocr(params): headers = gen_headers() url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/ocr/') resp = requests.post(url, json=params, headers=headers, timeout=30) return resp + + +def translate(params): + headers = gen_headers() + url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/translate/') + resp = requests.post(url, json=params, headers=headers, timeout=30) + return resp diff --git a/seahub/urls.py b/seahub/urls.py index 4ed758dab31..3b8e86711b4 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -2,7 +2,7 @@ from django.urls import include, path, re_path from django.views.generic import TemplateView -from seahub.ai.apis import ImageCaption, GenerateSummary, GenerateFileTags, OCR +from seahub.ai.apis import ImageCaption, GenerateSummary, GenerateFileTags, OCR, Translate from seahub.api2.endpoints.share_link_auth import ShareLinkUserAuthView, ShareLinkEmailAuthView from seahub.api2.endpoints.internal_api import InternalUserListView, InternalCheckShareLinkAccess, \ InternalCheckFileOperationAccess @@ -1049,4 +1049,5 @@ re_path(r'^api/v2.1/ai/generate-file-tags/$', GenerateFileTags.as_view(), name='api-v2.1-generate-file-tags'), re_path(r'^api/v2.1/ai/generate-summary/$', GenerateSummary.as_view(), name='api-v2.1-generate-summary'), re_path(r'^api/v2.1/ai/ocr/$', OCR.as_view(), name='api-v2.1-ocr'), + re_path(r'^api/v2.1/ai/translate/$', Translate.as_view(), name='api-v2.1-translate'), ]