Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Move Dart memories compute behind MemoriesComputer (#14296)

laurens-pilot43049afab520
diff --git a/mobile/apps/photos/lib/service_locator.dart b/mobile/apps/photos/lib/service_locator.dart
index f4dce5e7fe..8ecd745f98 100644
--- a/mobile/apps/photos/lib/service_locator.dart
+++ b/mobile/apps/photos/lib/service_locator.dart
@@ -35,11 +35,13 @@ import "package:photos/services/location_service.dart";
import "package:photos/services/machine_learning/compute_controller.dart";
import "package:photos/services/machine_learning/ml_decryption_record_store.dart";
import "package:photos/services/magic_cache_service.dart";
+import "package:photos/services/memories/dart_memories_computer.dart";
+import "package:photos/services/memories/memories_computer.dart";
+import "package:photos/services/memories/smart_memories_service.dart";
import "package:photos/services/memories_cache_service.dart";
import "package:photos/services/permission/service.dart";
import "package:photos/services/rituals/rituals_service.dart";
import "package:photos/services/smart_albums_service.dart";
-import "package:photos/services/smart_memories_service.dart";
import "package:photos/services/storage_bonus_service.dart";
import "package:photos/services/sync/trash_sync_service.dart";
import "package:photos/services/text_embeddings_cache_service.dart";
@@ -232,6 +234,12 @@ SmartMemoriesService get smartMemoriesService {
return _smartMemoriesService!;
}
+MemoriesComputer? _memoriesComputer;
+MemoriesComputer get memoriesComputer {
+ _memoriesComputer ??= DartMemoriesComputer();
+ return _memoriesComputer!;
+}
+
TextEmbeddingsCacheService? _textEmbeddingsCacheService;
TextEmbeddingsCacheService get textEmbeddingsCacheService {
_textEmbeddingsCacheService ??= TextEmbeddingsCacheService.instance;
diff --git a/mobile/apps/photos/lib/services/home_widget_service.dart b/mobile/apps/photos/lib/services/home_widget_service.dart
index 1e6e3bb50d..1ae9da829d 100644
--- a/mobile/apps/photos/lib/services/home_widget_service.dart
+++ b/mobile/apps/photos/lib/services/home_widget_service.dart
@@ -12,9 +12,9 @@ import 'package:photos/module/download/thumbnail.dart';
import 'package:photos/service_locator.dart';
import 'package:photos/services/album_home_widget_service.dart';
import 'package:photos/services/app_navigation_service.dart';
+import 'package:photos/services/memories/smart_memories_service.dart';
import 'package:photos/services/memory_home_widget_service.dart';
import 'package:photos/services/people_home_widget_service.dart';
-import 'package:photos/services/smart_memories_service.dart';
import 'package:photos/ui/settings/ml/ml_consent_sheet.dart';
import 'package:photos/ui/settings/widgets/albums_widget_settings.dart';
import 'package:photos/ui/settings/widgets/memories_widget_settings.dart';
diff --git a/mobile/apps/photos/lib/services/smart_memories_service.dart b/mobile/apps/photos/lib/services/memories/dart_memories_computer.dart
similarity index 88%
rename from mobile/apps/photos/lib/services/smart_memories_service.dart
rename to mobile/apps/photos/lib/services/memories/dart_memories_computer.dart
index 4c2d53cadd..64b7a7a2a8 100644
--- a/mobile/apps/photos/lib/services/smart_memories_service.dart
+++ b/mobile/apps/photos/lib/services/memories/dart_memories_computer.dart
@@ -4,10 +4,7 @@ import "dart:math" show Random, max, min;
import "package:computer/computer.dart";
import "package:ente_pure_utils/ente_pure_utils.dart";
-import "package:ente_strings/ente_strings.dart";
import "package:flutter/foundation.dart" show kDebugMode, visibleForTesting;
-import "package:flutter/material.dart";
-import "package:intl/intl.dart";
import "package:logging/logging.dart";
import "package:ml_linalg/vector.dart";
import "package:photos/core/configuration.dart";
@@ -17,7 +14,6 @@ import "package:photos/db/memories_db.dart";
import "package:photos/db/ml/base.dart";
import "package:photos/db/ml/db.dart";
import "package:photos/db/offline_files_db.dart";
-import "package:photos/locale.dart";
import "package:photos/models/base_location.dart";
import "package:photos/models/file/file.dart";
import "package:photos/models/location/location.dart";
@@ -37,34 +33,20 @@ import "package:photos/models/ml/face/person.dart";
import "package:photos/models/ml/vector.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/collections_service.dart";
-import "package:photos/services/language_service.dart";
import "package:photos/services/location_service.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
+import "package:photos/services/machine_learning/ml_model_download_service.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/memories/memories_computation_context.dart";
+import "package:photos/services/memories/memories_computer.dart";
import "package:photos/services/memories/photo_selector.dart";
import "package:photos/services/search_service.dart";
-part "smart_memories_clip_calculator.dart";
-part "memories/memory_file_index.dart";
-part "smart_memories_people_calculator.dart";
-part "smart_memories_time_calculator.dart";
-part "smart_memories_trip_calculator_v2.dart";
-
-class MemoriesResult {
- final List<SmartMemory> memories;
- final List<BaseLocation> baseLocations;
- final bool failed;
-
- MemoriesResult(this.memories, this.baseLocations, {this.failed = false});
-
- MemoriesResult.failed()
- : memories = const <SmartMemory>[],
- baseLocations = const <BaseLocation>[],
- failed = true;
-
- bool get isEmpty => memories.isEmpty;
-}
+part "../smart_memories_clip_calculator.dart";
+part "memory_file_index.dart";
+part "../smart_memories_people_calculator.dart";
+part "../smart_memories_time_calculator.dart";
+part "../smart_memories_trip_calculator_v2.dart";
typedef _PreparedMemoriesData = ({
List<PersonEntity> persons,
@@ -74,8 +56,8 @@ typedef _PreparedMemoriesData = ({
TripMemoriesAnalysis tripAnalysis,
});
-class SmartMemoriesService {
- final _logger = Logger("SmartMemoriesService");
+class DartMemoriesComputer implements MemoriesComputer {
+ final _logger = Logger("DartMemoriesComputer");
MemoriesDB get _memoriesDB => isLocalGalleryMode
? MemoriesDB.localGalleryInstance
: MemoriesDB.instance;
@@ -98,7 +80,33 @@ class SmartMemoriesService {
static const _unnamedClusterPersonIDPrefix = "cluster:";
static const _debugForceUnnamedClustersOnly = false;
- SmartMemoriesService();
+ bool get _mlEnabled =>
+ hasGrantedMLConsent && localSettings.isMLLocalIndexingEnabled;
+
+ @override
+ Future<bool> isMlReady() async {
+ if (!_mlEnabled) return false;
+ try {
+ final mlDataDB = isLocalGalleryMode
+ ? MLDataDB.localGalleryInstance
+ : MLDataDB.instance;
+ final clipIndexed = await mlDataDB.getClipIndexedFileCount();
+ if (clipIndexed < minimumMemoryLength) {
+ return false;
+ }
+ if (!await MLModelDownloadService.instance.canLoadClipTextModel()) {
+ _logger.info(
+ "ML not ready for smart memories because CLIP text model is not "
+ "cached and high bandwidth connectivity is unavailable",
+ );
+ return false;
+ }
+ return true;
+ } catch (e, s) {
+ _logger.warning("Failed to determine ML readiness", e, s);
+ return false;
+ }
+ }
Future<
({
@@ -144,156 +152,68 @@ class SmartMemoriesService {
);
}
- Future<MemoriesResult> calcSmartMemories(
+ @override
+ Future<MemoriesResult> computeAll(
DateTime now,
MemoriesCache oldCache, {
- bool debugSurfaceAll = false,
required bool mlEnabled,
}) async {
- try {
- final TimeLogger t = TimeLogger(context: "calcMemories");
- _logger.info(
- 'calcMemories called with time: $now at ${DateTime.now()} '
- '(mlEnabled: $mlEnabled) $t',
- );
- final computationContext = await _loadComputationContext(
- now,
- oldCache,
- debugSurfaceAll: debugSurfaceAll,
- mlEnabled: mlEnabled,
- timeLogger: t,
- );
-
- final local = await getLocale();
- final languageCode = local?.languageCode ?? "en";
- final s = await LanguageService.locals;
-
- _logger.info('get locale and S $t');
- final memoriesResult =
- await Computer.shared().compute(
- _allMemoriesCalculations,
- param: computationContext.toIsolateArgs(),
- )
- as MemoriesResult;
- _logger.info(
- '${memoriesResult.memories.length} memories computed in computer $t',
- );
- return await _finalizeMemoriesResult(memoriesResult, s, languageCode, t);
- } catch (e, s) {
- _logger.severe("Error calculating smart memories", e, s);
- return _fallbackAfterCalculationError();
- }
+ final TimeLogger t = TimeLogger(context: "calcMemories");
+ _logger.info(
+ 'calcMemories called with time: $now at ${DateTime.now()} '
+ '(mlEnabled: $mlEnabled) $t',
+ );
+ final computationContext = await _loadComputationContext(
+ now,
+ oldCache,
+ debugSurfaceAll: true,
+ mlEnabled: mlEnabled,
+ timeLogger: t,
+ );
+ final memoriesResult =
+ await Computer.shared().compute(
+ _allMemoriesCalculations,
+ param: computationContext.toIsolateArgs(),
+ )
+ as MemoriesResult;
+ _logger.info(
+ '${memoriesResult.memories.length} memories computed in computer $t',
+ );
+ return memoriesResult;
}
- Future<({MemoriesResult current, MemoriesResult next})>
- calcCurrentAndNextSmartMemories(
+ @override
+ Future<({MemoriesResult current, MemoriesResult next})> computeCurrentAndNext(
DateTime current,
DateTime next,
MemoriesCache oldCache, {
required bool mlEnabled,
}) async {
- try {
- final TimeLogger t = TimeLogger(context: "calcCurrentAndNextMemories");
- _logger.info(
- 'calcCurrentAndNextMemories called with times: $current and $next at '
- '${DateTime.now()} (mlEnabled: $mlEnabled) $t',
- );
- final computationContext = await _loadComputationContext(
- current,
- oldCache,
- debugSurfaceAll: false,
- mlEnabled: mlEnabled,
- timeLogger: t,
- );
-
- final local = await getLocale();
- final languageCode = local?.languageCode ?? "en";
- final s = await LanguageService.locals;
- _logger.info('get locale and S $t');
-
- final args = computationContext.toIsolateArgs()..["next"] = next;
- final results =
- (await Computer.shared().compute(
- _allMemoriesCalculationsForPeriods,
- param: args,
- )
- as List)
- .cast<MemoriesResult>();
- final currentResult = await _finalizeMemoriesResult(
- results[0],
- s,
- languageCode,
- t,
- );
- final nextResult = await _finalizeMemoriesResult(
- results[1],
- s,
- languageCode,
- t,
- );
- _logger.info(
- '${currentResult.memories.length} current and '
- '${nextResult.memories.length} next memories computed in computer $t',
- );
- return (current: currentResult, next: nextResult);
- } catch (e, s) {
- _logger.severe("Error calculating current and next smart memories", e, s);
- final currentResult = await _fallbackAfterCalculationError();
- if (currentResult.failed) {
- return (current: currentResult, next: MemoriesResult.failed());
- }
- return (
- current: currentResult,
- next: await _fallbackAfterCalculationError(),
- );
- }
- }
-
- Future<MemoriesResult> _finalizeMemoriesResult(
- MemoriesResult memoriesResult,
- StringsLocalizations s,
- String languageCode,
- TimeLogger t,
- ) async {
- try {
- if (isLocalGalleryMode && memoriesResult.isEmpty) {
- _logger.severe(
- "Smart memories returned empty in local gallery mode, falling back to simple memories",
- );
- final fallbackMemories = await calcSimpleMemories();
- return MemoriesResult(fallbackMemories, <BaseLocation>[]);
- }
- if (memoriesResult.failed) {
- return memoriesResult;
- }
- for (final memory in memoriesResult.memories) {
- memory.title = memory.createTitle(s, languageCode);
- }
- _logger.info('titles created for all memories $t');
- return memoriesResult;
- } catch (e, s) {
- _logger.severe("Error finalizing smart memories", e, s);
- return _fallbackAfterCalculationError();
- }
- }
-
- Future<MemoriesResult> _fallbackAfterCalculationError() async {
- if (isLocalGalleryMode) {
- try {
- _logger.warning(
- "Falling back to simple memories after smart memories failure in local gallery mode",
- );
- final fallbackMemories = await calcSimpleMemories();
- return MemoriesResult(fallbackMemories, <BaseLocation>[]);
- } catch (fallbackError, fallbackStackTrace) {
- _logger.severe(
- "Offline fallback to simple memories failed",
- fallbackError,
- fallbackStackTrace,
- );
- }
- }
- return MemoriesResult.failed();
+ final TimeLogger t = TimeLogger(context: "calcCurrentAndNextMemories");
+ _logger.info(
+ 'calcCurrentAndNextMemories called with times: $current and $next at '
+ '${DateTime.now()} (mlEnabled: $mlEnabled) $t',
+ );
+ final computationContext = await _loadComputationContext(
+ current,
+ oldCache,
+ debugSurfaceAll: false,
+ mlEnabled: mlEnabled,
+ timeLogger: t,
+ );
+ final args = computationContext.toIsolateArgs()..["next"] = next;
+ final results =
+ (await Computer.shared().compute(
+ _allMemoriesCalculationsForPeriods,
+ param: args,
+ )
+ as List)
+ .cast<MemoriesResult>();
+ _logger.info(
+ '${results[0].memories.length} current and '
+ '${results[1].memories.length} next memories computed in computer $t',
+ );
+ return (current: results[0], next: results[1]);
}
Future<MemoriesComputationContext> _loadComputationContext(
@@ -1119,7 +1039,8 @@ class SmartMemoriesService {
}
}
- Future<List<SmartMemory>> calcSimpleMemories() async {
+ @override
+ Future<List<SmartMemory>> computeSimple() async {
final now = DateTime.now();
final allFileIdsToFile = await _getFilesAndMapForMemories(
useLocalIntIds: isLocalGalleryMode,
@@ -1167,15 +1088,6 @@ class SmartMemoriesService {
localIdToIntId: localIdToIntId,
);
memories.addAll(fillerMemories);
-
- final local = await getLocale();
- final languageCode = local?.languageCode ?? "en";
- final s = await LanguageService.locals;
-
- _logger.info('get locale and S');
- for (final memory in memories) {
- memory.title = memory.createTitle(s, languageCode);
- }
return memories;
}
@@ -1419,29 +1331,6 @@ class SmartMemoriesService {
);
}
- static Future<String> getDateFormattedLocale({
- required int creationTime,
- }) async {
- final locale = await getLocale();
-
- return getDateFormatted(
- creationTime: creationTime,
- languageCode: locale!.languageCode,
- );
- }
-
- static String getDateFormatted({
- required int creationTime,
- BuildContext? context,
- String? languageCode,
- }) {
- return DateFormat.yMMMd(
- context != null
- ? Localizations.localeOf(context).languageCode
- : languageCode ?? "en",
- ).format(DateTime.fromMicrosecondsSinceEpoch(creationTime));
- }
-
static int? _seenTimeKeyForFile(
EnteFile file,
Map<String, int>? localIdToIntId,
diff --git a/mobile/apps/photos/lib/services/memories/memories_computer.dart b/mobile/apps/photos/lib/services/memories/memories_computer.dart
new file mode 100644
index 0000000000..946f8e05e1
--- /dev/null
+++ b/mobile/apps/photos/lib/services/memories/memories_computer.dart
@@ -0,0 +1,37 @@
+import "package:photos/models/base_location.dart";
+import "package:photos/models/memories/memories_cache.dart";
+import "package:photos/models/memories/smart_memory.dart";
+
+class MemoriesResult {
+ final List<SmartMemory> memories;
+ final List<BaseLocation> baseLocations;
+ final bool failed;
+
+ MemoriesResult(this.memories, this.baseLocations, {this.failed = false});
+
+ MemoriesResult.failed()
+ : memories = const <SmartMemory>[],
+ baseLocations = const <BaseLocation>[],
+ failed = true;
+
+ bool get isEmpty => memories.isEmpty;
+}
+
+abstract interface class MemoriesComputer {
+ Future<bool> isMlReady();
+
+ Future<({MemoriesResult current, MemoriesResult next})> computeCurrentAndNext(
+ DateTime current,
+ DateTime next,
+ MemoriesCache oldCache, {
+ required bool mlEnabled,
+ });
+
+ Future<MemoriesResult> computeAll(
+ DateTime now,
+ MemoriesCache oldCache, {
+ required bool mlEnabled,
+ });
+
+ Future<List<SmartMemory>> computeSimple();
+}
diff --git a/mobile/apps/photos/lib/services/memories/memory_file_index.dart b/mobile/apps/photos/lib/services/memories/memory_file_index.dart
index b0eb6ee49c..b3f0d5e7eb 100644
--- a/mobile/apps/photos/lib/services/memories/memory_file_index.dart
+++ b/mobile/apps/photos/lib/services/memories/memory_file_index.dart
@@ -1,4 +1,4 @@
-part of "../smart_memories_service.dart";
+part of "dart_memories_computer.dart";
typedef _CreationTimeRange = ({int start, int end});
diff --git a/mobile/apps/photos/lib/services/memories/smart_memories_service.dart b/mobile/apps/photos/lib/services/memories/smart_memories_service.dart
new file mode 100644
index 0000000000..670863fc0f
--- /dev/null
+++ b/mobile/apps/photos/lib/services/memories/smart_memories_service.dart
@@ -0,0 +1,170 @@
+import "package:ente_pure_utils/ente_pure_utils.dart";
+import "package:ente_strings/ente_strings.dart";
+import "package:flutter/material.dart";
+import "package:intl/intl.dart";
+import "package:logging/logging.dart";
+import "package:photos/locale.dart";
+import "package:photos/models/base_location.dart";
+import "package:photos/models/memories/memories_cache.dart";
+import "package:photos/models/memories/smart_memory.dart";
+import "package:photos/service_locator.dart";
+import "package:photos/services/language_service.dart";
+import "package:photos/services/memories/memories_computer.dart";
+
+class SmartMemoriesService {
+ final _logger = Logger("SmartMemoriesService");
+
+ SmartMemoriesService();
+
+ Future<MemoriesResult> calcAllSmartMemories(
+ DateTime now,
+ MemoriesCache oldCache, {
+ required bool mlEnabled,
+ }) async {
+ try {
+ final TimeLogger t = TimeLogger(context: "calcAllSmartMemories");
+ final memoriesResult = await memoriesComputer.computeAll(
+ now,
+ oldCache,
+ mlEnabled: mlEnabled,
+ );
+ final local = await getLocale();
+ final languageCode = local?.languageCode ?? "en";
+ final s = await LanguageService.locals;
+ _logger.info('get locale and S $t');
+ return await _finalizeMemoriesResult(memoriesResult, s, languageCode, t);
+ } catch (e, s) {
+ _logger.severe("Error calculating smart memories", e, s);
+ return _fallbackAfterCalculationError();
+ }
+ }
+
+ Future<({MemoriesResult current, MemoriesResult next})>
+ calcCurrentAndNextSmartMemories(
+ DateTime current,
+ DateTime next,
+ MemoriesCache oldCache, {
+ required bool mlEnabled,
+ }) async {
+ try {
+ final TimeLogger t = TimeLogger(
+ context: "calcCurrentAndNextSmartMemories",
+ );
+ final results = await memoriesComputer.computeCurrentAndNext(
+ current,
+ next,
+ oldCache,
+ mlEnabled: mlEnabled,
+ );
+ final local = await getLocale();
+ final languageCode = local?.languageCode ?? "en";
+ final s = await LanguageService.locals;
+ _logger.info('get locale and S $t');
+ final currentResult = await _finalizeMemoriesResult(
+ results.current,
+ s,
+ languageCode,
+ t,
+ );
+ final nextResult = await _finalizeMemoriesResult(
+ results.next,
+ s,
+ languageCode,
+ t,
+ );
+ return (current: currentResult, next: nextResult);
+ } catch (e, s) {
+ _logger.severe("Error calculating current and next smart memories", e, s);
+ final currentResult = await _fallbackAfterCalculationError();
+ if (currentResult.failed) {
+ return (current: currentResult, next: MemoriesResult.failed());
+ }
+ return (
+ current: currentResult,
+ next: await _fallbackAfterCalculationError(),
+ );
+ }
+ }
+
+ Future<MemoriesResult> _finalizeMemoriesResult(
+ MemoriesResult memoriesResult,
+ StringsLocalizations s,
+ String languageCode,
+ TimeLogger t,
+ ) async {
+ try {
+ if (isLocalGalleryMode && memoriesResult.isEmpty) {
+ _logger.severe(
+ "Smart memories returned empty in local gallery mode, falling back to simple memories",
+ );
+ final fallbackMemories = await calcSimpleMemories();
+ return MemoriesResult(fallbackMemories, <BaseLocation>[]);
+ }
+ if (memoriesResult.failed) {
+ return memoriesResult;
+ }
+ for (final memory in memoriesResult.memories) {
+ memory.title = memory.createTitle(s, languageCode);
+ }
+ _logger.info('titles created for all memories $t');
+ return memoriesResult;
+ } catch (e, s) {
+ _logger.severe("Error finalizing smart memories", e, s);
+ return _fallbackAfterCalculationError();
+ }
+ }
+
+ Future<MemoriesResult> _fallbackAfterCalculationError() async {
+ if (isLocalGalleryMode) {
+ try {
+ _logger.warning(
+ "Falling back to simple memories after smart memories failure in local gallery mode",
+ );
+ final fallbackMemories = await calcSimpleMemories();
+ return MemoriesResult(fallbackMemories, <BaseLocation>[]);
+ } catch (fallbackError, fallbackStackTrace) {
+ _logger.severe(
+ "Offline fallback to simple memories failed",
+ fallbackError,
+ fallbackStackTrace,
+ );
+ }
+ }
+ return MemoriesResult.failed();
+ }
+
+ Future<List<SmartMemory>> calcSimpleMemories() async {
+ final memories = await memoriesComputer.computeSimple();
+ final local = await getLocale();
+ final languageCode = local?.languageCode ?? "en";
+ final s = await LanguageService.locals;
+ _logger.info('get locale and S');
+ for (final memory in memories) {
+ memory.title = memory.createTitle(s, languageCode);
+ }
+ return memories;
+ }
+
+ static Future<String> getDateFormattedLocale({
+ required int creationTime,
+ }) async {
+ final locale = await getLocale();
+
+ return getDateFormatted(
+ creationTime: creationTime,
+ languageCode: locale!.languageCode,
+ );
+ }
+
+ static String getDateFormatted({
+ required int creationTime,
+ BuildContext? context,
+ String? languageCode,
+ }) {
+ return DateFormat.yMMMd(
+ context != null
+ ? Localizations.localeOf(context).languageCode
+ : languageCode ?? "en",
+ ).format(DateTime.fromMicrosecondsSinceEpoch(creationTime));
+ }
+}
diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dart
index 428d3f132f..5fee72c206 100644
--- a/mobile/apps/photos/lib/services/memories_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart
@@ -9,7 +9,6 @@ import "package:path_provider/path_provider.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/db/files_db.dart";
import "package:photos/db/memories_db.dart";
-import "package:photos/db/ml/db.dart";
import "package:photos/db/offline_files_db.dart";
import "package:photos/events/diff_sync_complete_event.dart";
import "package:photos/events/files_updated_event.dart";
@@ -31,12 +30,10 @@ import "package:photos/services/app_lifecycle_service.dart";
import "package:photos/services/app_navigation_service.dart";
import "package:photos/services/language_service.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
-import "package:photos/services/machine_learning/ml_model_download_service.dart";
import "package:photos/services/machine_learning/ml_run_control.dart";
import "package:photos/services/memories/photo_selector.dart";
import "package:photos/services/notification_service.dart";
import "package:photos/services/search_service.dart";
-import "package:photos/services/smart_memories_service.dart";
import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/theme/colors.dart";
import "package:photos/ui/home/memories/all_memories_page.dart";
@@ -180,33 +177,6 @@ class MemoriesCacheService {
bool get curatedMemoriesOption => showAnyMemories;
- bool get _mlEnabled =>
- hasGrantedMLConsent && localSettings.isMLLocalIndexingEnabled;
-
- Future<bool> _isMlReady() async {
- if (!_mlEnabled) return false;
- try {
- final mlDataDB = isLocalGalleryMode
- ? MLDataDB.localGalleryInstance
- : MLDataDB.instance;
- final clipIndexed = await mlDataDB.getClipIndexedFileCount();
- if (clipIndexed < SmartMemoriesService.minimumMemoryLength) {
- return false;
- }
- if (!await MLModelDownloadService.instance.canLoadClipTextModel()) {
- _logger.info(
- "ML not ready for smart memories because CLIP text model is not "
- "cached and high bandwidth connectivity is unavailable",
- );
- return false;
- }
- return true;
- } catch (e, s) {
- _logger.warning("Failed to determine ML readiness", e, s);
- return false;
- }
- }
-
void _checkIfTimeToUpdateCache() {
if (!enableSmartMemories) {
return;
@@ -913,7 +883,7 @@ class MemoriesCacheService {
final oldCache = await _readCacheFromDisk();
await _backfillInitialMemoriesNotification(_processOldCache(oldCache));
if (localSettings.initialMemoriesNotificationScheduledAt() != null ||
- !await _isMlReady()) {
+ !await memoriesComputer.isMlReady()) {
return false;
}
final indexStatus = await getIndexStatus();
@@ -1006,7 +976,7 @@ class MemoriesCacheService {
await _backfillInitialMemoriesNotification(newCache);
final now = DateTime.now();
final next = now.add(kMemoriesUpdateFrequency);
- final mlReady = await _isMlReady();
+ final mlReady = await memoriesComputer.isMlReady();
final periodResults = await smartMemoriesService
.calcCurrentAndNextSmartMemories(
now,
@@ -1288,8 +1258,8 @@ class MemoriesCacheService {
Future<List<SmartMemory>> debugGetAllMemories({DateTime? calcTime}) async {
return _memoriesUpdateLock.synchronized(() async {
- final mlReady = await _isMlReady();
- final result = await smartMemoriesService.calcSmartMemories(
+ final mlReady = await memoriesComputer.isMlReady();
+ final result = await smartMemoriesService.calcAllSmartMemories(
calcTime ?? DateTime.now(),
MemoriesCache(
toShowMemories: [],
@@ -1298,7 +1268,6 @@ class MemoriesCacheService {
tripsShownLogs: [],
baseLocations: [],
),
- debugSurfaceAll: true,
mlEnabled: mlReady,
);
locationService.baseLocations = result.baseLocations;
diff --git a/mobile/apps/photos/lib/services/search_service.dart b/mobile/apps/photos/lib/services/search_service.dart
index 93b284ecf0..e3dc4d2072 100644
--- a/mobile/apps/photos/lib/services/search_service.dart
+++ b/mobile/apps/photos/lib/services/search_service.dart
@@ -1905,10 +1905,9 @@ class SearchService {
if (pickedTime != null) calcTime = pickedTime;
final cache = await memoriesCacheService.debugCacheForTesting();
- final memoriesResult = await smartMemoriesService.calcSmartMemories(
+ final memoriesResult = await smartMemoriesService.calcAllSmartMemories(
calcTime,
cache,
- debugSurfaceAll: true,
mlEnabled:
hasGrantedMLConsent && localSettings.isMLLocalIndexingEnabled,
);
diff --git a/mobile/apps/photos/lib/services/smart_memories_clip_calculator.dart b/mobile/apps/photos/lib/services/smart_memories_clip_calculator.dart
index 94d691d4c4..6abf6598d7 100644
--- a/mobile/apps/photos/lib/services/smart_memories_clip_calculator.dart
+++ b/mobile/apps/photos/lib/services/smart_memories_clip_calculator.dart
@@ -1,4 +1,4 @@
-part of "smart_memories_service.dart";
+part of "memories/dart_memories_computer.dart";
class ClipMemoriesCalculator {
static Future<List<ClipMemory>> compute(
@@ -28,7 +28,7 @@ class ClipMemoriesCalculator {
}
final scoredFiles = <({EnteFile file, int fileID, double similarity})>[];
for (final file in allFiles) {
- final fileID = SmartMemoriesService._memoryFileId(
+ final fileID = DartMemoriesComputer._memoryFileId(
file,
isLocalGalleryMode: isLocalGalleryMode,
);
@@ -36,7 +36,7 @@ class ClipMemoriesCalculator {
final embedding = fileIDToImageEmbedding[fileID];
if (embedding == null) continue;
final similarity = embedding.vector.dot(activityVector);
- if (similarity > SmartMemoriesService._clipMemoryTypeQueryThreshold) {
+ if (similarity > DartMemoriesComputer._clipMemoryTypeQueryThreshold) {
scoredFiles.add((file: file, fileID: fileID, similarity: similarity));
}
}
@@ -53,14 +53,14 @@ class ClipMemoriesCalculator {
for (final candidate in topCandidates) {
if (selected.length >= 10) break;
final creationTime = candidate.file.creationTime;
- if (SmartMemoriesService._isTooCloseInTime(
+ if (DartMemoriesComputer._isTooCloseInTime(
creationTime,
selectedCreationTimes,
)) {
skipped++;
continue;
}
- if (SmartMemoriesService._isNearDuplicate(
+ if (DartMemoriesComputer._isNearDuplicate(
candidate.fileID,
selectedFileIDs,
fileIDToImageEmbedding,
diff --git a/mobile/apps/photos/lib/services/smart_memories_people_calculator.dart b/mobile/apps/photos/lib/services/smart_memories_people_calculator.dart
index 0cece7da0d..45b17c2763 100644
--- a/mobile/apps/photos/lib/services/smart_memories_people_calculator.dart
+++ b/mobile/apps/photos/lib/services/smart_memories_people_calculator.dart
@@ -1,4 +1,4 @@
-part of "smart_memories_service.dart";
+part of "memories/dart_memories_computer.dart";
class PeopleMemoriesCalculator {
static Future<List<PeopleMemory>> compute(
@@ -71,7 +71,7 @@ class PeopleMemoriesCalculator {
if (isMeAssigned) meFileIDs = personIdToFileIDs[meID]!;
Future<List<Memory>> selectPeopleMemories(List<Memory> memories) {
- return SmartMemoriesService._bestSelectionPeople(
+ return DartMemoriesComputer._bestSelectionPeople(
memories,
isLocalGalleryMode: isLocalGalleryMode,
fileIDToImageEmbedding: fileIDToImageEmbedding,
@@ -80,7 +80,7 @@ class PeopleMemoriesCalculator {
}
final unnamedClusterCandidates =
- SmartMemoriesService._buildUnnamedClusterCandidates(
+ DartMemoriesComputer._buildUnnamedClusterCandidates(
clusterIdToFaceCount: clusterIdToFaceCount,
clusterIdToFaceIDs: clusterIdToFaceIDs,
assignedClusterIDs: assignedClusterIDs,
@@ -95,7 +95,7 @@ class PeopleMemoriesCalculator {
selectionBuilder: selectPeopleMemories,
);
final randomizedUnnamedClusterCandidates =
- SmartMemoriesService._orderUnnamedCandidatesByRecencyAndRandom(
+ DartMemoriesComputer._orderUnnamedCandidatesByRecencyAndRandom(
candidates: unnamedClusterCandidates,
shownPeople: shownPeople,
currentTime: currentTime,
@@ -103,7 +103,7 @@ class PeopleMemoriesCalculator {
);
w?.log('unnamed cluster candidates setup');
- if (kDebugMode && SmartMemoriesService._debugForceUnnamedClustersOnly) {
+ if (kDebugMode && DartMemoriesComputer._debugForceUnnamedClustersOnly) {
for (final candidate in randomizedUnnamedClusterCandidates) {
final memory = await candidate.realize();
if (memory == null) continue;
@@ -130,7 +130,7 @@ class PeopleMemoriesCalculator {
spotlightFiles.add(file);
}
}
- if (spotlightFiles.length > SmartMemoriesService.minimumMemoryLength) {
+ if (spotlightFiles.length > DartMemoriesComputer.minimumMemoryLength) {
final spotlightMemories = spotlightFiles
.map((f) => Memory.fromFile(f, seenTimes))
.toList(growable: false);
@@ -165,7 +165,7 @@ class PeopleMemoriesCalculator {
youAndThemFiles.add(file);
}
}
- if (youAndThemFiles.length > SmartMemoriesService.minimumMemoryLength) {
+ if (youAndThemFiles.length > DartMemoriesComputer.minimumMemoryLength) {
final youAndThemMemories = youAndThemFiles
.map((f) => Memory.fromFile(f, seenTimes))
.toList(growable: false);
@@ -191,7 +191,7 @@ class PeopleMemoriesCalculator {
}
if (isMeAssigned && meID != personID) {
- final vectors = SmartMemoriesService._getEmbeddingsForFileIDs(
+ final vectors = DartMemoriesComputer._getEmbeddingsForFileIDs(
fileIDToImageEmbedding,
personFileIDs,
);
@@ -216,14 +216,14 @@ class PeopleMemoriesCalculator {
for (final fileID in personFileIDs) {
final similarity = similarities[fileID];
if (similarity == null) continue;
- if (similarity > SmartMemoriesService._clipActivityQueryThreshold) {
+ if (similarity > DartMemoriesComputer._clipActivityQueryThreshold) {
final file = allFileIdsToFile[fileID];
if (file != null) {
activityFiles.add(file);
}
}
}
- if (activityFiles.length > SmartMemoriesService.minimumMemoryLength) {
+ if (activityFiles.length > DartMemoriesComputer.minimumMemoryLength) {
final activityMemories = activityFiles
.map((f) => Memory.fromFile(f, seenTimes))
.toList(growable: false);
@@ -292,14 +292,14 @@ class PeopleMemoriesCalculator {
.map((f) => Memory.fromFile(f, seenTimes))
.toList();
final filteredLastTimeMemories =
- SmartMemoriesService._filterNearDuplicates(
+ DartMemoriesComputer._filterNearDuplicates(
lastTimeMemories,
fileIDToImageEmbedding,
minKeep: 2,
isLocalGalleryMode: isLocalGalleryMode,
);
final spacedLastTimeMemories =
- SmartMemoriesService._filterByTimeSpacing(filteredLastTimeMemories);
+ DartMemoriesComputer._filterByTimeSpacing(filteredLastTimeMemories);
final lastTimeList = personToCandidates
.putIfAbsent(personID, () => {})
.putIfAbsent(
@@ -532,7 +532,7 @@ class PeopleMemoriesCalculator {
if (alreadyInResults) {
continue;
}
- if (SmartMemoriesService._wasPersonShownRecently(
+ if (DartMemoriesComputer._wasPersonShownRecently(
personID: candidatePersonID,
shownPeople: shownPeople,
currentTime: currentTime,
@@ -543,7 +543,7 @@ class PeopleMemoriesCalculator {
eligibleUnnamedCandidates.add(candidate);
}
final orderedEligibleUnnamedCandidates =
- SmartMemoriesService._orderUnnamedCandidatesByRecencyAndRandom(
+ DartMemoriesComputer._orderUnnamedCandidatesByRecencyAndRandom(
candidates: eligibleUnnamedCandidates,
shownPeople: shownPeople,
currentTime: currentTime,
diff --git a/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart b/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart
index 2b4dfb182c..d13eb3f481 100644
--- a/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart
+++ b/mobile/apps/photos/lib/services/smart_memories_time_calculator.dart
@@ -1,4 +1,4 @@
-part of "smart_memories_service.dart";
+part of "memories/dart_memories_computer.dart";
class TimeMemoriesCalculator {
static const _recentTimeMemorySelectionSize = 10;
@@ -125,7 +125,7 @@ class TimeMemoriesCalculator {
if (significantDays.length >= 3) {
final titleDate = DateTime(significantDays.first, month, day);
final allPhotos = yearGroups.values.expand((x) => x).toList();
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
allPhotos,
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -147,7 +147,7 @@ class TimeMemoriesCalculator {
for (final year in significantDays) {
final date = DateTime(year, month, day);
final files = yearGroups[year]!;
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
files,
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -179,7 +179,7 @@ class TimeMemoriesCalculator {
final allPhotos = currentWeekYearGroups.values
.expand((x) => x)
.toList();
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
allPhotos,
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -203,7 +203,7 @@ class TimeMemoriesCalculator {
1,
).add(Duration(days: (currentWeek - 1) * 7));
final files = currentWeekYearGroups[year]!;
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
files,
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -230,14 +230,14 @@ class TimeMemoriesCalculator {
const monthSelectionSize = 20;
final startOfNextMonth = DateTime(currentYear, currentMonth + 1);
final historicalMemoryFileIds = <int>{};
- SmartMemoriesService._markUsedMemories(
+ DartMemoriesComputer._markUsedMemories(
historicalMemoryFileIds,
historicalMemoryResult,
isLocalGalleryMode: isLocalGalleryMode,
);
currentMonthYearGroups.removeWhere((_, memories) {
memories.removeWhere((memory) {
- final fileId = SmartMemoriesService._memoryFileIdFromMemory(
+ final fileId = DartMemoriesComputer._memoryFileIdFromMemory(
memory,
isLocalGalleryMode: isLocalGalleryMode,
);
@@ -256,7 +256,7 @@ class TimeMemoriesCalculator {
if (sortedYearsForCurrentMonth.isEmpty) break;
final year = sortedYearsForCurrentMonth.removeAt(0);
final monthYearFiles = currentMonthYearGroups[year]!;
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
monthYearFiles,
prefferedSize: monthSelectionSize,
isLocalGalleryMode: isLocalGalleryMode,
@@ -282,7 +282,7 @@ class TimeMemoriesCalculator {
final allPhotos = sortedYearsForCurrentMonth
.expand((year) => currentMonthYearGroups[year]!)
.toList();
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
allPhotos,
prefferedSize: monthSelectionSize,
isLocalGalleryMode: isLocalGalleryMode,
@@ -455,11 +455,11 @@ class TimeMemoriesCalculator {
candidates.add(Memory.fromFile(file, seenTimes));
}
- if (candidates.length < SmartMemoriesService.minimumMemoryLength) {
+ if (candidates.length < DartMemoriesComputer.minimumMemoryLength) {
return;
}
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
candidates,
prefferedSize: _recentTimeMemorySelectionSize,
distributionOverride: SelectionDistribution.timeBuckets,
@@ -515,7 +515,7 @@ class TimeMemoriesCalculator {
Memory.fromFile(
file,
seenTimes,
- seenTimeKey: SmartMemoriesService._seenTimeKeyForFile(
+ seenTimeKey: DartMemoriesComputer._seenTimeKeyForFile(
file,
localIdToIntId,
),
@@ -524,7 +524,7 @@ class TimeMemoriesCalculator {
}
for (
var yearAgo = 1;
- yearAgo <= SmartMemoriesService.yearsBefore;
+ yearAgo <= DartMemoriesComputer.yearsBefore;
yearAgo++
) {
final memories = yearsAgoToMemories[yearAgo];
@@ -583,7 +583,7 @@ class TimeMemoriesCalculator {
Memory.fromFile(
file,
seenTimes,
- seenTimeKey: SmartMemoriesService._seenTimeKeyForFile(
+ seenTimeKey: DartMemoriesComputer._seenTimeKeyForFile(
file,
localIdToIntId,
),
diff --git a/mobile/apps/photos/lib/services/smart_memories_trip_calculator_v2.dart b/mobile/apps/photos/lib/services/smart_memories_trip_calculator_v2.dart
index ad57aa70b4..5dbc2a3e1c 100644
--- a/mobile/apps/photos/lib/services/smart_memories_trip_calculator_v2.dart
+++ b/mobile/apps/photos/lib/services/smart_memories_trip_calculator_v2.dart
@@ -1,4 +1,4 @@
-part of "smart_memories_service.dart";
+part of "memories/dart_memories_computer.dart";
class _LocationCluster {
final List<EnteFile> files;
@@ -364,7 +364,7 @@ class TripMemoriesCalculatorV2 {
BaseLocation(
files
.map(
- (file) => SmartMemoriesService._memoryFileId(
+ (file) => DartMemoriesComputer._memoryFileId(
file,
isLocalGalleryMode: isLocalGalleryMode,
),
@@ -939,7 +939,7 @@ class TripMemoriesCalculatorV2 {
final files = baseLocation.fileIDs
.map((fileID) => allFileIdsToFile[fileID]!)
.toList();
- final String? locationName = SmartMemoriesService._tryFindLocationName(
+ final String? locationName = DartMemoriesComputer._tryFindLocationName(
Memory.fromFiles(files, seenTimes),
citySearchIndex,
base: true,
@@ -962,12 +962,12 @@ class TripMemoriesCalculatorV2 {
final year = DateTime.fromMicrosecondsSinceEpoch(
trip.averageCreationTime(),
).year;
- final String? locationName = SmartMemoriesService._tryFindLocationName(
+ final String? locationName = DartMemoriesComputer._tryFindLocationName(
trip.memories,
citySearchIndex,
excludedCountryNames: baseCountriesToExclude,
);
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
List<Memory>.of(trip.memories),
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -1111,12 +1111,12 @@ class TripMemoriesCalculatorV2 {
final year = DateTime.fromMicrosecondsSinceEpoch(
trip.averageCreationTime(),
).year;
- final String? locationName = SmartMemoriesService._tryFindLocationName(
+ final String? locationName = DartMemoriesComputer._tryFindLocationName(
trip.memories,
citySearchIndex,
excludedCountryNames: baseCountriesToExclude,
);
- final photoSelection = await SmartMemoriesService._bestSelection(
+ final photoSelection = await DartMemoriesComputer._bestSelection(
List<Memory>.of(trip.memories),
isLocalGalleryMode: isLocalGalleryMode,
mlEnabled: mlEnabled,
@@ -1154,7 +1154,7 @@ class TripMemoriesCalculatorV2 {
continue;
}
- final countryName = SmartMemoriesService._tryFindCountryName(
+ final countryName = DartMemoriesComputer._tryFindCountryName(
Memory.fromFiles(files, seenTimes),
citySearchIndex,
);
@@ -1162,7 +1162,7 @@ class TripMemoriesCalculatorV2 {
continue;
}
- final normalizedCountry = SmartMemoriesService._normalizePlaceName(
+ final normalizedCountry = DartMemoriesComputer._normalizePlaceName(
countryName,
);
if (normalizedCountry.isNotEmpty) {
diff --git a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
index 7b28163da8..478dc4a92e 100644
--- a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart
@@ -26,8 +26,8 @@ import "package:photos/models/selected_files.dart";
import "package:photos/module/download/file.dart";
import "package:photos/module/download/thumbnail.dart";
import "package:photos/service_locator.dart";
+import "package:photos/services/memories/smart_memories_service.dart";
import "package:photos/services/memory_share_service.dart";
-import "package:photos/services/smart_memories_service.dart";
import "package:photos/ui/actions/file/file_actions.dart";
import "package:photos/ui/collections/collection_action_sheet.dart";
import "package:photos/ui/home/memories/custom_listener.dart";
diff --git a/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart b/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart
index 2751aeabd7..a8106d8f7b 100644
--- a/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart
+++ b/mobile/apps/photos/test/services/memories/smart_memories_source_selection_test.dart
@@ -11,7 +11,7 @@ import "package:photos/models/memories/time_memory.dart";
import "package:photos/models/ml/face/face_with_embedding.dart";
import "package:photos/models/ml/vector.dart";
import "package:photos/services/location_service.dart";
-import "package:photos/services/smart_memories_service.dart";
+import "package:photos/services/memories/dart_memories_computer.dart";
import "package:timezone/data/latest.dart" as tzdata;
import "package:timezone/timezone.dart" as timezone;