Skip to content

Commit

Permalink
Feat ai op tip (#7270)
Browse files Browse the repository at this point in the history
* feat: ai op tip

* feat: optimize code

---------

Co-authored-by: 杨国璇 <[email protected]>
  • Loading branch information
YangGuoXuan-0503 and 杨国璇 authored Dec 27, 2024
1 parent 58e9d14 commit 9d63b3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
19 changes: 15 additions & 4 deletions frontend/src/hooks/metadata-ai-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const MetadataAIOperationsProvider = ({
const canModify = useMemo(() => permission === 'rw', [permission]);

const OCRSuccessCallBack = useCallback(({ parentDir, fileName, ocrResult } = {}) => {
toaster.success(gettext('Successfully OCR'));
if (!ocrResult) return;
const update = { [PRIVATE_COLUMN_KEY.OCR]: ocrResult };
metadataAPI.modifyRecord(repoID, { parentDir, fileName }, update).then(res => {
Expand All @@ -32,12 +31,16 @@ export const MetadataAIOperationsProvider = ({

const onOCR = useCallback(({ parentDir, fileName }, { success_callback, fail_callback } = {}) => {
const filePath = Utils.joinPath(parentDir, fileName);
const inProgressToaster = toaster.notifyInProgress(gettext('Using AI to extract text, please wait...'), { duration: null });
metadataAPI.ocr(repoID, filePath).then(res => {
const ocrResult = res.data.ocr_result;
const validResult = Array.isArray(ocrResult) && ocrResult.length > 0 ? JSON.stringify(ocrResult) : null;
inProgressToaster.close();
toaster.success(gettext('AI to extract text, completed.'));
success_callback && success_callback({ parentDir, fileName, ocrResult: validResult });
}).catch(error => {
const errorMessage = gettext('OCR failed');
inProgressToaster.close();
const errorMessage = gettext('Failed to extract text');
toaster.danger(errorMessage);
fail_callback && fail_callback();
});
Expand All @@ -51,22 +54,30 @@ export const MetadataAIOperationsProvider = ({
APIName = isImage ? 'imageCaption' : 'generateDescription';
}
if (!APIName) return;

const descriptionTip = isImage ? gettext('image description') : gettext('description');
const inProgressToaster = toaster.notifyInProgress(gettext('Using AI to generate {description}, please wait...').replace('{description}', descriptionTip), { duration: null });
metadataAPI[APIName](repoID, filePath, lang).then(res => {
const description = res?.data?.summary || res.data.desc || '';
inProgressToaster.close();
toaster.success(gettext('AI to generate description, completed.').replace('{description}', descriptionTip));
success_callback && success_callback({ parentDir, fileName, description });
}).catch(error => {
const errorMessage = isImage ? gettext('Failed to generate image description') : gettext('Failed to generate description');
inProgressToaster.close();
const errorMessage = gettext('Failed to generate description').replace('{description}', descriptionTip);
toaster.danger(errorMessage);
fail_callback && fail_callback();
});
}, [repoID]);

const extractFilesDetails = useCallback((objIds, { success_callback, fail_callback } = {}) => {
const inProgressToaster = toaster.notifyInProgress(gettext('Using AI to extract file details, please wait...'), { duration: null });
metadataAPI.extractFileDetails(repoID, objIds).then(res => {
const details = res?.data?.details || [];
inProgressToaster.close();
toaster.success(gettext('AI to extract file details, completed.'));
success_callback && success_callback({ details });
}).catch(error => {
inProgressToaster.close();
const errorMessage = gettext('Failed to extract file details');
toaster.danger(errorMessage);
fail_callback && fail_callback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const NOT_DISPLAY_COLUMN_KEYS = [
PRIVATE_COLUMN_KEY.SIZE,
PRIVATE_COLUMN_KEY.SUFFIX,
PRIVATE_COLUMN_KEY.FILE_DETAILS,
PRIVATE_COLUMN_KEY.LOCATION,
PRIVATE_COLUMN_KEY.FACE_LINKS,
PRIVATE_COLUMN_KEY.EXCLUDED_FACE_LINKS,
PRIVATE_COLUMN_KEY.FACE_VECTORS,
Expand Down
7 changes: 2 additions & 5 deletions frontend/src/metadata/components/metadata-details/utils.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { getNormalizedColumnType } from '../../utils/column';
import { getCellValueByColumn } from '../../utils/cell';
import { NOT_DISPLAY_COLUMN_KEYS, CellType, PRIVATE_COLUMN_KEY } from './constants';

export const normalizeFields = (fields) => {
if (!Array.isArray(fields) || fields.length === 0) return [];
let validFields = fields.map((field) => {
return fields.map((field) => {
const { type, key, ...params } = field;
return {
...params,
key,
type: getNormalizedColumnType(key, type),
width: 200,
};
}).filter(field => !NOT_DISPLAY_COLUMN_KEYS.includes(field.key));
validFields.push({ key: PRIVATE_COLUMN_KEY.LOCATION, type: CellType.GEOLOCATION, width: 200 });
return validFields;
});
};

export {
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/metadata/hooks/metadata-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getCellValueByColumn, getColumnOptionNamesByIds, getColumnOptionNameByI
import tagsAPI from '../../tag/api';
import { getColumnByKey, getColumnOptions, getColumnOriginName } from '../utils/column';
import ObjectUtils from '../utils/object-utils';
import { NOT_DISPLAY_COLUMN_KEYS } from '../components/metadata-details/constants';

const MetadataDetailsContext = React.createContext(null);

Expand Down Expand Up @@ -56,7 +57,7 @@ export const MetadataDetailsProvider = ({ repoID, repoInfo, path, dirent, dirent
}, [record]);

const onChange = useCallback((fieldKey, newValue) => {
const field = getColumnByKey(originColumns, fieldKey);
const field = getColumnByKey(allColumnsRef.current, fieldKey);
const columnName = getColumnOriginName(field);
const recordId = getRecordIdFromRecord(record);
let update = { [columnName]: newValue };
Expand All @@ -74,7 +75,7 @@ export const MetadataDetailsProvider = ({ repoID, repoInfo, path, dirent, dirent
const errorMsg = Utils.getErrorMsg(error);
toaster.danger(errorMsg);
});
}, [repoID, record, originColumns]);
}, [repoID, record, allColumnsRef]);

const modifyColumnData = useCallback((fieldKey, newData) => {
let newColumns = originColumns.slice(0);
Expand Down Expand Up @@ -174,8 +175,9 @@ export const MetadataDetailsProvider = ({ repoID, repoInfo, path, dirent, dirent
metadataAPI.getRecord(repoID, { parentDir, fileName }).then(res => {
const { results, metadata } = res.data;
const record = Array.isArray(results) && results.length > 0 ? results[0] : {};
const columns = normalizeFields(metadata).map(field => new Column(field));
allColumnsRef.current = columns;
const allColumns = normalizeFields(metadata).map(field => new Column(field));
allColumnsRef.current = allColumns;
const columns = allColumns.filter(c => !NOT_DISPLAY_COLUMN_KEYS.includes(c.key));
setRecord(record);
setOriginColumns(columns);
setLoading(false);
Expand Down

0 comments on commit 9d63b3c

Please sign in to comment.