mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-15 06:44:16 +00:00
image_caption (#6657)
* image_caption * update * update * update * update * update --------- Co-authored-by: zheng.shen <zheng.shen@seafile.com>
This commit is contained in:
@@ -194,10 +194,21 @@ class MetadataManagerAPI {
|
||||
};
|
||||
|
||||
// ai
|
||||
generateSummary = (repoID, filePaths) => {
|
||||
const url = this.server + '/api/v2.1/repos/' + repoID + '/metadata/ai/summarize-documents/';
|
||||
generateSummary = (repoID, filePath) => {
|
||||
const url = this.server + '/api/v2.1/ai/generate-summary/';
|
||||
const params = {
|
||||
file_paths_list: filePaths,
|
||||
path: filePath,
|
||||
repo_id: repoID,
|
||||
};
|
||||
return this.req.post(url, params);
|
||||
};
|
||||
|
||||
imageCaption = (repoID, filePath, lang) => {
|
||||
const url = this.server + '/api/v2.1/ai/image-caption/';
|
||||
const params = {
|
||||
path: filePath,
|
||||
repo_id: repoID,
|
||||
lang: lang,
|
||||
};
|
||||
return this.req.post(url, params);
|
||||
};
|
||||
|
@@ -15,6 +15,7 @@ const OPERATION = {
|
||||
OPEN_PARENT_FOLDER: 'open-parent-folder',
|
||||
OPEN_IN_NEW_TAB: 'open-new-tab',
|
||||
GENERATE_SUMMARY: 'generate-summary',
|
||||
IMAGE_CAPTION: 'image-caption',
|
||||
};
|
||||
|
||||
const ContextMenu = ({
|
||||
@@ -90,6 +91,8 @@ const ContextMenu = ({
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (Utils.isSdocFile(fileName) && canModifyRow(record)) {
|
||||
list.push({ value: OPERATION.GENERATE_SUMMARY, label: gettext('Generate summary') });
|
||||
} else if (Utils.imageCheck(fileName) && canModifyRow(record)) {
|
||||
list.push({ value: OPERATION.IMAGE_CAPTION, label: gettext('Generate image description') });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,63 +137,65 @@ const ContextMenu = ({
|
||||
|
||||
const generateSummary = useCallback(() => {
|
||||
const canModifyRow = window.sfMetadataContext.canModifyRow;
|
||||
const selectedRecords = Object.keys(recordMetrics.idSelectedRecordMap);
|
||||
const summaryColumnKey = PRIVATE_COLUMN_KEY.FILE_SUMMARY;
|
||||
let paths = [];
|
||||
let path = '';
|
||||
let idOldRecordData = {};
|
||||
let idOriginalOldRecordData = {};
|
||||
if (selectedRange) {
|
||||
const { topLeft, bottomRight } = selectedRange;
|
||||
for (let i = topLeft.rowIdx; i <= bottomRight.rowIdx; i++) {
|
||||
const record = recordGetterByIndex({ isGroupView, groupRecordIndex: topLeft.groupRecordIndex, recordIndex: i });
|
||||
if (!canModifyRow(record)) continue;
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (!Utils.isSdocFile(fileName)) continue;
|
||||
const parentDir = record[PRIVATE_COLUMN_KEY.PARENT_DIR];
|
||||
paths.push(Utils.joinPath(parentDir, fileName));
|
||||
idOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
idOriginalOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
}
|
||||
} else if (selectedRecords.length > 0) {
|
||||
selectedRecords.forEach(recordId => {
|
||||
const record = metadata.id_row_map[recordId];
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (Utils.isSdocFile(fileName) && canModifyRow(record)) {
|
||||
const parentDir = record[PRIVATE_COLUMN_KEY.PARENT_DIR];
|
||||
paths.push(Utils.joinPath(parentDir, fileName));
|
||||
idOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
idOriginalOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
}
|
||||
});
|
||||
} else if (selectedPosition) {
|
||||
const { groupRecordIndex, rowIdx } = selectedPosition;
|
||||
const record = recordGetterByIndex({ isGroupView, groupRecordIndex, recordIndex: rowIdx });
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (Utils.isSdocFile(fileName) && canModifyRow(record)) {
|
||||
const parentDir = record[PRIVATE_COLUMN_KEY.PARENT_DIR];
|
||||
paths.push(Utils.joinPath(parentDir, fileName));
|
||||
idOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
idOriginalOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
}
|
||||
const { groupRecordIndex, rowIdx } = selectedPosition;
|
||||
const record = recordGetterByIndex({ isGroupView, groupRecordIndex, recordIndex: rowIdx });
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (Utils.isSdocFile(fileName) && canModifyRow(record)) {
|
||||
const parentDir = record[PRIVATE_COLUMN_KEY.PARENT_DIR];
|
||||
path = Utils.joinPath(parentDir, fileName);
|
||||
idOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
idOriginalOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
}
|
||||
if (paths.length === 0) return;
|
||||
window.sfMetadataContext.generateSummary(paths).then(res => {
|
||||
const updatedRecords = res.data.rows;
|
||||
let recordIds = [];
|
||||
if (path === '') return;
|
||||
window.sfMetadataContext.generateSummary(path).then(res => {
|
||||
const summary = res.data.summary;
|
||||
const updateRecordId = record[PRIVATE_COLUMN_KEY.ID];
|
||||
const recordIds = [updateRecordId];
|
||||
let idRecordUpdates = {};
|
||||
let idOriginalRecordUpdates = {};
|
||||
updatedRecords.forEach(updatedRecord => {
|
||||
const { _id: updateRecordId, _summary } = updatedRecord;
|
||||
recordIds.push(updateRecordId);
|
||||
idRecordUpdates[updateRecordId] = { [summaryColumnKey]: _summary };
|
||||
idOriginalRecordUpdates[updateRecordId] = { [summaryColumnKey]: _summary };
|
||||
});
|
||||
idRecordUpdates[updateRecordId] = { [summaryColumnKey]: summary };
|
||||
idOriginalRecordUpdates[updateRecordId] = { [summaryColumnKey]: summary };
|
||||
updateRecords({ recordIds, idRecordUpdates, idOriginalRecordUpdates, idOldRecordData, idOriginalOldRecordData });
|
||||
}).catch(error => {
|
||||
const errorMessage = gettext('Failed to generate summary');
|
||||
toaster.danger(errorMessage);
|
||||
});
|
||||
}, [isGroupView, selectedRange, selectedPosition, recordMetrics, metadata, recordGetterByIndex, updateRecords]);
|
||||
}, [isGroupView, selectedPosition, recordGetterByIndex, updateRecords]);
|
||||
|
||||
const imageCaption = useCallback(() => {
|
||||
const canModifyRow = window.sfMetadataContext.canModifyRow;
|
||||
const summaryColumnKey = PRIVATE_COLUMN_KEY.FILE_SUMMARY;
|
||||
let path = '';
|
||||
let idOldRecordData = {};
|
||||
let idOriginalOldRecordData = {};
|
||||
const { groupRecordIndex, rowIdx } = selectedPosition;
|
||||
const record = recordGetterByIndex({ isGroupView, groupRecordIndex, recordIndex: rowIdx });
|
||||
const fileName = record[PRIVATE_COLUMN_KEY.FILE_NAME];
|
||||
if (Utils.imageCheck(fileName) && canModifyRow(record)) {
|
||||
const parentDir = record[PRIVATE_COLUMN_KEY.PARENT_DIR];
|
||||
path = Utils.joinPath(parentDir, fileName);
|
||||
idOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
idOriginalOldRecordData[record[PRIVATE_COLUMN_KEY.ID]] = { [summaryColumnKey]: record[summaryColumnKey] };
|
||||
}
|
||||
if (path === '') return;
|
||||
window.sfMetadataContext.imageCaption(path).then(res => {
|
||||
const desc = res.data.desc;
|
||||
const updateRecordId = record[PRIVATE_COLUMN_KEY.ID];
|
||||
const recordIds = [updateRecordId];
|
||||
let idRecordUpdates = {};
|
||||
let idOriginalRecordUpdates = {};
|
||||
idRecordUpdates[updateRecordId] = { [summaryColumnKey]: desc };
|
||||
idOriginalRecordUpdates[updateRecordId] = { [summaryColumnKey]: desc };
|
||||
updateRecords({ recordIds, idRecordUpdates, idOriginalRecordUpdates, idOldRecordData, idOriginalOldRecordData });
|
||||
}).catch(error => {
|
||||
const errorMessage = gettext('Failed to generate image description');
|
||||
toaster.danger(errorMessage);
|
||||
});
|
||||
}, [isGroupView, selectedPosition, recordGetterByIndex, updateRecords]);
|
||||
|
||||
const handleOptionClick = useCallback((event, option) => {
|
||||
event.stopPropagation();
|
||||
@@ -215,12 +220,16 @@ const ContextMenu = ({
|
||||
generateSummary && generateSummary();
|
||||
break;
|
||||
}
|
||||
case OPERATION.IMAGE_CAPTION: {
|
||||
imageCaption && imageCaption();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
setVisible(false);
|
||||
}, [onOpenFileInNewTab, onOpenParentFolder, onCopySelected, onClearSelected, generateSummary]);
|
||||
}, [onOpenFileInNewTab, onOpenParentFolder, onCopySelected, onClearSelected, generateSummary, imageCaption]);
|
||||
|
||||
const getMenuPosition = useCallback((x = 0, y = 0) => {
|
||||
let menuStyles = {
|
||||
|
@@ -2,12 +2,12 @@ import metadataAPI from '../api';
|
||||
import { LocalStorage, PRIVATE_COLUMN_KEYS, EDITABLE_DATA_PRIVATE_COLUMN_KEYS,
|
||||
EDITABLE_PRIVATE_COLUMN_KEYS, PREDEFINED_COLUMN_KEYS } from './_basic';
|
||||
import EventBus from '../../components/common/event-bus';
|
||||
import { username } from '../../utils/constants';
|
||||
import { username, lang } from '../../utils/constants';
|
||||
|
||||
class Context {
|
||||
|
||||
constructor() {
|
||||
this.settings = {};
|
||||
this.settings = { lang };
|
||||
this.metadataAPI = null;
|
||||
this.localStorage = null;
|
||||
this.eventBus = null;
|
||||
@@ -20,7 +20,7 @@ class Context {
|
||||
if (this.hasInit) return;
|
||||
|
||||
// init settings
|
||||
this.settings = settings || {};
|
||||
this.settings = { ...this.settings, ...settings };
|
||||
|
||||
// init metadataAPI
|
||||
const { repoInfo } = this.settings;
|
||||
@@ -192,9 +192,15 @@ class Context {
|
||||
};
|
||||
|
||||
// ai
|
||||
generateSummary = (filePaths) => {
|
||||
generateSummary = (filePath) => {
|
||||
const repoID = this.settings['repoID'];
|
||||
return this.metadataAPI.generateSummary(repoID, filePaths);
|
||||
return this.metadataAPI.generateSummary(repoID, filePath);
|
||||
};
|
||||
|
||||
imageCaption = (filePath) => {
|
||||
const repoID = this.settings['repoID'];
|
||||
const lang = this.settings['lang'];
|
||||
return this.metadataAPI.imageCaption(repoID, filePath, lang);
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user