Skip to content

armgit / ente

Public Imported from GitHub
0

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

Integrate static APIs

vishnukvmd94ab10315be8
diff --git a/lib/services/semantic_search_service.dart b/lib/services/semantic_search_service.dart
index bbe828b722..0734aca8a6 100644
--- a/lib/services/semantic_search_service.dart
+++ b/lib/services/semantic_search_service.dart
@@ -1,4 +1,3 @@
-import "dart:convert";
import "dart:io";
import "package:clip_ggml/clip_ggml.dart";
@@ -12,23 +11,32 @@ class SemanticSearchService {
static final SemanticSearchService instance =
SemanticSearchService._privateConstructor();
- late CLIP _clip;
+ bool hasLoaded = false;
final _logger = Logger("SemanticSearchService");
Future<void> init() async {
- _clip = CLIP();
await _loadModel();
_testJson();
}
+ Future<void> runInference(Uint8List image, String text) async {
+ if (!hasLoaded) {
+ return;
+ }
+ final imagePath = await _writeToFile(image, "input.jpg");
+ final imageEmbedding = CLIP.createImageEmbedding(imagePath);
+ final textEmbedding = CLIP.createTextEmbedding(text);
+ final score = CLIP.computeScore(imageEmbedding, textEmbedding);
+ _logger.info("Score: " + score.toString());
+ }
+
Future<void> _loadModel() async {
- final clip = CLIP();
const modelPath =
"assets/models/clip/openai_clip-vit-base-patch32.ggmlv0.f16.bin";
final path = await _getAccessiblePathForAsset(modelPath, "model.bin");
final startTime = DateTime.now();
- clip.loadModel(path);
+ CLIP.loadModel(path);
final endTime = DateTime.now();
_logger.info(
"Loading model took: " +
@@ -36,21 +44,18 @@ class SemanticSearchService {
.toString() +
"ms",
);
+ hasLoaded = true;
_testJson();
}
Future<void> _testJson() async {
final startTime = DateTime.now();
- final input = {
- "embedding": [1.1, 2.2],
- };
- _logger.info(jsonEncode(input));
- final result = _clip.createTextEmbedding("hello world");
+ final result = CLIP.createTextEmbedding("hello world");
final endTime = DateTime.now();
_logger.info(
"Output: " +
- result +
+ result.toString() +
" (" +
(endTime.millisecondsSinceEpoch - startTime.millisecondsSinceEpoch)
.toString() +
@@ -63,9 +68,13 @@ class SemanticSearchService {
String tempName,
) async {
final byteData = await rootBundle.load(assetPath);
+ return _writeToFile(byteData.buffer.asUint8List(), tempName);
+ }
+
+ Future<String> _writeToFile(Uint8List bytes, String fileName) async {
final tempDir = await getTemporaryDirectory();
- final file = await File('${tempDir.path}/$tempName')
- .writeAsBytes(byteData.buffer.asUint8List());
+ final file = await File('${tempDir.path}/$fileName')
+ .writeAsBytes(bytes);
return file.path;
}
}
diff --git a/lib/ui/viewer/file/zoomable_image.dart b/lib/ui/viewer/file/zoomable_image.dart
index 6ac386f93d..9bf691d330 100644
--- a/lib/ui/viewer/file/zoomable_image.dart
+++ b/lib/ui/viewer/file/zoomable_image.dart
@@ -16,6 +16,7 @@ import "package:photos/models/file/extensions/file_props.dart";
import 'package:photos/models/file/file.dart';
import "package:photos/models/metadata/file_magic.dart";
import "package:photos/services/file_magic_service.dart";
+import "package:photos/services/semantic_search_service.dart";
import 'package:photos/ui/common/loading_widget.dart';
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/image_util.dart';
@@ -59,6 +60,7 @@ class _ZoomableImageState extends State<ZoomableImage>
@override
void initState() {
_photo = widget.photo;
+ _test();
_logger = Logger("ZoomableImage");
_logger.info('initState for ${_photo.generatedID} with tag ${_photo.tag}');
_scaleStateChangedCallback = (value) {
@@ -78,6 +80,13 @@ class _ZoomableImageState extends State<ZoomableImage>
super.dispose();
}
+ void _test() {
+ final thumb = ThumbnailInMemoryLruCache.get(_photo);
+ if (thumb != null) {
+ SemanticSearchService.instance.runInference(thumb, "toddler");
+ }
+ }
+
@override
Widget build(BuildContext context) {
if (_photo.isRemoteFile) {
diff --git a/plugins/clip_ggml b/plugins/clip_ggml
index 5d2d390aeb..84c5a499e7 160000
--- a/plugins/clip_ggml
+++ b/plugins/clip_ggml
@@ -1 +1 @@
-Subproject commit 5d2d390aeb8640a69c19bd62ddd2540906e393f7
+Subproject commit 84c5a499e700cc72e546c42d771d4a92a7a21ec2