π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Convert more
Manav Rathi3ae6c72332fe
diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.tsindex fab1147c37..e0bf6eecd4 100644--- a/web/packages/new/photos/services/ml/clip.ts+++ b/web/packages/new/photos/services/ml/clip.ts@@ -1,4 +1,3 @@-import type { EnteFile } from "@/new/photos/types/file";import type { Electron } from "@/next/types/ipc";import type { ImageBitmapAndData } from "./blob";import { clipIndexes } from "./db";@@ -59,14 +58,8 @@ export const clipIndexingVersion = 1;* itself, is the same across clients - web and mobile.*/export interface CLIPIndex {- /** The ID of the {@link EnteFile} whose index this is. */- fileID: number;- /** An integral version number of the indexing algorithm / pipeline. */- version: number;- /** The UA for the client which generated this embedding. */- client: string;/**- * The CLIP embedding itself.+ * The CLIP embedding.** This is an array of 512 floating point values that represent the* embedding of the image in the same space where we'll embed the text so@@ -75,6 +68,18 @@ export interface CLIPIndex {embedding: number[];}+export type RemoteCLIPIndex = CLIPIndex & {+ /** An integral version number of the indexing algorithm / pipeline. */+ version: number;+ /** The UA for the client which generated this embedding. */+ client: string;+};++export type LocalCLIPIndex = CLIPIndex & {+ /** The ID of the {@link EnteFile} whose index this is. */+ fileID: number;+};+/*** Compute the CLIP embedding of a given file.*@@ -89,33 +94,19 @@ export interface CLIPIndex {* that it can be saved locally and also uploaded to the user's remote storage* for use on their other devices).*- * @param enteFile The {@link EnteFile} to index.- ** @param uploadItem If we're called during the upload process, then this will* be set to the {@link UploadItem} that was uploaded. This way, we can directly* use the on-disk file instead of needing to download the original from remote.** @param electron The {@link MLWorkerElectron} instance that allows us to call- * our Node.js layer for various functionality.- *- * @param userAgent The UA of the client that is doing the indexing (us).+ * our Node.js layer to run the ONNX inference.*/export const indexCLIP = async (- enteFile: EnteFile,image: ImageBitmapAndData,electron: MLWorkerElectron,- userAgent: string,-): Promise<CLIPIndex> => {- const { data: imageData } = image;- const fileID = enteFile.id;-- return {- fileID,- version: clipIndexingVersion,- client: userAgent,- embedding: await computeEmbedding(imageData, electron),- };-};+): Promise<CLIPIndex> => ({+ embedding: await computeEmbedding(image.data, electron),+});const computeEmbedding = async (imageData: ImageData,@@ -184,10 +175,10 @@ const normalized = (embedding: Float32Array) => {* native code running in our desktop app (the embedding happens in the native* layer).*- * It return a list of files that should be shown in the search results. The- * actual return type is a map from fileIDs to the scores they got (higher is- * better). This map will only contains entries whose score was above our- * minimum threshold.+ * It returns file (IDs) that should be shown in the search results. They're+ * returned as a map from fileIDs to the scores they got (higher is better).+ * This map will only contains entries whose score was above our minimum+ * threshold.** The result can also be `undefined`, which indicates that the download for the* ML model is still in progress (trying again later should succeed).diff --git a/web/packages/new/photos/services/ml/db.ts b/web/packages/new/photos/services/ml/db.tsindex 51ca2f749b..b5a1c0e515 100644--- a/web/packages/new/photos/services/ml/db.ts+++ b/web/packages/new/photos/services/ml/db.ts@@ -1,9 +1,9 @@import log from "@/next/log";import localForage from "@ente/shared/storage/localForage";import { deleteDB, openDB, type DBSchema } from "idb";-import type { CLIPIndex } from "./clip";+import type { LocalCLIPIndex } from "./clip";import type { EmbeddingModel } from "./embedding";-import type { FaceIndex } from "./face";+import type { LocalFaceIndex } from "./face";/*** ML DB schema.@@ -15,11 +15,11 @@ import type { FaceIndex } from "./face";* required), this is synced with the list of files that the current client* knows about locally.*- * - "face-index": Contains {@link FaceIndex} objects, either indexed locally or- * fetched from remote storage.+ * - "face-index": Contains {@link LocalFaceIndex} objects, either indexed+ * locally or fetched from remote.*- * - "clip-index": Contains {@link CLIPIndex} objects, either indexed locally or- * fetched from remote storage.+ * - "clip-index": Contains {@link LocalCLIPIndex} objects, either indexed+ * locally or fetched from remote.** All the stores are keyed by {@link fileID}. The "file-status" contains* book-keeping about the indexing process (whether or not a file needs@@ -27,7 +27,7 @@ import type { FaceIndex } from "./face";* the actual indexing results.** In tandem, these serve as the underlying storage for the functions exposed by- * this file.+ * the ML database.*/interface MLDBSchema extends DBSchema {"file-status": {@@ -37,11 +37,11 @@ interface MLDBSchema extends DBSchema {};"face-index": {key: number;- value: FaceIndex;+ value: LocalFaceIndex;};"clip-index": {key: number;- value: CLIPIndex;+ value: LocalCLIPIndex;};}@@ -194,7 +194,7 @@ export const clearMLDB = async () => {* embeddings. If there are no other pending embeddings, the status changes to* "indexed".*/-export const saveFaceIndex = async (faceIndex: FaceIndex) => {+export const saveFaceIndex = async (faceIndex: LocalFaceIndex) => {const { fileID } = faceIndex;const db = await mlDB();@@ -242,7 +242,7 @@ const newFileStatus = (fileID: number): FileStatus => ({* embeddings. If there are no other pending embeddings, the status changes to* "indexed".*/-export const saveCLIPIndex = async (clipIndex: CLIPIndex) => {+export const saveCLIPIndex = async (clipIndex: LocalCLIPIndex) => {const { fileID } = clipIndex;const db = await mlDB();diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/embedding.tsindex cb2149592f..fd6e21568e 100644--- a/web/packages/new/photos/services/ml/embedding.ts+++ b/web/packages/new/photos/services/ml/embedding.ts@@ -12,7 +12,11 @@ import { getKV, setKV } from "@/next/kv";import log from "@/next/log";import { apiURL } from "@/next/origins";import { z } from "zod";-import { clipIndexingVersion, type CLIPIndex } from "./clip";+import {+ clipIndexingVersion,+ type CLIPIndex,+ type RemoteCLIPIndex,+} from "./clip";import { saveCLIPIndex, saveFaceIndex } from "./db";import {faceIndexingVersion,@@ -347,11 +351,11 @@ const putEmbeddingString = async (export const putDerivedData = async (enteFile: EnteFile,remoteFaceIndex: RemoteFaceIndex,- clipIndex: CLIPIndex,+ remoteCLIPIndex: RemoteCLIPIndex,) => {const combined = {face: remoteFaceIndex,- clip: clipIndex,+ clip: remoteCLIPIndex,};log.debug(() => ["Uploading derived data", combined]);diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.tsindex 1f982125d3..159803a46f 100644--- a/web/packages/new/photos/services/ml/worker.ts+++ b/web/packages/new/photos/services/ml/worker.ts@@ -16,7 +16,7 @@ import {renderableBlob,type ImageBitmapAndData,} from "./blob";-import { indexCLIP, type CLIPIndex } from "./clip";+import { clipIndexingVersion, indexCLIP, type CLIPIndex } from "./clip";import { saveFaceCrops } from "./crop";import {indexableFileIDs,@@ -441,7 +441,7 @@ const index = async (try {[faceIndex, clipIndex] = await Promise.all([indexFaces(enteFile, image, electron),- indexCLIP(enteFile, image, electron, userAgent),+ indexCLIP(image, electron),]);} catch (e) {// See: [Note: Transient and permanent indexing failures]@@ -462,18 +462,31 @@ const index = async (client: userAgent,};+ const remoteCLIPIndex = {+ ...clipIndex,+ version: clipIndexingVersion,+ client: userAgent,+ };++ try {+ await putDerivedData(enteFile, remoteFaceIndex, remoteCLIPIndex);+ } catch (e) {+ // See: [Note: Transient and permanent indexing failures]+ log.error(`Failed to put face index for ${f}`, e);+ if (isHTTP4xxError(e)) await markIndexingFailed(enteFile.id);+ throw e;+ }++ const fileID = enteFile.id;+try {- await putDerivedData(enteFile, remoteFaceIndex, clipIndex);- await saveFaceIndex(faceIndex);- await saveCLIPIndex(clipIndex);+ await saveFaceIndex({ fileID, ...faceIndex });+ await saveCLIPIndex({ fileID, ...clipIndex });} catch (e) {// Not sure if DB failures should be considered permanent or// transient. There isn't a known case where writing to the local// indexedDB would fail.- //- // See: [Note: Transient and permanent indexing failures]- log.error(`Failed to put/save face index for ${f}`, e);- if (isHTTP4xxError(e)) await markIndexingFailed(enteFile.id);+ log.error(`Failed to save face index for ${f}`, e);throw e;}