Skip to content

Commit

Permalink
generate doc tags (#7182)
Browse files Browse the repository at this point in the history
* generate doc tags

* update

---------

Co-authored-by: zheng.shen <[email protected]>
  • Loading branch information
shenzheng-1 and zheng.shen authored Dec 13, 2024
1 parent d7f1126 commit 977aa79
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 42 deletions.
4 changes: 2 additions & 2 deletions frontend/src/metadata/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ class MetadataManagerAPI {
return this.req.post(url, params);
};

imageTags = (repoID, filePath) => {
const url = this.server + '/api/v2.1/ai/image-tags/';
generateFileTags = (repoID, filePath) => {
const url = this.server + '/api/v2.1/ai/generate-file-tags/';
const params = {
path: filePath,
repo_id: repoID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import { useTags } from '../../../../tag/hooks';

import './index.css';

const ImageTagsDialog = ({ record, onToggle, onSubmit }) => {
const FileTagsDialog = ({ record, onToggle, onSubmit }) => {

const [isLoading, setLoading] = useState(true);
const [isSubmitting, setSubmitting] = useState(false);
const [imageTags, setImageTags] = useState([]);
const [fileTags, setFileTags] = useState([]);
const [selectedTags, setSelectedTags] = useState([]);

const fileName = useMemo(() => getFileNameFromRecord(record), [record]);
Expand All @@ -28,20 +28,20 @@ const ImageTagsDialog = ({ record, onToggle, onSubmit }) => {

useEffect(() => {
let path = '';
if (Utils.imageCheck(fileName) && window.sfMetadataContext.canModifyRow(record)) {
if (window.sfMetadataContext.canModifyRow(record)) {
const parentDir = getParentDirFromRecord(record);
path = Utils.joinPath(parentDir, fileName);
}
if (path === '') {
setLoading(false);
return;
}
window.sfMetadataContext.imageTags(path).then(res => {
const tags = res.data.tags;
setImageTags(tags);
window.sfMetadataContext.generateFileTags(path).then(res => {
const tags = res.data.tags || [];
setFileTags(tags);
setLoading(false);
}).catch(error => {
const errorMessage = gettext('Failed to generate image tags');
const errorMessage = gettext('Failed to generate file tags');
toaster.danger(errorMessage);
setLoading(false);
});
Expand Down Expand Up @@ -124,9 +124,9 @@ const ImageTagsDialog = ({ record, onToggle, onSubmit }) => {
<CenteredLoading />
) : (
<div className="auto-image-tags-container">
{imageTags.length > 0 ? (
{fileTags.length > 0 ? (
<>
{imageTags.map((tagName, index) => {
{fileTags.map((tagName, index) => {
const isSelected = selectedTags.includes(tagName);
return (
<div
Expand All @@ -153,10 +153,10 @@ const ImageTagsDialog = ({ record, onToggle, onSubmit }) => {
);
};

ImageTagsDialog.propTypes = {
FileTagsDialog.propTypes = {
record: PropTypes.object,
onToggle: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
};

export default ImageTagsDialog;
export default FileTagsDialog;
4 changes: 2 additions & 2 deletions frontend/src/metadata/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ class Context {
return this.metadataAPI.imageCaption(repoID, filePath, lang);
};

imageTags = (filePath) => {
generateFileTags = (filePath) => {
const repoID = this.settings['repoID'];
return this.metadataAPI.imageTags(repoID, filePath);
return this.metadataAPI.generateFileTags(repoID, filePath);
};

extractFileDetails = (objIds) => {
Expand Down
24 changes: 12 additions & 12 deletions frontend/src/metadata/views/table/context-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EVENT_BUS_TYPE, EVENT_BUS_TYPE as METADATA_EVENT_BUS_TYPE, PRIVATE_COLU
import { getFileNameFromRecord, getParentDirFromRecord, getFileObjIdFromRecord,
getRecordIdFromRecord,
} from '../../../utils/cell';
import ImageTagsDialog from '../../../components/dialog/image-tags-dialog';
import FileTagsDialog from '../../../components/dialog/file-tags-dialog';

import './index.css';

Expand All @@ -21,7 +21,7 @@ const OPERATION = {
OPEN_IN_NEW_TAB: 'open-new-tab',
GENERATE_DESCRIPTION: 'generate-description',
IMAGE_CAPTION: 'image-caption',
IMAGE_TAGS: 'image-tags',
FILE_TAGS: 'file-tags',
DELETE_RECORD: 'delete-record',
DELETE_RECORDS: 'delete-records',
RENAME_FILE: 'rename-file',
Expand All @@ -37,7 +37,7 @@ const ContextMenu = (props) => {
const menuRef = useRef(null);
const [visible, setVisible] = useState(false);
const [position, setPosition] = useState({ top: 0, left: 0 });
const [imageTagsRecord, setImageTagsRecord] = useState(null);
const [fileTagsRecord, setFileTagsRecord] = useState(null);

const { metadata } = useMetadataView();

Expand Down Expand Up @@ -143,8 +143,8 @@ const ContextMenu = (props) => {
list.push({ value: OPERATION.FILE_DETAIL, label: gettext('Extract file detail'), record: record });
}

if (tagsColumn && canModifyRow && Utils.imageCheck(fileName)) {
list.push({ value: OPERATION.IMAGE_TAGS, label: gettext('Generate image tags'), record: record });
if (tagsColumn && canModifyRow && (Utils.imageCheck(fileName) || checkIsDescribableDoc(record))) {
list.push({ value: OPERATION.FILE_TAGS, label: gettext('Generate file tags'), record: record });
}

// handle delete folder/file
Expand Down Expand Up @@ -252,8 +252,8 @@ const ContextMenu = (props) => {
});
}, [updateRecords]);

const toggleImageTagsRecord = useCallback((record = null) => {
setImageTagsRecord(record);
const toggleFileTagsRecord = useCallback((record = null) => {
setFileTagsRecord(record);
}, []);

const updateFileDetails = useCallback((records) => {
Expand Down Expand Up @@ -325,10 +325,10 @@ const ContextMenu = (props) => {
imageCaption(record);
break;
}
case OPERATION.IMAGE_TAGS: {
case OPERATION.FILE_TAGS: {
const { record } = option;
if (!record) break;
toggleImageTagsRecord(record);
toggleFileTagsRecord(record);
break;
}
case OPERATION.DELETE_RECORD: {
Expand Down Expand Up @@ -374,7 +374,7 @@ const ContextMenu = (props) => {
}
}
setVisible(false);
}, [onOpenFileInNewTab, onOpenParentFolder, onCopySelected, onClearSelected, generateDescription, imageCaption, deleteRecords, toggleDeleteFolderDialog, selectNone, updateFileDetails, toggleImageTagsRecord]);
}, [onOpenFileInNewTab, onOpenParentFolder, onCopySelected, onClearSelected, generateDescription, imageCaption, deleteRecords, toggleDeleteFolderDialog, selectNone, updateFileDetails, toggleFileTagsRecord]);

const getMenuPosition = useCallback((x = 0, y = 0) => {
let menuStyles = {
Expand Down Expand Up @@ -461,8 +461,8 @@ const ContextMenu = (props) => {
return (
<>
{renderMenu()}
{imageTagsRecord && (
<ImageTagsDialog record={imageTagsRecord} onToggle={toggleImageTagsRecord} onSubmit={updateFileTags} />
{fileTagsRecord && (
<FileTagsDialog record={fileTagsRecord} onToggle={toggleFileTagsRecord} onSubmit={updateFileTags} />
)}
</>

Expand Down
36 changes: 25 additions & 11 deletions 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, image_tags
from seahub.ai.utils import image_caption, verify_ai_config, generate_summary, generate_file_tags

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -140,7 +140,7 @@ def post(self, request):
return Response(resp_json, resp.status_code)


class ImageTags(APIView):
class GenerateFileTags(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated,)
throttle_classes = (UserRateThrottle,)
Expand All @@ -162,13 +162,6 @@ def post(self, request):
error_msg = 'Library %s not found.' % repo_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

try:
record = RepoMetadata.objects.filter(repo_id=repo_id).first()
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

permission = check_folder_permission(request, repo_id, os.path.dirname(path))
if not permission:
error_msg = 'Permission denied.'
Expand All @@ -191,11 +184,32 @@ def post(self, request):
params = {
'path': path,
'download_token': token,
'lang': record.tags_lang if record and record.tags_enabled else None
}

file_type, _ = get_file_type_and_ext(os.path.basename(path))
if file_type == IMAGE:
try:
record = RepoMetadata.objects.filter(repo_id=repo_id).first()
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

params['file_type'] = 'image'
params['lang'] = record.tags_lang if record and record.tags_enabled else None
else:
from seahub.repo_metadata.metadata_server_api import MetadataServerAPI
from seafevents.repo_metadata.constants import TAGS_TABLE
metadata_server_api = MetadataServerAPI(repo_id, request.user.username)

sql = f'SELECT `{TAGS_TABLE.columns.name.name}` FROM `{TAGS_TABLE.name}`'
query_result = metadata_server_api.query_rows(sql).get('results', [])

params['file_type'] = 'doc'
params['candidate_tags'] = [item[TAGS_TABLE.columns.name.name].strip() for item in query_result]

try:
resp = image_tags(params)
resp = generate_file_tags(params)
resp_json = resp.json()
except Exception as e:
error_msg = 'Internal Server Error'
Expand Down
4 changes: 2 additions & 2 deletions seahub/ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def generate_summary(params):
return resp


def image_tags(params):
def generate_file_tags(params):
headers = gen_headers()
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/image-tags/')
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/generate-file-tags/')
resp = requests.post(url, json=params, headers=headers, timeout=30)
return resp
4 changes: 2 additions & 2 deletions 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, ImageTags
from seahub.ai.apis import ImageCaption, GenerateSummary, GenerateFileTags
from seahub.api2.endpoints.share_link_auth import ShareLinkUserAuthView, ShareLinkEmailAuthView
from seahub.api2.endpoints.internal_api import InternalUserListView, InternalCheckShareLinkAccess, \
InternalCheckFileOperationAccess
Expand Down Expand Up @@ -1046,6 +1046,6 @@
# ai API
urlpatterns += [
re_path(r'^api/v2.1/ai/image-caption/$', ImageCaption.as_view(), name='api-v2.1-image-caption'),
re_path(r'^api/v2.1/ai/image-tags/$', ImageTags.as_view(), name='api-v2.1-image-tags'),
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'),
]

0 comments on commit 977aa79

Please sign in to comment.