π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
add clip computeImageEmbeddings API
Abhinav62099fc6c92e
diff --git a/src/api/clip.ts b/src/api/clip.tsnew file mode 100644index 0000000000..355648adda--- /dev/null+++ b/src/api/clip.ts@@ -0,0 +1,24 @@+import { ipcRenderer } from 'electron';+import { writeStream } from '../services/fs';++export async function computeImageEmbeddings(+ imageFile: File+): Promise<Float32Array> {+ let tempInputFilePath = null;+ try {+ tempInputFilePath = await ipcRenderer.invoke(+ 'get-temp-file-path',+ imageFile.name+ );+ await writeStream(tempInputFilePath, imageFile.stream());+ const embeddings = await ipcRenderer.invoke(+ 'compute-image-embeddings',+ tempInputFilePath+ );+ return embeddings;+ } finally {+ if (tempInputFilePath) {+ await ipcRenderer.invoke('remove-temp-file', tempInputFilePath);+ }+ }+}diff --git a/src/preload.ts b/src/preload.tsindex 279754138d..be72cd2d53 100644--- a/src/preload.ts+++ b/src/preload.ts@@ -59,6 +59,7 @@ import {logRendererProcessMemoryUsage,} from './utils/processStats';import { runFFmpegCmd } from './api/ffmpeg';+import { computeImageEmbeddings } from './api/clip';fixHotReloadNext12();setupLogging();@@ -114,4 +115,5 @@ windowObject['ElectronAPIs'] = {rename,deleteFile,updateOptOutOfCrashReports,+ computeImageEmbeddings,};diff --git a/src/services/clipService.ts b/src/services/clipService.tsnew file mode 100644index 0000000000..ea927a99ee--- /dev/null+++ b/src/services/clipService.ts@@ -0,0 +1,56 @@+import * as log from 'electron-log';+import util from 'util';+import path from 'path';+const shellescape = require('any-shell-escape');+const execAsync = util.promisify(require('child_process').exec);++const CLIP_MODEL_PATH_PLACEHOLDER = 'CLIP_MODEL';+const GGMLCLIP_PATH_PLACEHOLDER = 'GGML_PATH';+const INPUT_PATH_PLACEHOLDER = 'INPUT';++const IMAGE_EMBEDDING_EXTRACT_CMD: string[] = [+ GGMLCLIP_PATH_PLACEHOLDER,+ '-m',+ CLIP_MODEL_PATH_PLACEHOLDER,+ '-image',+ INPUT_PATH_PLACEHOLDER,+];++function getClipModelPath() {+ return path.join(+ __dirname,+ '../models/openai_clip-vit-base-patch32.ggmlv0.f16'+ );+}++function getGGMLClipPath() {+ return path.join(__dirname, '../bin/ggmlclip');+}++export async function computeImageEmbeddings(+ inputFilePath: string+): Promise<Float32Array> {+ const clipModelPath = getClipModelPath();+ const ggmlclipPath = getGGMLClipPath();+ const cmd = IMAGE_EMBEDDING_EXTRACT_CMD.map((cmdPart) => {+ if (cmdPart === GGMLCLIP_PATH_PLACEHOLDER) {+ return ggmlclipPath;+ } else if (cmdPart === CLIP_MODEL_PATH_PLACEHOLDER) {+ return clipModelPath;+ } else if (cmdPart === INPUT_PATH_PLACEHOLDER) {+ return inputFilePath;+ } else {+ return cmdPart;+ }+ });++ const escapedCmd = shellescape(cmd);+ log.info('running clip command', escapedCmd);+ const startTime = Date.now();+ const { stdout, stderr } = await execAsync(escapedCmd);+ log.info('clip command execution time ', Date.now() - startTime);+ log.info('clip command stdout ', stdout);+ log.info('clip command stderr ', stderr);+ const embeddings = JSON.parse(stdout);+ return embeddings;+}diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.tsindex 99c537dcef..1b435121c0 100644--- a/src/utils/ipcComms.ts+++ b/src/utils/ipcComms.ts@@ -27,6 +27,7 @@ import { deleteTempFile, runFFmpegCmd } from '../services/ffmpeg';import { generateTempFilePath } from './temp';import { setOptOutOfCrashReports } from '../services/userPreference';import { updateOptOutOfCrashReports } from '../main';+import { computeImageEmbeddings } from '../services/clipService';export default function setupIpcComs(tray: Tray,@@ -166,4 +167,7 @@ export default function setupIpcComs(setOptOutOfCrashReports(optOut);updateOptOutOfCrashReports(optOut);});+ ipcMain.handle('compute-image-embeddings', (_, inputFilePath) => {+ return computeImageEmbeddings(inputFilePath);+ });}