Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fetch and cache embeddings

vishnukvmda5e2fff272bc
diff --git a/lib/services/semantic_search_service.dart b/lib/services/semantic_search_service.dart
index 5de62282a0..3d47912485 100644
--- a/lib/services/semantic_search_service.dart
+++ b/lib/services/semantic_search_service.dart
@@ -5,6 +5,10 @@ import "package:computer/computer.dart";
import "package:flutter/services.dart";
import "package:logging/logging.dart";
import "package:path_provider/path_provider.dart";
+import "package:photos/db/files_db.dart";
+import "package:photos/models/embedding.dart";
+import "package:photos/models/file/file.dart";
+import "package:photos/utils/thumbnail_util.dart";
class SemanticSearchService {
SemanticSearchService._privateConstructor();
@@ -21,22 +25,37 @@ class SemanticSearchService {
await _loadModel();
}
- Future<void> runInference(Uint8List image, String text) async {
+ Future<void> runInference(EnteFile file, String text) async {
if (!hasLoaded || isRunning) {
return;
}
isRunning = true;
_logger.info("Running clip");
- final imagePath = await _writeToFile(image, "input.jpg");
+ final imagePath = (await getThumbnailFile(file))!.path;
var startTime = DateTime.now();
- final imageEmbedding = await _computer.compute(
- createImageEmbedding,
- param: {
- "imagePath": imagePath,
- },
- taskName: "createImageEmbedding",
- );
+ // ignore: prefer_typing_uninitialized_variables
+ var imageEmbedding;
+ final embeddings = await FilesDB.instance.getAllEmbeddings();
+ bool hasCachedEmbedding = false;
+ for (final embedding in embeddings) {
+ if (embedding.id == file.generatedID) {
+ imageEmbedding = embedding.embedding;
+ hasCachedEmbedding = true;
+ _logger.info("Found cached embedding");
+ }
+ }
+ if (!hasCachedEmbedding) {
+ imageEmbedding ??= await _computer.compute(
+ createImageEmbedding,
+ param: {
+ "imagePath": imagePath,
+ },
+ taskName: "createImageEmbedding",
+ );
+ await FilesDB.instance
+ .insertEmbedding(Embedding(file.generatedID!, imageEmbedding, -1));
+ }
var endTime = DateTime.now();
_logger.info(
"createImageEmbedding took: " +
diff --git a/lib/ui/viewer/file/zoomable_image.dart b/lib/ui/viewer/file/zoomable_image.dart
index 35237b47b4..a92f3970ca 100644
--- a/lib/ui/viewer/file/zoomable_image.dart
+++ b/lib/ui/viewer/file/zoomable_image.dart
@@ -60,7 +60,7 @@ class _ZoomableImageState extends State<ZoomableImage>
@override
void initState() {
_photo = widget.photo;
- _test();
+ SemanticSearchService.instance.runInference(_photo, "desert with a tree");
_logger = Logger("ZoomableImage");
_logger.info('initState for ${_photo.generatedID} with tag ${_photo.tag}');
_scaleStateChangedCallback = (value) {
@@ -80,13 +80,6 @@ class _ZoomableImageState extends State<ZoomableImage>
super.dispose();
}
- void _test() {
- final thumb = ThumbnailInMemoryLruCache.get(_photo);
- if (thumb != null) {
- SemanticSearchService.instance.runInference(thumb, "desert with a tree");
- }
- }
-
@override
Widget build(BuildContext context) {
if (_photo.isRemoteFile) {
diff --git a/lib/utils/thumbnail_util.dart b/lib/utils/thumbnail_util.dart
index 109446ca62..393e62aab2 100644
--- a/lib/utils/thumbnail_util.dart
+++ b/lib/utils/thumbnail_util.dart
@@ -42,6 +42,18 @@ Future<Uint8List?> getThumbnail(EnteFile file) async {
}
}
+Future<File?> getThumbnailFile(EnteFile file) async {
+ final thumbnail = await getThumbnail(file);
+ if (thumbnail != null) {
+ final cachedThumbnail = cachedThumbnailPath(file);
+ if (!await cachedThumbnail.exists()) {
+ await cachedThumbnail.writeAsBytes(thumbnail, flush: true);
+ }
+ return cachedThumbnail;
+ }
+ return null;
+}
+
Future<Uint8List> getThumbnailFromServer(EnteFile file) async {
final cachedThumbnail = cachedThumbnailPath(file);
if (await cachedThumbnail.exists()) {