Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Switch ML flags

laurenspriem1bf6b56f965c
diff --git a/mobile/lib/services/machine_learning/ml_service.dart b/mobile/lib/services/machine_learning/ml_service.dart
index 935996d85c..d33e94f745 100644
--- a/mobile/lib/services/machine_learning/ml_service.dart
+++ b/mobile/lib/services/machine_learning/ml_service.dart
@@ -25,6 +25,7 @@ import "package:photos/services/machine_learning/ml_indexing_isolate.dart";
import 'package:photos/services/machine_learning/ml_result.dart';
import "package:photos/services/machine_learning/semantic_search/clip/clip_image_encoder.dart";
import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";
+import "package:photos/services/user_remote_flag_service.dart";
import "package:photos/utils/ml_util.dart";
import "package:photos/utils/network_util.dart";
import "package:synchronized/synchronized.dart";
@@ -62,8 +63,10 @@ class MLService {
static const _kForceClusteringFaceCount = 8000;
/// Only call this function once at app startup, after that you can directly call [runAllML]
- Future<void> init({bool firstTime = false}) async {
+ Future<void> init() async {
if (_isInitialized) return;
+ if (!userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled)) return;
_logger.info("init called");
// Get client name
@@ -73,7 +76,8 @@ class MLService {
// Listen on MachineLearningController
Bus.instance.on<MachineLearningControlEvent>().listen((event) {
- // if (!canFetch()) return;
+ if (!userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled)) return;
_mlControllerStatus = event.shouldRun;
if (_mlControllerStatus) {
@@ -177,7 +181,7 @@ class MLService {
/// Analyzes all the images in the user library with the latest ml version and stores the results in the database.
///
- /// This function first fetches from remote and checks if the image has already been analyzed
+ /// This function first fetches from remote and checks if the image has already been analyzed
/// with the lastest faceMlVersion and stored on remote or local database. If so, it skips the image.
Future<void> fetchAndIndexAllImages() async {
if (_cannotRunMLFunction()) return;
@@ -613,7 +617,7 @@ class MLService {
void _logStatus() {
final String status = '''
isInternalUser: ${flagService.internalUser}
- ML Lite: ${!localSettings.isMLIndexingEnabled}
+ Local indexing: ${localSettings.isMLIndexingEnabled}
canRunMLController: $_mlControllerStatus
isIndexingOrClusteringRunning: $_isIndexingOrClusteringRunning
shouldPauseIndexingAndClustering: $_shouldPauseIndexingAndClustering
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.dart
index e6eb268f52..e2474cae3e 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
@@ -22,6 +22,7 @@ import "package:photos/services/collections_service.dart";
import "package:photos/services/machine_learning/ml_computer.dart";
import "package:photos/services/machine_learning/ml_result.dart";
import "package:photos/services/machine_learning/semantic_search/clip/clip_image_encoder.dart";
+import "package:photos/services/user_remote_flag_service.dart";
import "package:shared_preferences/shared_preferences.dart";
class SemanticSearchService {
@@ -43,13 +44,15 @@ class SemanticSearchService {
String? _latestPendingQuery;
Future<void> init() async {
- if (!localSettings.isMLIndexingEnabled) {
- return;
- }
if (_hasInitialized) {
_logger.info("Initialized already");
return;
}
+ final hasGivenConsent = userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled);
+ if (!hasGivenConsent) return;
+
+ _logger.info("init called");
_hasInitialized = true;
// call getClipEmbeddings after 5 seconds
@@ -64,7 +67,8 @@ class SemanticSearchService {
}
bool isMagicSearchEnabledAndReady() {
- return localSettings.isMLIndexingEnabled && _textModelIsLoaded;
+ return userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled) && _textModelIsLoaded;
}
// searchScreenQuery should only be used for the user initiate query on the search screen.
@@ -73,7 +77,8 @@ class SemanticSearchService {
if (!isMagicSearchEnabledAndReady()) {
if (flagService.internalUser) {
_logger.info(
- "Magic search enabled: ${localSettings.isMLIndexingEnabled}, loaded: $_textModelIsLoaded ",
+ "ML global consent: ${userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled)}, loaded: $_textModelIsLoaded ",
);
}
return (query, <EnteFile>[]);
diff --git a/mobile/lib/services/magic_cache_service.dart b/mobile/lib/services/magic_cache_service.dart
index 895db2061b..410b813acc 100644
--- a/mobile/lib/services/magic_cache_service.dart
+++ b/mobile/lib/services/magic_cache_service.dart
@@ -21,6 +21,7 @@ import "package:photos/service_locator.dart";
import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";
import "package:photos/services/remote_assets_service.dart";
import "package:photos/services/search_service.dart";
+import "package:photos/services/user_remote_flag_service.dart";
import "package:photos/ui/viewer/search/result/magic_result_screen.dart";
import "package:photos/utils/navigation_util.dart";
import "package:shared_preferences/shared_preferences.dart";
@@ -186,7 +187,8 @@ class MagicCacheService {
return _prefs.getInt(_lastMagicCacheUpdateTime) ?? 0;
}
- bool get enableDiscover => localSettings.isMLIndexingEnabled;
+ bool get enableDiscover => userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled);
void queueUpdate(String reason) {
_pendingUpdateReason.add(reason);
diff --git a/mobile/lib/services/search_service.dart b/mobile/lib/services/search_service.dart
index 22b02d953b..fb3e202aad 100644
--- a/mobile/lib/services/search_service.dart
+++ b/mobile/lib/services/search_service.dart
@@ -32,6 +32,7 @@ import "package:photos/services/location_service.dart";
import "package:photos/services/machine_learning/face_ml/face_filtering/face_filtering_constants.dart";
import "package:photos/services/machine_learning/face_ml/person/person_service.dart";
import 'package:photos/services/machine_learning/semantic_search/semantic_search_service.dart';
+import "package:photos/services/user_remote_flag_service.dart";
import "package:photos/states/location_screen_state.dart";
import "package:photos/ui/viewer/location/add_location_sheet.dart";
import "package:photos/ui/viewer/location/location_screen.dart";
@@ -179,7 +180,8 @@ class SearchService {
Future<List<GenericSearchResult>> getMagicSectionResults(
BuildContext context,
) async {
- if (localSettings.isMLIndexingEnabled) {
+ if (userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled)) {
return magicCacheService.getMagicGenericSearchResult(context);
} else {
return <GenericSearchResult>[];
diff --git a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
index bfa6f57e5b..516e0fb8db 100644
--- a/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
+++ b/mobile/lib/ui/settings/debug/ml_debug_section_widget.dart
@@ -78,7 +78,7 @@ class _MLDebugSectionWidgetState extends State<MLDebugSectionWidget> {
value: () => localSettings.isMLIndexingEnabled,
onChanged: () async {
try {
- final isEnabled = await localSettings.toggleMLIndexing();
+ final isEnabled = await localSettings.toggleLocalMLIndexing();
logger.info('ML indexing turned ${isEnabled ? 'on' : 'off'}');
if (isEnabled) {
await MLService.instance.init();
diff --git a/mobile/lib/ui/settings/ml/machine_learning_settings_page.dart b/mobile/lib/ui/settings/ml/machine_learning_settings_page.dart
index ceac16fa46..f53da70d6d 100644
--- a/mobile/lib/ui/settings/ml/machine_learning_settings_page.dart
+++ b/mobile/lib/ui/settings/ml/machine_learning_settings_page.dart
@@ -7,6 +7,7 @@ import "package:photos/l10n/l10n.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/machine_learning/face_ml/face_detection/face_detection_service.dart";
import "package:photos/services/machine_learning/face_ml/face_embedding/face_embedding_service.dart";
+import "package:photos/services/machine_learning/ml_indexing_isolate.dart";
import "package:photos/services/machine_learning/ml_service.dart";
import "package:photos/services/machine_learning/semantic_search/clip/clip_image_encoder.dart";
import "package:photos/services/machine_learning/semantic_search/clip/clip_text_encoder.dart";
@@ -76,7 +77,8 @@ class _MachineLearningSettingsPageState
@override
Widget build(BuildContext context) {
- final hasEnabled = localSettings.isMLIndexingEnabled;
+ final hasEnabled = userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled);
return Scaffold(
body: CustomScrollView(
primary: false,
@@ -91,7 +93,6 @@ class _MachineLearningSettingsPageState
_titleTapCount++;
if (_titleTapCount >= 7) {
_titleTapCount = 0;
- // showShortToast(context, "Advanced options enabled");
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
@@ -169,7 +170,7 @@ class _MachineLearningSettingsPageState
buttonType: ButtonType.primary,
labelText: context.l10n.enable,
onTap: () async {
- await toggleIndexingState();
+ await toggleMlConsent();
},
),
const SizedBox(height: 12),
@@ -209,10 +210,19 @@ class _MachineLearningSettingsPageState
);
}
- Future<void> toggleIndexingState() async {
- final hasGivenConsent = userRemoteFlagService
+ Future<void> toggleMlConsent() async {
+ final oldMlConsent = userRemoteFlagService
.getCachedBoolValue(UserRemoteFlagService.mlEnabled);
- if (!localSettings.isMLIndexingEnabled && !hasGivenConsent) {
+ final mlConsent = !oldMlConsent;
+ await userRemoteFlagService.setBoolValue(
+ UserRemoteFlagService.mlEnabled,
+ mlConsent,
+ );
+ if (!mlConsent) {
+ MLService.instance.pauseIndexingAndClustering();
+ unawaited(
+ MLIndexingIsolate.instance.cleanupLocalIndexingModels(),
+ );
final result = await Navigator.push(
context,
MaterialPageRoute(
@@ -224,18 +234,10 @@ class _MachineLearningSettingsPageState
if (result == null || result == false) {
return;
}
- }
- final isEnabled = await localSettings.toggleMLIndexing();
- if (isEnabled) {
- await MLService.instance.init(firstTime: true);
+ } else {
+ await MLService.instance.init();
await SemanticSearchService.instance.init();
unawaited(MLService.instance.runAllML(force: true));
- } else {
- MLService.instance.pauseIndexingAndClustering();
- await userRemoteFlagService.setBoolValue(
- UserRemoteFlagService.mlEnabled,
- false,
- );
}
if (mounted) {
setState(() {});
@@ -244,66 +246,75 @@ class _MachineLearningSettingsPageState
Widget _getMlSettings(BuildContext context) {
final colorScheme = getEnteColorScheme(context);
- final hasEnabled = localSettings.isMLIndexingEnabled;
+ final hasEnabled = userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled);
+ if (!hasEnabled) {
+ return const SizedBox.shrink();
+ }
return Column(
children: [
- if (hasEnabled)
- MenuItemWidget(
- captionedTextWidget: CaptionedTextWidget(
- title: S.of(context).enabled,
- ),
- menuItemColor: colorScheme.fillFaint,
- trailingWidget: ToggleSwitchWidget(
- value: () => localSettings.isMLIndexingEnabled,
- onChanged: () async {
- await toggleIndexingState();
- },
- ),
- singleBorderRadius: 8,
- alignCaptionedTextToLeft: true,
- isGestureDetectorDisabled: true,
+ MenuItemWidget(
+ captionedTextWidget: CaptionedTextWidget(
+ title: S.of(context).enabled,
),
- if (hasEnabled)
- const SizedBox(
- height: 4,
+ menuItemColor: colorScheme.fillFaint,
+ trailingWidget: ToggleSwitchWidget(
+ value: () => hasEnabled,
+ onChanged: () async {
+ await toggleMlConsent();
+ },
),
- if (hasEnabled)
- ExpandableMenuItemWidget(
- title: "Advanced configuration",
- selectionOptionsWidget: Column(
- children: [
- const SizedBox(
- height: 2,
+ singleBorderRadius: 8,
+ alignCaptionedTextToLeft: true,
+ isGestureDetectorDisabled: true,
+ ),
+ const SizedBox(
+ height: 4,
+ ),
+ ExpandableMenuItemWidget(
+ title: "Configuration",
+ selectionOptionsWidget: Column(
+ children: [
+ const SizedBox(
+ height: 2,
+ ),
+ MenuItemWidget(
+ captionedTextWidget: const CaptionedTextWidget(
+ title: "Local indexing",
),
- MenuItemWidget(
- captionedTextWidget: const CaptionedTextWidget(
- title: "Local indexing",
- ),
- menuItemColor: colorScheme.fillFaint,
- trailingWidget: ToggleSwitchWidget(
- value: () => localSettings.isMLIndexingEnabled,
- onChanged: () async {
- await localSettings.toggleMLIndexing();
- if (mounted) {
- setState(() {});
- }
- },
- ),
- singleBorderRadius: 8,
- isGestureDetectorDisabled: true,
+ menuItemColor: colorScheme.fillFaint,
+ trailingWidget: ToggleSwitchWidget(
+ value: () => localSettings.isMLIndexingEnabled,
+ onChanged: () async {
+ final localIndexing =
+ await localSettings.toggleLocalMLIndexing();
+ if (localIndexing) {
+ unawaited(MLService.instance.runAllML(force: true));
+ } else {
+ MLService.instance.pauseIndexingAndClustering();
+ unawaited(
+ MLIndexingIsolate.instance.cleanupLocalIndexingModels(),
+ );
+ }
+
+ if (mounted) {
+ setState(() {});
+ }
+ },
),
- ],
- ),
- leadingIcon: Icons.settings_outlined,
- ),
- if (hasEnabled)
- const SizedBox(
- height: 12,
+ singleBorderRadius: 8,
+ isGestureDetectorDisabled: true,
+ ),
+ ],
),
- if (hasEnabled)
- MLService.instance.areModelsDownloaded
- ? const MLStatusWidget()
- : const ModelLoadingState(),
+ leadingIcon: Icons.settings_outlined,
+ ),
+ const SizedBox(
+ height: 12,
+ ),
+ MLService.instance.areModelsDownloaded
+ ? const MLStatusWidget()
+ : const ModelLoadingState(),
],
);
}
diff --git a/mobile/lib/ui/viewer/file/file_details_widget.dart b/mobile/lib/ui/viewer/file/file_details_widget.dart
index 21318523ba..0d3d403655 100644
--- a/mobile/lib/ui/viewer/file/file_details_widget.dart
+++ b/mobile/lib/ui/viewer/file/file_details_widget.dart
@@ -18,6 +18,7 @@ import "package:photos/models/location/location.dart";
import "package:photos/models/metadata/file_magic.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/file_magic_service.dart";
+import "package:photos/services/user_remote_flag_service.dart";
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/components/buttons/icon_button_widget.dart';
import "package:photos/ui/components/divider_widget.dart";
@@ -280,7 +281,8 @@ class _FileDetailsWidgetState extends State<FileDetailsWidget> {
]);
}
- if (localSettings.isMLIndexingEnabled) {
+ if (userRemoteFlagService
+ .getCachedBoolValue(UserRemoteFlagService.mlEnabled)) {
fileDetailsTiles.addAll([
FacesItemWidget(file),
const FileDetailsDivider(),
diff --git a/mobile/lib/ui/viewer/search_tab/search_tab.dart b/mobile/lib/ui/viewer/search_tab/search_tab.dart
index 650eb1be11..46b25614a7 100644
--- a/mobile/lib/ui/viewer/search_tab/search_tab.dart
+++ b/mobile/lib/ui/viewer/search_tab/search_tab.dart
@@ -9,6 +9,7 @@ import "package:photos/models/search/index_of_indexed_stack.dart";
import "package:photos/models/search/search_result.dart";
import "package:photos/models/search/search_types.dart";
import "package:photos/service_locator.dart";
+import "package:photos/services/user_remote_flag_service.dart";
import "package:photos/states/all_sections_examples_state.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/viewer/search/result/no_result_widget.dart";
@@ -115,7 +116,9 @@ class _AllSearchSectionsState extends State<AllSearchSections> {
itemBuilder: (context, index) {
switch (searchTypes[index]) {
case SectionType.face:
- if (!localSettings.isMLIndexingEnabled) {
+ if (!userRemoteFlagService.getCachedBoolValue(
+ UserRemoteFlagService.mlEnabled,
+ )) {
return const SizedBox.shrink();
}
return PeopleSection(
diff --git a/mobile/lib/utils/local_settings.dart b/mobile/lib/utils/local_settings.dart
index 1ea9c4af95..d9efd43740 100644
--- a/mobile/lib/utils/local_settings.dart
+++ b/mobile/lib/utils/local_settings.dart
@@ -57,11 +57,8 @@ class LocalSettings {
return getRateUsShownCount() < kRateUsPromptThreshold;
}
- // remove `enable_face_indexing`fallback after sometime, affects internal users only
bool get isMLIndexingEnabled =>
- _prefs.getBool(_kisMLIndexingEnabled) ??
- _prefs.getBool('enable_face_indexing') ??
- false;
+ _prefs.getBool(_kisMLIndexingEnabled) ?? false;
bool get userEnabledMultiplePart =>
_prefs.getBool(kEnableMultiplePart) ?? false;
@@ -72,7 +69,7 @@ class LocalSettings {
}
/// toggleFaceIndexing toggles the face indexing setting and returns the new value
- Future<bool> toggleMLIndexing() async {
+ Future<bool> toggleLocalMLIndexing() async {
await _prefs.setBool(_kisMLIndexingEnabled, !isMLIndexingEnabled);
return isMLIndexingEnabled;
}