Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

adds computeTextEmbeddings API

Abhinav43c806be76db
diff --git a/src/api/clip.ts b/src/api/clip.ts
index 31692a7fe5..9020244605 100644
--- a/src/api/clip.ts
+++ b/src/api/clip.ts
@@ -20,3 +20,13 @@ export async function computeImageEmbeddings(
}
}
}
+
+export async function computeTextEmbeddings(
+ text: string
+): Promise<Float32Array> {
+ const embeddings = await ipcRenderer.invoke(
+ 'compute-text-embeddings',
+ text
+ );
+ return embeddings;
+}
diff --git a/src/preload.ts b/src/preload.ts
index be72cd2d53..9d53f10178 100644
--- a/src/preload.ts
+++ b/src/preload.ts
@@ -59,7 +59,7 @@ import {
logRendererProcessMemoryUsage,
} from './utils/processStats';
import { runFFmpegCmd } from './api/ffmpeg';
-import { computeImageEmbeddings } from './api/clip';
+import { computeImageEmbeddings, computeTextEmbeddings } from './api/clip';
fixHotReloadNext12();
setupLogging();
@@ -116,4 +116,5 @@ windowObject['ElectronAPIs'] = {
deleteFile,
updateOptOutOfCrashReports,
computeImageEmbeddings,
+ computeTextEmbeddings,
};
diff --git a/src/services/clipService.ts b/src/services/clipService.ts
index c6d355d899..ccf0fbc5c8 100644
--- a/src/services/clipService.ts
+++ b/src/services/clipService.ts
@@ -16,6 +16,14 @@ const IMAGE_EMBEDDING_EXTRACT_CMD: string[] = [
INPUT_PATH_PLACEHOLDER,
];
+const TEXT_EMBEDDING_EXTRACT_CMD: string[] = [
+ GGMLCLIP_PATH_PLACEHOLDER,
+ '-m',
+ CLIP_MODEL_PATH_PLACEHOLDER,
+ '--text',
+ INPUT_PATH_PLACEHOLDER,
+];
+
function getClipModelPath() {
return './models/openai_clip-vit-base-patch32.ggmlv0.f16.bin';
}
@@ -58,3 +66,39 @@ export async function computeImageEmbeddings(
logErrorSentry(err, 'Error in computeImageEmbeddings');
}
}
+
+export async function computeTextEmbeddings(
+ text: string
+): Promise<Float32Array> {
+ try {
+ const clipModelPath = getClipModelPath();
+ const ggmlclipPath = getGGMLClipPath();
+ const cmd = TEXT_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 text;
+ } else {
+ return cmdPart;
+ }
+ });
+
+ const escapedCmd = shellescape(cmd);
+ log.info('running clip command', escapedCmd);
+ const startTime = Date.now();
+ const { stdout } = await execAsync(escapedCmd);
+ log.info('clip command execution time ', Date.now() - startTime);
+ // parse stdout and return embeddings
+ // get the last line of stdout
+ const lines = stdout.split('\n');
+ const lastLine = lines[lines.length - 1];
+ const embeddings = JSON.parse(lastLine);
+ const embeddingsArray = new Float32Array(embeddings);
+ console.log('embeddingsArray', embeddingsArray);
+ return embeddingsArray;
+ } catch (err) {
+ logErrorSentry(err, 'Error in computeImageEmbeddings');
+ }
+}
diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.ts
index 1b435121c0..5cd5123c37 100644
--- a/src/utils/ipcComms.ts
+++ b/src/utils/ipcComms.ts
@@ -27,7 +27,10 @@ import { deleteTempFile, runFFmpegCmd } from '../services/ffmpeg';
import { generateTempFilePath } from './temp';
import { setOptOutOfCrashReports } from '../services/userPreference';
import { updateOptOutOfCrashReports } from '../main';
-import { computeImageEmbeddings } from '../services/clipService';
+import {
+ computeImageEmbeddings,
+ computeTextEmbeddings,
+} from '../services/clipService';
export default function setupIpcComs(
tray: Tray,
@@ -170,4 +173,7 @@ export default function setupIpcComs(
ipcMain.handle('compute-image-embeddings', (_, inputFilePath) => {
return computeImageEmbeddings(inputFilePath);
});
+ ipcMain.handle('compute-text-embeddings', (_, text) => {
+ return computeTextEmbeddings(text);
+ });
}