Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor people memory selection

laurenspriem576ab347c335
diff --git a/mobile/apps/photos/lib/models/memories/people_memory.dart b/mobile/apps/photos/lib/models/memories/people_memory.dart
index 4816b38b62..6b89eada22 100644
--- a/mobile/apps/photos/lib/models/memories/people_memory.dart
+++ b/mobile/apps/photos/lib/models/memories/people_memory.dart
@@ -1,3 +1,5 @@
+import "dart:async";
+
import "package:photos/generated/l10n.dart";
import "package:photos/models/memories/memory.dart";
import "package:photos/models/memories/smart_memory.dart";
@@ -176,3 +178,64 @@ class PeopleMemory extends SmartMemory {
}
}
}
+
+typedef PeopleSelectionBuilder = Future<List<Memory>> Function(
+ List<Memory> memories,
+);
+
+class PeopleMemoryCandidate {
+ PeopleMemoryCandidate({
+ required this.personID,
+ required this.personName,
+ required this.type,
+ required this.rawMemories,
+ required this.firstDateToShow,
+ required this.lastDateToShow,
+ this.activity,
+ this.lastCreationTime,
+ this.selectionBuilder,
+ this.requiresSelection = true,
+ }) : assert(!requiresSelection || selectionBuilder != null);
+
+ final String personID;
+ final String? personName;
+ final PeopleMemoryType type;
+ final PeopleActivity? activity;
+ final List<Memory> rawMemories;
+ final int firstDateToShow;
+ final int lastDateToShow;
+ final int? lastCreationTime;
+ final bool requiresSelection;
+ final PeopleSelectionBuilder? selectionBuilder;
+
+ PeopleMemory? _resolvedMemory;
+ Future<PeopleMemory?>? _pendingBuild;
+
+ Future<PeopleMemory?> realize() {
+ if (_resolvedMemory != null) return Future.value(_resolvedMemory);
+ _pendingBuild ??= _build();
+ return _pendingBuild!;
+ }
+
+ Future<PeopleMemory?> _build() async {
+ List<Memory> memories = rawMemories;
+ if (requiresSelection) {
+ final builder = selectionBuilder;
+ if (builder == null) return null;
+ final selection = await builder(rawMemories);
+ if (selection.isEmpty) return null;
+ memories = selection;
+ }
+ _resolvedMemory = PeopleMemory(
+ memories,
+ firstDateToShow,
+ lastDateToShow,
+ type,
+ personID,
+ personName,
+ lastCreationTime: lastCreationTime,
+ activity: activity,
+ );
+ return _resolvedMemory;
+ }
+}
diff --git a/mobile/apps/photos/lib/services/smart_memories_service.dart b/mobile/apps/photos/lib/services/smart_memories_service.dart
index 42b306fec1..d553fd4fb4 100644
--- a/mobile/apps/photos/lib/services/smart_memories_service.dart
+++ b/mobile/apps/photos/lib/services/smart_memories_service.dart
@@ -442,8 +442,16 @@ class SmartMemoriesService {
if (isMeAssigned) meFileIDs = personIdToFileIDs[meID]!;
// Loop through the people and find all memories
- final Map<String, Map<PeopleMemoryType, List<PeopleMemory>>>
- personToMemories = {};
+ Future<List<Memory>> selectPeopleMemories(List<Memory> memories) {
+ return _bestSelectionPeople(
+ memories,
+ fileIDToImageEmbedding: fileIDToImageEmbedding,
+ clipPositiveTextVector: clipPositiveTextVector,
+ );
+ }
+
+ final Map<String, Map<PeopleMemoryType, List<PeopleMemoryCandidate>>>
+ personToCandidates = {};
for (final personID in orderedImportantPersonsID) {
final personFileIDs = personIdToFileIDs[personID]!;
final personName = personIdToPerson[personID]!.data.name;
@@ -461,22 +469,25 @@ class SmartMemoriesService {
}
}
if (spotlightFiles.length > minimumMemoryLength) {
- final selectSpotlightMemories = await _bestSelectionPeople(
- spotlightFiles.map((f) => Memory.fromFile(f, seenTimes)).toList(),
- fileIDToImageEmbedding: fileIDToImageEmbedding,
- clipPositiveTextVector: clipPositiveTextVector,
- );
- final spotlightMemory = PeopleMemory(
- selectSpotlightMemories,
- nowInMicroseconds,
- windowEnd,
- PeopleMemoryType.spotlight,
- personID,
- (isMeAssigned && meID == personID) ? null : personName,
+ final spotlightMemories = spotlightFiles
+ .map((f) => Memory.fromFile(f, seenTimes))
+ .toList(growable: false);
+ final spotlightList =
+ personToCandidates.putIfAbsent(personID, () => {}).putIfAbsent(
+ PeopleMemoryType.spotlight,
+ () => <PeopleMemoryCandidate>[],
+ );
+ spotlightList.add(
+ PeopleMemoryCandidate(
+ personID: personID,
+ personName: (isMeAssigned && meID == personID) ? null : personName,
+ type: PeopleMemoryType.spotlight,
+ rawMemories: spotlightMemories,
+ firstDateToShow: nowInMicroseconds,
+ lastDateToShow: windowEnd,
+ selectionBuilder: selectPeopleMemories,
+ ),
);
- personToMemories
- .putIfAbsent(personID, () => {})
- .putIfAbsent(PeopleMemoryType.spotlight, () => [spotlightMemory]);
}
w?.log('spotlight setup');
@@ -493,24 +504,25 @@ class SmartMemoriesService {
}
}
if (youAndThemFiles.length > minimumMemoryLength) {
- // final String title = "You and $personName";
- final selectYouAndThemMemories = await _bestSelectionPeople(
- youAndThemFiles.map((f) => Memory.fromFile(f, seenTimes)).toList(),
- fileIDToImageEmbedding: fileIDToImageEmbedding,
- clipPositiveTextVector: clipPositiveTextVector,
- );
- final youAndThemMemory = PeopleMemory(
- selectYouAndThemMemories,
- nowInMicroseconds,
- windowEnd,
- PeopleMemoryType.youAndThem,
- personID,
- personName,
+ final youAndThemMemories = youAndThemFiles
+ .map((f) => Memory.fromFile(f, seenTimes))
+ .toList(growable: false);
+ final youAndThemList =
+ personToCandidates.putIfAbsent(personID, () => {}).putIfAbsent(
+ PeopleMemoryType.youAndThem,
+ () => <PeopleMemoryCandidate>[],
+ );
+ youAndThemList.add(
+ PeopleMemoryCandidate(
+ personID: personID,
+ personName: personName,
+ type: PeopleMemoryType.youAndThem,
+ rawMemories: youAndThemMemories,
+ firstDateToShow: nowInMicroseconds,
+ lastDateToShow: windowEnd,
+ selectionBuilder: selectPeopleMemories,
+ ),
);
- personToMemories.putIfAbsent(personID, () => {}).putIfAbsent(
- PeopleMemoryType.youAndThem,
- () => [youAndThemMemory],
- );
}
w?.log('youAndThem setup');
}
@@ -549,27 +561,26 @@ class SmartMemoriesService {
}
}
if (activityFiles.length > minimumMemoryLength) {
- final selectActivityMemories = await _bestSelectionPeople(
- activityFiles.map((f) => Memory.fromFile(f, seenTimes)).toList(),
- fileIDToImageEmbedding: fileIDToImageEmbedding,
- clipPositiveTextVector: clipPositiveTextVector,
- );
- final activityMemory = PeopleMemory(
- selectActivityMemories,
- nowInMicroseconds,
- windowEnd,
- PeopleMemoryType.doingSomethingTogether,
- personID,
- personName,
- activity: activity,
+ final activityMemories = activityFiles
+ .map((f) => Memory.fromFile(f, seenTimes))
+ .toList(growable: false);
+ final activityList =
+ personToCandidates.putIfAbsent(personID, () => {}).putIfAbsent(
+ PeopleMemoryType.doingSomethingTogether,
+ () => <PeopleMemoryCandidate>[],
+ );
+ activityList.add(
+ PeopleMemoryCandidate(
+ personID: personID,
+ personName: personName,
+ type: PeopleMemoryType.doingSomethingTogether,
+ rawMemories: activityMemories,
+ firstDateToShow: nowInMicroseconds,
+ lastDateToShow: windowEnd,
+ activity: activity,
+ selectionBuilder: selectPeopleMemories,
+ ),
);
- personToMemories
- .putIfAbsent(personID, () => {})
- .putIfAbsent(
- PeopleMemoryType.doingSomethingTogether,
- () => [],
- )
- .add(activityMemory);
}
}
@@ -604,31 +615,40 @@ class SmartMemoriesService {
}
}
if (longAgo && lastTimeYouSawThemFiles.length >= 2 && meID != personID) {
- final lastTimeMemory = PeopleMemory(
- lastTimeYouSawThemFiles
- .map((f) => Memory.fromFile(f, seenTimes))
- .toList(),
- nowInMicroseconds,
- windowEnd,
- PeopleMemoryType.lastTimeYouSawThem,
- personID,
- personName,
- lastCreationTime: lastCreationTime,
+ final lastTimeMemories = lastTimeYouSawThemFiles
+ .map((f) => Memory.fromFile(f, seenTimes))
+ .toList(growable: false);
+ final lastTimeList =
+ personToCandidates.putIfAbsent(personID, () => {}).putIfAbsent(
+ PeopleMemoryType.lastTimeYouSawThem,
+ () => <PeopleMemoryCandidate>[],
+ );
+ lastTimeList.add(
+ PeopleMemoryCandidate(
+ personID: personID,
+ personName: personName,
+ type: PeopleMemoryType.lastTimeYouSawThem,
+ rawMemories: lastTimeMemories,
+ firstDateToShow: nowInMicroseconds,
+ lastDateToShow: windowEnd,
+ lastCreationTime: lastCreationTime,
+ requiresSelection: false,
+ ),
);
- personToMemories.putIfAbsent(personID, () => {}).putIfAbsent(
- PeopleMemoryType.lastTimeYouSawThem,
- () => [lastTimeMemory],
- );
}
w?.log('lastTimeYouSawThem setup');
}
// Surface everything just for debug checking
if (surfaceAll) {
- for (final personID in personToMemories.keys) {
- final personMemories = personToMemories[personID]!;
- for (final memoryType in personMemories.keys) {
- memoryResults.addAll(personMemories[memoryType]!);
+ for (final personCandidates in personToCandidates.values) {
+ for (final candidateList in personCandidates.values) {
+ for (final candidate in candidateList) {
+ final memory = await candidate.realize();
+ if (memory != null) {
+ memoryResults.add(memory);
+ }
+ }
}
}
return memoryResults;
@@ -636,20 +656,24 @@ class SmartMemoriesService {
// Loop through the people and check if we should surface anything based on relevancy (bday, last met)
for (final personID in orderedImportantPersonsID) {
- final personMemories = personToMemories[personID];
- if (personMemories == null) continue;
+ final personCandidates = personToCandidates[personID];
+ if (personCandidates == null) continue;
final person = personIdToPerson[personID]!;
// Check if we should surface memory based on last met
- final lastMetMemory =
- personMemories[PeopleMemoryType.lastTimeYouSawThem]?.first;
- if (lastMetMemory != null) {
+ final lastMetCandidate =
+ personCandidates[PeopleMemoryType.lastTimeYouSawThem]?.first;
+ if (lastMetCandidate != null &&
+ lastMetCandidate.lastCreationTime != null) {
final lastMetTime = DateTime.fromMicrosecondsSinceEpoch(
- lastMetMemory.lastCreationTime!,
+ lastMetCandidate.lastCreationTime!,
).copyWith(year: currentTime.year);
final daysSinceLastMet = lastMetTime.difference(currentTime).inDays;
if (daysSinceLastMet < 7 && daysSinceLastMet >= 0) {
- memoryResults.add(lastMetMemory);
+ final lastMetMemory = await lastMetCandidate.realize();
+ if (lastMetMemory != null) {
+ memoryResults.add(lastMetMemory);
+ }
}
}
@@ -661,52 +685,59 @@ class SmartMemoriesService {
final daysTillBirthday = thisBirthday.difference(currentTime).inDays;
if (daysTillBirthday < 6 && daysTillBirthday >= 0) {
final int newAge = currentTime.year - birthdate.year;
- final spotlightMem =
- personMemories[PeopleMemoryType.spotlight]?.first;
- if (spotlightMem != null && spotlightMem.personName != null) {
- final thisBirthday = birthdate.copyWith(year: currentTime.year);
- memoryResults.add(
- spotlightMem.copyWith(
- isBirthday: false,
- newAge: newAge,
- firstDateToShow: thisBirthday
- .subtract(const Duration(days: 5))
- .microsecondsSinceEpoch,
- lastDateToShow: thisBirthday.microsecondsSinceEpoch,
- ),
- );
- memoryResults.add(
- spotlightMem.copyWith(
- isBirthday: true,
- newAge: newAge,
- firstDateToShow: thisBirthday.microsecondsSinceEpoch,
- lastDateToShow:
- thisBirthday.add(kDayItself).microsecondsSinceEpoch,
- ),
- );
+ final spotlightCandidate =
+ personCandidates[PeopleMemoryType.spotlight]?.first;
+ if (spotlightCandidate != null &&
+ spotlightCandidate.personName != null) {
+ final spotlightMem = await spotlightCandidate.realize();
+ if (spotlightMem != null) {
+ final thisBirthday = birthdate.copyWith(year: currentTime.year);
+ memoryResults.add(
+ spotlightMem.copyWith(
+ isBirthday: false,
+ newAge: newAge,
+ firstDateToShow: thisBirthday
+ .subtract(const Duration(days: 5))
+ .microsecondsSinceEpoch,
+ lastDateToShow: thisBirthday.microsecondsSinceEpoch,
+ ),
+ );
+ memoryResults.add(
+ spotlightMem.copyWith(
+ isBirthday: true,
+ newAge: newAge,
+ firstDateToShow: thisBirthday.microsecondsSinceEpoch,
+ lastDateToShow:
+ thisBirthday.add(kDayItself).microsecondsSinceEpoch,
+ ),
+ );
+ }
}
- final youAndThemMem =
- personMemories[PeopleMemoryType.youAndThem]?.first;
- if (youAndThemMem != null) {
- memoryResults.add(
- youAndThemMem.copyWith(
- isBirthday: false,
- newAge: newAge,
- firstDateToShow: thisBirthday
- .subtract(const Duration(days: 5))
- .microsecondsSinceEpoch,
- lastDateToShow: thisBirthday.microsecondsSinceEpoch,
- ),
- );
- memoryResults.add(
- youAndThemMem.copyWith(
- isBirthday: true,
- newAge: newAge,
- firstDateToShow: thisBirthday.microsecondsSinceEpoch,
- lastDateToShow:
- thisBirthday.add(kDayItself).microsecondsSinceEpoch,
- ),
- );
+ final youAndThemCandidate =
+ personCandidates[PeopleMemoryType.youAndThem]?.first;
+ if (youAndThemCandidate != null) {
+ final youAndThemMem = await youAndThemCandidate.realize();
+ if (youAndThemMem != null) {
+ memoryResults.add(
+ youAndThemMem.copyWith(
+ isBirthday: false,
+ newAge: newAge,
+ firstDateToShow: thisBirthday
+ .subtract(const Duration(days: 5))
+ .microsecondsSinceEpoch,
+ lastDateToShow: thisBirthday.microsecondsSinceEpoch,
+ ),
+ );
+ memoryResults.add(
+ youAndThemMem.copyWith(
+ isBirthday: true,
+ newAge: newAge,
+ firstDateToShow: thisBirthday.microsecondsSinceEpoch,
+ lastDateToShow:
+ thisBirthday.add(kDayItself).microsecondsSinceEpoch,
+ ),
+ );
+ }
}
}
}
@@ -737,27 +768,29 @@ class SmartMemoriesService {
currentTime.difference(shownDate) < shownPersonTimeout;
if (seenPersonRecently) continue peopleRotationLoop;
}
- if (personToMemories[personID] == null) continue peopleRotationLoop;
+ final personCandidates = personToCandidates[personID];
+ if (personCandidates == null) continue peopleRotationLoop;
int added = 0;
- final amountOfMemoryTypesForPerson = personToMemories[personID]!.length;
+ final amountOfMemoryTypesForPerson = personCandidates.length;
final bool manyMemoryTypes = amountOfMemoryTypesForPerson > 2;
potentialMemoryLoop:
- for (final memoriesForCategory in personToMemories[personID]!.values) {
- PeopleMemory potentialMemory = memoriesForCategory.first;
- if (memoriesForCategory.length > 1) {
- if (potentialMemory.peopleMemoryType !=
+ for (final candidatesForCategory in personCandidates.values) {
+ if (candidatesForCategory.isEmpty) continue;
+ PeopleMemoryCandidate potentialCandidate = candidatesForCategory.first;
+ if (candidatesForCategory.length > 1) {
+ if (potentialCandidate.type !=
PeopleMemoryType.doingSomethingTogether) {
dev.log(
- 'Something is going wrong, ${potentialMemory.peopleMemoryType} has multiple memories for same person',
+ 'Something is going wrong, ${potentialCandidate.type} has multiple memories for same person',
);
} else {
- final randIdx = Random().nextInt(memoriesForCategory.length);
- potentialMemory = memoriesForCategory[randIdx];
+ final randIdx = Random().nextInt(candidatesForCategory.length);
+ potentialCandidate = candidatesForCategory[randIdx];
}
}
for (final shownLog in shownPeople) {
if (shownLog.personID != personID) continue;
- if (shownLog.peopleMemoryType != potentialMemory.peopleMemoryType) {
+ if (shownLog.peopleMemoryType != potentialCandidate.type) {
continue;
}
final shownTypeDate =
@@ -768,6 +801,8 @@ class SmartMemoriesService {
continue potentialMemoryLoop;
}
}
+ final potentialMemory = await potentialCandidate.realize();
+ if (potentialMemory == null) continue;
memoryResults.add(potentialMemory);
added++;
if (added >= 2) break peopleRotationLoop;