Skip to content

Commit

Permalink
seafile_ocr
Browse files Browse the repository at this point in the history
  • Loading branch information
zheng.shen authored and 杨国璇 committed Aug 20, 2024
1 parent 10d1e4d commit a133260
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
47 changes: 46 additions & 1 deletion seahub/ai/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from seahub.utils.repo import is_valid_repo_id_format, is_repo_admin
from seahub.ai.utils import search, get_file_download_token, get_search_repos, \
RELATED_REPOS_PREFIX, RELATED_REPOS_CACHE_TIMEOUT, SEARCH_REPOS_LIMIT, \
format_repos
format_repos, ocr
from seahub.utils import is_org_context, normalize_cache_key, HAS_FILE_SEASEARCH
from seahub.views import check_folder_permission

Expand Down Expand Up @@ -121,3 +121,48 @@ def post(self, request):
f['fullpath'] = f['fullpath'].split(origin_path)[-1]

return Response(resp_json, resp.status_code)


class OCR(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated, )
throttle_classes = (UserRateThrottle, )

def post(self, request):
repo_id = request.data.get('repo_id')
path = request.data.get('path')

if not repo_id:
return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')
if not path:
return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid')

repo = seafile_api.get_repo(repo_id)
if not repo:
error_msg = 'Library %s not found.' % repo_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

file_obj = seafile_api.get_dirent_by_path(repo_id, path)
if not file_obj:
error_msg = 'File %s not found.' % path
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

# permission check
if not check_folder_permission(request, repo_id, path):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

params = {
'repo_id': repo_id,
'path': path,
'obj_id': file_obj.obj_id,
}

try:
ocr_result = ocr(params)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

return Response({'ocr_result': ocr_result}, status.HTTP_200_OK)
15 changes: 14 additions & 1 deletion seahub/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from urllib.parse import urljoin

from seahub.settings import SECRET_KEY, SEAFEVENTS_SERVER_URL
from seahub.settings import SECRET_KEY, SEAFEVENTS_SERVER_URL, SEAFILE_AI_SECRET_KEY, SEAFILE_AI_SERVER_URL
from seahub.utils import get_user_repos

from seaserv import seafile_api
Expand All @@ -18,6 +18,12 @@
RELATED_REPOS_CACHE_TIMEOUT = 2 * 60 * 60


def gen_headers():
payload = {'exp': int(time.time()) + 300, }
token = jwt.encode(payload, SEAFILE_AI_SECRET_KEY, algorithm='HS256')
return {"Authorization": "Token %s" % token}


def search(params):
payload = {'exp': int(time.time()) + 300, }
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
Expand Down Expand Up @@ -65,3 +71,10 @@ def format_repos(repos):
continue
repos_map[real_repo_id] = (real_repo_id, origin_path, repo_name)
return searched_repos, repos_map


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
7 changes: 7 additions & 0 deletions seahub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,13 @@ def genpassword():
ENABLE_GLOBAL_ADDRESSBOOK = True
ENABLE_ADDRESSBOOK_OPT_IN = False

#####################
# Seafile AI #
#####################
SEAFILE_AI_SERVER_URL = ''
SEAFILE_AI_SECRET_KEY = ''
ENABLE_SEAFILE_AI = False

####################
# Guest Invite #
####################
Expand Down
7 changes: 6 additions & 1 deletion seahub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
from seahub.seadoc.views import sdoc_revision, sdoc_revisions, sdoc_to_docx
from seahub.ocm.settings import OCM_ENDPOINT

from seahub.ai.apis import Search
from seahub.ai.apis import Search, OCR
from seahub.wiki2.views import wiki_view
from seahub.api2.endpoints.wiki2 import Wikis2View, Wiki2View, Wiki2ConfigView, Wiki2PagesView, Wiki2PageView, \
Wiki2DuplicatePageView, WikiPageTrashView
Expand Down Expand Up @@ -1044,3 +1044,8 @@
re_path(r'^api/v2.1/repos/(?P<repo_id>[-0-9a-f]{36})/metadata/move-views/$', MetadataViewsMoveView.as_view(), name='api-v2.1-metadata-views-move'),

]

if settings.ENABLE_SEAFILE_AI:
urlpatterns += [
re_path(r'^api/v2.1/ai/ocr/$', OCR.as_view(), name='api-v2.1-ai-ocr'),
]

0 comments on commit a133260

Please sign in to comment.