Skip to content

armgit / ente

Public Imported from GitHub
0

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

Continue

Manav Rathi900a7942abdd
diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/embedding.ts
index b7f3fca1cd..cb2149592f 100644
--- a/web/packages/new/photos/services/ml/embedding.ts
+++ b/web/packages/new/photos/services/ml/embedding.ts
@@ -14,7 +14,11 @@ import { apiURL } from "@/next/origins";
import { z } from "zod";
import { clipIndexingVersion, type CLIPIndex } from "./clip";
import { saveCLIPIndex, saveFaceIndex } from "./db";
-import { faceIndexingVersion, type FaceIndex } from "./face";
+import {
+ faceIndexingVersion,
+ type FaceIndex,
+ type RemoteFaceIndex,
+} from "./face";
/**
* The embeddings that we (the current client) knows how to handle.
@@ -342,11 +346,11 @@ const putEmbeddingString = async (
*/
export const putDerivedData = async (
enteFile: EnteFile,
- faceIndex: FaceIndex,
+ remoteFaceIndex: RemoteFaceIndex,
clipIndex: CLIPIndex,
) => {
const combined = {
- face: faceIndex,
+ face: remoteFaceIndex,
clip: clipIndex,
};
log.debug(() => ["Uploading derived data", combined]);
@@ -415,20 +419,20 @@ const saveFaceIndexIfNewer = async (index: FaceIndex) => {
};
/**
- * Zod schemas for the {@link FaceIndex} types.
+ * Zod schemas for the {@link RemoteFaceIndex} type.
*
- * [Note: Duplicated between Zod schemas and TS type]
+ * [Note: Duplicated Zod schema and TypeScript type]
*
* Usually we define a Zod schema, and then infer the corresponding TypeScript
- * type for it using `z.infer`. This works great except now the docstrings don't
- * show up: The doc strings get added to the Zod schema, but usually the code
+ * type for it using `z.infer`. This works great except that the docstrings
+ * don't show up: Docstrings get added to the Zod schema, but usually the code
* using the parsed data will reference the TypeScript type, and the docstrings
* added to the fields in the Zod schema won't show up.
*
- * We usually live with this infelicity, since the alternative is code
- * duplication: Define the TypeScript type (putting the docstrings therein)
- * _and_ also a corresponding Zod schema. The duplication happens because it is
- * not possible to go the other way (TS type => Zod schema).
+ * We usually live with this infelicity since the alternative is code
+ * duplication: Defining a TypeScript type (putting the docstrings therein)
+ * _and_ also a corresponding Zod schema. The duplication is needed because it
+ * is not possible to go the other way (TypeScript type => Zod schema).
*
* However, in some cases having when the TypeScript type under consideration is
* used pervasively in code, having a standalone TypeScript type with attached
@@ -438,50 +442,32 @@ const saveFaceIndexIfNewer = async (index: FaceIndex) => {
* out of sync in the shape of the types they represent, the TypeScript compiler
* will flag it for us.
*/
-const FaceIndex = z
- .object({
- fileID: z.number(),
- width: z.number(),
- height: z.number(),
- faceEmbedding: z
- .object({
- version: z.number(),
- client: z.string(),
- faces: z.array(
- z
- .object({
- faceID: z.string(),
- detection: z
- .object({
- box: z
- .object({
- x: z.number(),
- y: z.number(),
- width: z.number(),
- height: z.number(),
- })
- .passthrough(),
- landmarks: z.array(
- z
- .object({
- x: z.number(),
- y: z.number(),
- })
- .passthrough(),
- ),
- })
- .passthrough(),
- score: z.number(),
- blur: z.number(),
- embedding: z.array(z.number()),
- })
- .passthrough(),
+const RemoteFaceIndex = z.object({
+ version: z.number(),
+ client: z.string(),
+ faces: z.array(
+ z.object({
+ faceID: z.string(),
+ detection: z.object({
+ box: z.object({
+ x: z.number(),
+ y: z.number(),
+ width: z.number(),
+ height: z.number(),
+ }),
+ landmarks: z.array(
+ z.object({
+ x: z.number(),
+ y: z.number(),
+ }),
),
- })
- .passthrough(),
- })
- // Retain fields we might not (currently) understand.
- .passthrough();
+ }),
+ score: z.number(),
+ blur: z.number(),
+ embedding: z.array(z.number()),
+ }),
+ ),
+});
/**
* Save the face index for the given {@link enteFile} on remote so that other
@@ -533,7 +519,7 @@ const saveCLIPIndexIfNewer = async (index: CLIPIndex) => {
/**
* Zod schemas for the {@link CLIPIndex} types.
*
- * See: [Note: Duplicated between Zod schemas and TS type]
+ * See: [Note: Duplicated Zod schema and TypeScript type]
*/
const CLIPIndex = z
.object({
diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts
index 2f75ac2b5c..1f982125d3 100644
--- a/web/packages/new/photos/services/ml/worker.ts
+++ b/web/packages/new/photos/services/ml/worker.ts
@@ -25,8 +25,8 @@ import {
saveFaceIndex,
updateAssumingLocalFiles,
} from "./db";
-import { pullFaceEmbeddings, putCLIPIndex, putFaceIndex } from "./embedding";
-import { indexFaces, type FaceIndex } from "./face";
+import { pullFaceEmbeddings, putDerivedData } from "./embedding";
+import { faceIndexingVersion, indexFaces, type FaceIndex } from "./face";
import type { MLWorkerDelegate, MLWorkerElectron } from "./worker-types";
const idleDurationStart = 5; /* 5 seconds */
@@ -440,7 +440,7 @@ const index = async (
try {
[faceIndex, clipIndex] = await Promise.all([
- indexFaces(enteFile, image, electron, userAgent),
+ indexFaces(enteFile, image, electron),
indexCLIP(enteFile, image, electron, userAgent),
]);
} catch (e) {
@@ -456,9 +456,14 @@ const index = async (
return `Indexed ${nf} faces and clip in ${f} (${ms} ms)`;
});
+ const remoteFaceIndex = {
+ ...faceIndex,
+ version: faceIndexingVersion,
+ client: userAgent,
+ };
+
try {
- await putFaceIndex(enteFile, faceIndex);
- await putCLIPIndex(enteFile, clipIndex);
+ await putDerivedData(enteFile, remoteFaceIndex, clipIndex);
await saveFaceIndex(faceIndex);
await saveCLIPIndex(clipIndex);
} catch (e) {