Skip to content

Commit

Permalink
add translate api
Browse files Browse the repository at this point in the history
  • Loading branch information
zheng.shen committed Dec 16, 2024
1 parent de925b1 commit eca3825
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
34 changes: 33 additions & 1 deletion seahub/ai/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)
8 changes: 8 additions & 0 deletions seahub/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion seahub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
]

0 comments on commit eca3825

Please sign in to comment.