Skip to content

armgit / ente

Public Imported from GitHub
0

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

add getTextEmbedding service and clip search helper utils

Abhinav396a316de557
diff --git a/apps/photos/src/services/clipService.ts b/apps/photos/src/services/clipService.ts
index 1921156723..22a61509a4 100644
--- a/apps/photos/src/services/clipService.ts
+++ b/apps/photos/src/services/clipService.ts
@@ -50,6 +50,15 @@ class ClipServiceImpl {
}
};
+ getTextEmbedding = async (text: string): Promise<Float32Array> => {
+ try {
+ return this.electronAPIs.computeTextEmbedding(text);
+ } catch (e) {
+ logError(e, 'failed to compute text embedding');
+ throw e;
+ }
+ };
+
private runClipEmbeddingExtraction = async () => {
try {
const localFiles = await getLocalFiles();
@@ -111,3 +120,37 @@ const getNonClipEmbeddingExtractedFiles = async (
);
return files.filter((file) => !existingEmbeddingFileIds.has(file.id));
};
+
+export const getAllClipImageEmbeddings = async () => {
+ const allEmbeddings = await getLocalEmbeddings();
+ return allEmbeddings.filter(
+ (embedding) => embedding.model === Model.GGML_CLIP
+ );
+};
+
+export const computeClipMatchScore = async (
+ imageEmbedding: Float32Array,
+ textEmbedding: Float32Array
+) => {
+ if (imageEmbedding.length !== textEmbedding.length) {
+ throw Error('imageEmbedding and textEmbedding length mismatch');
+ }
+ let score = 0;
+ let imageNormalization = 0;
+ let textNormalization = 0;
+
+ for (let index = 0; index < imageEmbedding.length; index++) {
+ imageNormalization += imageEmbedding[index] * imageEmbedding[index];
+ textNormalization += textEmbedding[index] * textEmbedding[index];
+ }
+ for (let index = 0; index < imageEmbedding.length; index++) {
+ imageEmbedding[index] =
+ imageEmbedding[index] / Math.sqrt(imageNormalization);
+ textEmbedding[index] =
+ textEmbedding[index] / Math.sqrt(textNormalization);
+ }
+ for (let index = 0; index < imageEmbedding.length; index++) {
+ score += imageEmbedding[index] * textEmbedding[index];
+ }
+ return score;
+};