π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob] Refactor
Neeraj Gupta2cade6f27110
diff --git a/mobile/lib/services/machine_learning/ml_service.dart b/mobile/lib/services/machine_learning/ml_service.dartindex 51831be5ae..d6dcdb3bfc 100644--- a/mobile/lib/services/machine_learning/ml_service.dart+++ b/mobile/lib/services/machine_learning/ml_service.dart@@ -53,7 +53,7 @@ class MLService {bool _shouldPauseIndexingAndClustering = false;static const _kForceClusteringFaceCount = 8000;- late final faceMLDB = MLDataDB.instance;+ late final mlDataDB = MLDataDB.instance;/// Only call this function once at app startup, after that you can directly call [runAllML]Future<void> init() async {@@ -133,7 +133,7 @@ class MLService {await sync();final int unclusteredFacesCount =- await faceMLDB.getUnclusteredFaceCount();+ await mlDataDB.getUnclusteredFaceCount();if (unclusteredFacesCount > _kForceClusteringFaceCount) {_logger.info("There are $unclusteredFacesCount unclustered faces, doing clustering first",@@ -147,7 +147,7 @@ class MLService {if (canFetch()) {await fetchAndIndexAllImages();}- if ((await faceMLDB.getUnclusteredFaceCount()) > 0) {+ if ((await mlDataDB.getUnclusteredFaceCount()) > 0) {await clusterAllImages();}if (_mlControllerStatus == true) {@@ -272,12 +272,12 @@ class MLService {try {// Get a sense of the total number of faces in the database- final int totalFaces = await faceMLDB.getTotalFaceCount();+ final int totalFaces = await mlDataDB.getTotalFaceCount();final fileIDToCreationTime =await FilesDB.instance.getFileIDToCreationTime();final startEmbeddingFetch = DateTime.now();// read all embeddings- final result = await faceMLDB.getFaceInfoForClustering(+ final result = await mlDataDB.getFaceInfoForClustering(maxFaces: totalFaces,);final Set<int> missingFileIDs = {};@@ -304,7 +304,7 @@ class MLService {// Get the current cluster statisticsfinal Map<String, (Uint8List, int)> oldClusterSummaries =- await faceMLDB.getAllClusterSummary();+ await mlDataDB.getAllClusterSummary();if (clusterInBuckets) {const int bucketSize = 10000;@@ -363,9 +363,9 @@ class MLService {return;}- await faceMLDB+ await mlDataDB.updateFaceIdToClusterId(clusteringResult.newFaceIdToCluster);- await faceMLDB+ await mlDataDB.clusterSummaryUpdate(clusteringResult.newClusterSummaries);Bus.instance.fire(PeopleChangedEvent());for (final faceInfo in faceInfoForClustering) {@@ -408,9 +408,9 @@ class MLService {_logger.info('Updating ${clusteringResult.newFaceIdToCluster.length} FaceIDs with clusterIDs in the DB',);- await faceMLDB+ await mlDataDB.updateFaceIdToClusterId(clusteringResult.newFaceIdToCluster);- await faceMLDB+ await mlDataDB.clusterSummaryUpdate(clusteringResult.newClusterSummaries);Bus.instance.fire(PeopleChangedEvent());_logger.info('Done updating FaceIDs with clusterIDs in the DB, in '@@ -499,7 +499,7 @@ class MLService {);_logger.info("ML results for fileID ${result.fileId} stored on remote");// Storing results locally- if (result.facesRan) await faceMLDB.bulkInsertFaces(faces);+ if (result.facesRan) await mlDataDB.bulkInsertFaces(faces);if (result.clipRan) {await SemanticSearchService.instance.storeClipImageResult(result.clip!,@@ -522,7 +522,7 @@ class MLService {e,s,);- await faceMLDB.bulkInsertFaces(+ await mlDataDB.bulkInsertFaces([Face.empty(instruction.file.uploadedFileID!, error: true)],);await SemanticSearchService.instance.storeEmptyClipImageResult(diff --git a/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart b/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dartindex afeb23d4d0..df56796cd1 100644--- a/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart+++ b/mobile/lib/services/machine_learning/semantic_search/semantic_search_service.dart@@ -34,7 +34,7 @@ class SemanticSearchService {static final Computer _computer = Computer.shared();final LRUMap<String, List<double>> _queryEmbeddingCache = LRUMap(20);static const kMinimumSimilarityThreshold = 0.175;- late final faceMLDB = MLDataDB.instance;+ late final mlDataDB = MLDataDB.instance;bool _hasInitialized = false;bool _textModelIsLoaded = false;@@ -106,7 +106,7 @@ class SemanticSearchService {}Future<void> clearIndexes() async {- await faceMLDB.deleteClipIndexes();+ await mlDataDB.deleteClipIndexes();final preferences = await SharedPreferences.getInstance();await preferences.remove("sync_time_embeddings_v3");_logger.info("Indexes cleared");@@ -116,7 +116,7 @@ class SemanticSearchService {if (_cachedImageEmbeddingVectors != null) {return _cachedImageEmbeddingVectors!;}- _cachedImageEmbeddingVectors ??= faceMLDB.getAllClipVectors();+ _cachedImageEmbeddingVectors ??= mlDataDB.getAllClipVectors();_logger.info("read all embeddings from DB");return _cachedImageEmbeddingVectors!;@@ -181,7 +181,7 @@ class SemanticSearchService {_logger.info(results.length.toString() + " results");if (deletedEntries.isNotEmpty) {- unawaited(faceMLDB.deleteClipEmbeddings(deletedEntries));+ unawaited(mlDataDB.deleteClipEmbeddings(deletedEntries));}return results;@@ -221,12 +221,12 @@ class SemanticSearchService {embedding: clipResult.embedding,version: clipMlVersion,);- await faceMLDB.putClip([embedding]);+ await mlDataDB.putClip([embedding]);}Future<void> storeEmptyClipImageResult(EnteFile entefile) async {final embedding = ClipEmbedding.empty(entefile.uploadedFileID!);- await faceMLDB.putClip([embedding]);+ await mlDataDB.putClip([embedding]);}Future<List<double>> _getTextEmbedding(String query) async {diff --git a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dartindex 16f2510b47..d89ee548a1 100644--- a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart+++ b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart@@ -33,7 +33,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {Timer? _timer;bool isExpanded = false;final Logger logger = Logger("MLDebugSectionWidget");- late final faceMLDB = MLDataDB.instance;+ late final mlDataDB = MLDataDB.instance;@overridevoid initState() {super.initState();@@ -240,7 +240,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {sectionOptionSpacing,MenuItemWidget(captionedTextWidget: FutureBuilder<double>(- future: faceMLDB.getClusteredToIndexableFilesRatio(),+ future: mlDataDB.getClusteredToIndexableFilesRatio(),builder: (context, snapshot) {if (snapshot.hasData) {return CaptionedTextWidget(@@ -311,7 +311,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {trailingIcon: Icons.chevron_right_outlined,trailingIconIsMuted: true,onTap: () async {- final emptyFaces = await faceMLDB.getErroredFaceCount();+ final emptyFaces = await mlDataDB.getErroredFaceCount();showShortToast(context, '$emptyFaces empty faces');},),@@ -333,7 +333,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {firstButtonLabel: "Yes, confirm",firstButtonOnTap: () async {try {- await faceMLDB.dropFacesFeedbackTables();+ await mlDataDB.dropFacesFeedbackTables();Bus.instance.fire(PeopleChangedEvent());showShortToast(context, "Done");} catch (e, s) {@@ -366,7 +366,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {for (final PersonEntity p in persons) {await PersonService.instance.deletePerson(p.remoteID);}- await faceMLDB.dropClustersAndPersonTable();+ await mlDataDB.dropClustersAndPersonTable();Bus.instance.fire(PeopleChangedEvent());showShortToast(context, "Done");} catch (e, s) {@@ -394,7 +394,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {firstButtonLabel: "Yes, confirm",firstButtonOnTap: () async {try {- await faceMLDB.dropClustersAndPersonTable(faces: true);+ await mlDataDB.dropClustersAndPersonTable(faces: true);Bus.instance.fire(PeopleChangedEvent());showShortToast(context, "Done");} catch (e, s) {diff --git a/mobile/lib/ui/settings/ml/ml_user_dev_screen.dart b/mobile/lib/ui/settings/ml/ml_user_dev_screen.dartindex 730f7d8bf3..f295d4b202 100644--- a/mobile/lib/ui/settings/ml/ml_user_dev_screen.dart+++ b/mobile/lib/ui/settings/ml/ml_user_dev_screen.dart@@ -27,7 +27,7 @@ class MLUserDeveloperOptions extends StatefulWidget {}class _MLUserDeveloperOptionsState extends State<MLUserDeveloperOptions> {- late final IMLDataDB<int> faceMLDB = MLDataDB.instance;+ late final IMLDataDB<int> mlDataDB = MLDataDB.instance;@overrideWidget build(BuildContext context) {final colorScheme = getEnteColorScheme(context);@@ -122,9 +122,9 @@ class _MLUserDeveloperOptionsState extends State<MLUserDeveloperOptions> {Future<void> deleteEmptyIndices(BuildContext context) async {try {- final Set<int> emptyFileIDs = await faceMLDB.getErroredFileIDs();- await faceMLDB.deleteFaceIndexForFiles(emptyFileIDs.toList());- await faceMLDB.deleteClipEmbeddings(emptyFileIDs.toList());+ final Set<int> emptyFileIDs = await mlDataDB.getErroredFileIDs();+ await mlDataDB.deleteFaceIndexForFiles(emptyFileIDs.toList());+ await mlDataDB.deleteClipEmbeddings(emptyFileIDs.toList());showShortToast(context, "Deleted ${emptyFileIDs.length} entries");} catch (e) {// ignore: unawaited_futures@@ -137,7 +137,7 @@ class _MLUserDeveloperOptionsState extends State<MLUserDeveloperOptions> {Future<void> deleteAllLocalML(BuildContext context) async {try {- await faceMLDB.dropClustersAndPersonTable(faces: true);+ await mlDataDB.dropClustersAndPersonTable(faces: true);await SemanticSearchService.instance.clearIndexes();Bus.instance.fire(PeopleChangedEvent());showShortToast(context, "All local ML cleared");diff --git a/mobile/lib/ui/viewer/file_details/face_widget.dart b/mobile/lib/ui/viewer/file_details/face_widget.dartindex a72b5f8278..dc23851727 100644--- a/mobile/lib/ui/viewer/file_details/face_widget.dart+++ b/mobile/lib/ui/viewer/file_details/face_widget.dart@@ -138,7 +138,7 @@ class _FaceWidgetState extends State<FaceWidget> {);} else if (widget.clusterID != null) {final fileIdsToClusterIds =- await MLDataDB.instance.getFileIdToClusterIds();+ await mlDataDB.getFileIdToClusterIds();final files =await SearchService.instance.getAllFilesForSearch();final clusterFiles = filesdiff --git a/mobile/lib/utils/hierarchical_search_util.dart b/mobile/lib/utils/hierarchical_search_util.dartindex afc3df2711..a3254ef260 100644--- a/mobile/lib/utils/hierarchical_search_util.dart+++ b/mobile/lib/utils/hierarchical_search_util.dart@@ -31,7 +31,7 @@ Future<List<EnteFile>> getFilteredFiles(List<HierarchicalSearchFilter> filters,) async {final logger = Logger("HierarchicalSearchUtil");- final faceMLDB = MLDataDB.instance;+ final mlDataDB = MLDataDB.instance;late final List<EnteFile> filteredFiles;final files = await SearchService.instance.getAllFilesForHierarchicalSearch();final resultsNeverComputedFilters = <HierarchicalSearchFilter>[];@@ -43,12 +43,12 @@ Future<List<EnteFile>> getFilteredFiles(if (filter is FaceFilter && filter.matchedUploadedIDs.isEmpty) {try {if (filter.personId != null) {- final fileIDs = await faceMLDB.getFileIDsOfPersonID(+ final fileIDs = await mlDataDB.getFileIDsOfPersonID(filter.personId!,);filter.matchedUploadedIDs.addAll(fileIDs);} else if (filter.clusterId != null) {- final fileIDs = await faceMLDB.getFileIDsOfClusterID(+ final fileIDs = await mlDataDB.getFileIDsOfClusterID(filter.clusterId!,);filter.matchedUploadedIDs.addAll(fileIDs);@@ -81,19 +81,19 @@ Future<List<EnteFile>> getFilteredFiles(}}- await faceMLDB+ await mlDataDB.getPersonsClusterIDs(selectedPersonIDs).then((clusterIDs) {selectedClusterIDs.addAll(clusterIDs);});final fileIDsToAvoid =- await faceMLDB.getAllFilesAssociatedWithAllClusters(+ await mlDataDB.getAllFilesAssociatedWithAllClusters(exceptClusters: selectedClusterIDs,);final filesOfFaceIDsNotInAnyCluster =- await faceMLDB.getAllFileIDsOfFaceIDsNotInAnyCluster();+ await mlDataDB.getAllFileIDsOfFaceIDsNotInAnyCluster();fileIDsToAvoid.addAll(filesOfFaceIDsNotInAnyCluster);diff --git a/mobile/lib/utils/ml_util.dart b/mobile/lib/utils/ml_util.dartindex ccfe5ec4bc..06ba53c1d6 100644--- a/mobile/lib/utils/ml_util.dart+++ b/mobile/lib/utils/ml_util.dart@@ -55,10 +55,10 @@ class FileMLInstruction {Future<IndexStatus> getIndexStatus() async {try {- final faceMLDB = MLDataDB.instance;+ final mlDataDB = MLDataDB.instance;final int indexableFiles = (await getIndexableFileIDs()).length;- final int facesIndexedFiles = await faceMLDB.getFaceIndexedFileCount();- final int clipIndexedFiles = await faceMLDB.getClipIndexedFileCount();+ final int facesIndexedFiles = await mlDataDB.getFaceIndexedFileCount();+ final int clipIndexedFiles = await mlDataDB.getClipIndexedFileCount();final int indexedFiles = math.min(facesIndexedFiles, clipIndexedFiles);final showIndexedFiles = math.min(indexedFiles, indexableFiles);@@ -82,15 +82,15 @@ int _lastFetchTimeForOthersIndexed = 0;/// Return a list of file instructions for files that should be indexed for MLFuture<List<FileMLInstruction>> getFilesForMlIndexing() async {_logger.info('getFilesForMlIndexing called');- final faceMLDB = MLDataDB.instance;+ final mlDataDB = MLDataDB.instance;final time = DateTime.now();// Get indexed fileIDs for each ML service- final Map<int, int> faceIndexedFileIDs = await faceMLDB.faceIndexedFileIds();+ final Map<int, int> faceIndexedFileIDs = await mlDataDB.faceIndexedFileIds();final Map<int, int> clipIndexedFileIDs =- await faceMLDB.clipIndexedFileWithVersion();+ await mlDataDB.clipIndexedFileWithVersion();final Set<int> queuedFiledIDs = {};- final Set<int> filesWithFDStatus = await faceMLDB.getFileIDsWithFDData();+ final Set<int> filesWithFDStatus = await mlDataDB.getFileIDsWithFDData();// Get all regular files and all hidden filesfinal enteFiles = await SearchService.instance.getAllFilesForSearch();@@ -171,7 +171,7 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {}}_logger.info(- 'Chececking index for ${filesOwnedByOthers.length} owned by others',+ 'Checking index for ${filesOwnedByOthers.length} owned by others',);return [...splitResult.matched, ...filesOwnedByOthers];}@@ -183,7 +183,7 @@ Future<List<FileMLInstruction>> getFilesForMlIndexing() async {Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(int yieldSize,) async* {- final faceMLDB = MLDataDB.instance;+ final mlDataDB = MLDataDB.instance;final List<FileMLInstruction> filesToIndex = await getFilesForMlIndexing();final List<List<FileMLInstruction>> chunks =filesToIndex.chunks(embeddingFetchLimit);@@ -246,8 +246,8 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(}}}- await faceMLDB.bulkInsertFaces(faces);- await faceMLDB.putClip(clipEmbeddings);+ await mlDataDB.bulkInsertFaces(faces);+ await mlDataDB.putClip(clipEmbeddings);}// Yield any remaining instructionsif (batchToYield.isNotEmpty) {