[mob][photos] Rename to MLIndexingIsolate

This commit is contained in:
laurenspriem 2024-07-25 21:50:56 +02:00
parent d30c04cc55
commit 00beadbc01
2 changed files with 20 additions and 20 deletions

View File

@ -14,10 +14,10 @@ import "package:photos/services/machine_learning/semantic_search/clip/clip_image
import "package:photos/utils/ml_util.dart"; import "package:photos/utils/ml_util.dart";
import "package:synchronized/synchronized.dart"; import "package:synchronized/synchronized.dart";
enum MLOperation { analyzeImage, loadModels, releaseModels } enum MLIndexingOperation { analyzeImage, loadModels, releaseModels }
class MLIsolate { class MLIndexingIsolate {
static final _logger = Logger("MLIsolate"); static final _logger = Logger("MLIndexingIsolate");
Timer? _inactivityTimer; Timer? _inactivityTimer;
final Duration _inactivityDuration = const Duration(seconds: 120); final Duration _inactivityDuration = const Duration(seconds: 120);
@ -35,9 +35,9 @@ class MLIsolate {
bool shouldPauseIndexingAndClustering = false; bool shouldPauseIndexingAndClustering = false;
// Singleton pattern // Singleton pattern
MLIsolate._privateConstructor(); MLIndexingIsolate._privateConstructor();
static final instance = MLIsolate._privateConstructor(); static final instance = MLIndexingIsolate._privateConstructor();
factory MLIsolate() => instance; factory MLIndexingIsolate() => instance;
Future<void> _initIsolate() async { Future<void> _initIsolate() async {
return _initIsolateLock.synchronized(() async { return _initIsolateLock.synchronized(() async {
@ -74,13 +74,13 @@ class MLIsolate {
mainSendPort.send(receivePort.sendPort); mainSendPort.send(receivePort.sendPort);
receivePort.listen((message) async { receivePort.listen((message) async {
final functionIndex = message[0] as int; final functionIndex = message[0] as int;
final function = MLOperation.values[functionIndex]; final function = MLIndexingOperation.values[functionIndex];
final args = message[1] as Map<String, dynamic>; final args = message[1] as Map<String, dynamic>;
final sendPort = message[2] as SendPort; final sendPort = message[2] as SendPort;
try { try {
switch (function) { switch (function) {
case MLOperation.analyzeImage: case MLIndexingOperation.analyzeImage:
final time = DateTime.now(); final time = DateTime.now();
final MLResult result = await analyzeImageStatic(args); final MLResult result = await analyzeImageStatic(args);
_logger.info( _logger.info(
@ -88,7 +88,7 @@ class MLIsolate {
); );
sendPort.send(result.toJsonString()); sendPort.send(result.toJsonString());
break; break;
case MLOperation.loadModels: case MLIndexingOperation.loadModels:
final modelNames = args['modelNames'] as List<String>; final modelNames = args['modelNames'] as List<String>;
final modelPaths = args['modelPaths'] as List<String>; final modelPaths = args['modelPaths'] as List<String>;
final addresses = <int>[]; final addresses = <int>[];
@ -101,7 +101,7 @@ class MLIsolate {
} }
sendPort.send(List.from(addresses, growable: false)); sendPort.send(List.from(addresses, growable: false));
break; break;
case MLOperation.releaseModels: case MLIndexingOperation.releaseModels:
final modelNames = args['modelNames'] as List<String>; final modelNames = args['modelNames'] as List<String>;
final modelAddresses = args['modelAddresses'] as List<int>; final modelAddresses = args['modelAddresses'] as List<int>;
for (int i = 0; i < modelNames.length; i++) { for (int i = 0; i < modelNames.length; i++) {
@ -122,13 +122,13 @@ class MLIsolate {
/// The common method to run any operation in the isolate. It sends the [message] to [_isolateMain] and waits for the result. /// The common method to run any operation in the isolate. It sends the [message] to [_isolateMain] and waits for the result.
Future<dynamic> _runInIsolate( Future<dynamic> _runInIsolate(
(MLOperation, Map<String, dynamic>) message, (MLIndexingOperation, Map<String, dynamic>) message,
) async { ) async {
await _initIsolate(); await _initIsolate();
return _functionLock.synchronized(() async { return _functionLock.synchronized(() async {
_resetInactivityTimer(); _resetInactivityTimer();
if (message.$1 == MLOperation.analyzeImage && if (message.$1 == MLIndexingOperation.analyzeImage &&
shouldPauseIndexingAndClustering) { shouldPauseIndexingAndClustering) {
return null; return null;
} }
@ -198,7 +198,7 @@ class MLIsolate {
try { try {
final resultJsonString = await _runInIsolate( final resultJsonString = await _runInIsolate(
( (
MLOperation.analyzeImage, MLIndexingOperation.analyzeImage,
{ {
"enteFileID": instruction.enteFile.uploadedFileID ?? -1, "enteFileID": instruction.enteFile.uploadedFileID ?? -1,
"filePath": filePath, "filePath": filePath,
@ -271,7 +271,7 @@ class MLIsolate {
try { try {
final addresses = await _runInIsolate( final addresses = await _runInIsolate(
( (
MLOperation.loadModels, MLIndexingOperation.loadModels,
{ {
"modelNames": modelNames, "modelNames": modelNames,
"modelPaths": modelPaths, "modelPaths": modelPaths,
@ -306,7 +306,7 @@ class MLIsolate {
try { try {
await _runInIsolate( await _runInIsolate(
( (
MLOperation.releaseModels, MLIndexingOperation.releaseModels,
{ {
"modelNames": modelNames, "modelNames": modelNames,
"modelAddresses": modelAddresses, "modelAddresses": modelAddresses,

View File

@ -26,7 +26,7 @@ import "package:photos/services/machine_learning/face_ml/person/person_service.d
import 'package:photos/services/machine_learning/file_ml/file_ml.dart'; import 'package:photos/services/machine_learning/file_ml/file_ml.dart';
import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.dart'; import 'package:photos/services/machine_learning/file_ml/remote_fileml_service.dart';
import 'package:photos/services/machine_learning/ml_exceptions.dart'; import 'package:photos/services/machine_learning/ml_exceptions.dart';
import "package:photos/services/machine_learning/ml_isolate.dart"; 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/ml_result.dart';
import "package:photos/services/machine_learning/semantic_search/clip/clip_image_encoder.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/machine_learning/semantic_search/semantic_search_service.dart";
@ -138,13 +138,13 @@ class MLService {
void pauseIndexingAndClustering() { void pauseIndexingAndClustering() {
if (_isIndexingOrClusteringRunning) { if (_isIndexingOrClusteringRunning) {
_shouldPauseIndexingAndClustering = true; _shouldPauseIndexingAndClustering = true;
MLIsolate.instance.shouldPauseIndexingAndClustering = true; MLIndexingIsolate.instance.shouldPauseIndexingAndClustering = true;
} }
} }
void _cancelPauseIndexingAndClustering() { void _cancelPauseIndexingAndClustering() {
_shouldPauseIndexingAndClustering = false; _shouldPauseIndexingAndClustering = false;
MLIsolate.instance.shouldPauseIndexingAndClustering = false; MLIndexingIsolate.instance.shouldPauseIndexingAndClustering = false;
} }
/// Analyzes all the images in the database with the latest ml version and stores the results in the database. /// Analyzes all the images in the database with the latest ml version and stores the results in the database.
@ -380,7 +380,7 @@ class MLService {
bool actuallyRanML = false; bool actuallyRanML = false;
try { try {
final MLResult? result = await MLIsolate.instance.analyzeImage( final MLResult? result = await MLIndexingIsolate.instance.analyzeImage(
instruction, instruction,
); );
if (result == null) { if (result == null) {
@ -518,7 +518,7 @@ class MLService {
_logger.info( _logger.info(
'Loading models. faces: $shouldLoadFaces, clip: $shouldLoadClip', 'Loading models. faces: $shouldLoadFaces, clip: $shouldLoadClip',
); );
await MLIsolate.instance await MLIndexingIsolate.instance
.loadModels(loadFaces: shouldLoadFaces, loadClip: shouldLoadClip); .loadModels(loadFaces: shouldLoadFaces, loadClip: shouldLoadClip);
_logger.info('Models loaded'); _logger.info('Models loaded');
_logStatus(); _logStatus();