2023-12-14 11:55:40 -05:00
|
|
|
import { ImmichLogger } from '@app/infra/logger';
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-12-08 11:15:46 -05:00
|
|
|
import { setTimeout } from 'timers/promises';
|
2023-05-22 20:05:06 +02:00
|
|
|
import { usePagination } from '../domain.util';
|
2023-12-08 11:15:46 -05:00
|
|
|
import { IBaseJob, IEntityJob, JOBS_ASSET_PAGINATION_SIZE, JobName, QueueName } from '../job';
|
2023-10-09 10:25:03 -04:00
|
|
|
import {
|
2023-12-27 18:36:51 -05:00
|
|
|
DatabaseLock,
|
2023-10-09 10:25:03 -04:00
|
|
|
IAssetRepository,
|
2023-12-27 18:36:51 -05:00
|
|
|
IDatabaseRepository,
|
2023-10-09 10:25:03 -04:00
|
|
|
IJobRepository,
|
|
|
|
|
IMachineLearningRepository,
|
|
|
|
|
ISmartInfoRepository,
|
|
|
|
|
ISystemConfigRepository,
|
|
|
|
|
WithoutProperty,
|
|
|
|
|
} from '../repositories';
|
|
|
|
|
import { SystemConfigCore } from '../system-config';
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SmartInfoService {
|
2023-08-25 00:15:03 -04:00
|
|
|
private configCore: SystemConfigCore;
|
2023-12-14 11:55:40 -05:00
|
|
|
private logger = new ImmichLogger(SmartInfoService.name);
|
2023-02-25 09:12:03 -05:00
|
|
|
|
|
|
|
|
constructor(
|
2023-03-20 11:55:28 -04:00
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
2023-12-27 18:36:51 -05:00
|
|
|
@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository,
|
2023-03-18 08:44:42 -05:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-02-25 09:12:03 -05:00
|
|
|
@Inject(IMachineLearningRepository) private machineLearning: IMachineLearningRepository,
|
2023-12-27 18:36:51 -05:00
|
|
|
@Inject(ISmartInfoRepository) private repository: ISmartInfoRepository,
|
|
|
|
|
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
|
2023-08-25 00:15:03 -04:00
|
|
|
) {
|
2023-10-09 02:51:03 +02:00
|
|
|
this.configCore = SystemConfigCore.create(configRepository);
|
2023-08-25 00:15:03 -04:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2023-12-08 11:15:46 -05:00
|
|
|
async init() {
|
2023-12-16 11:50:46 -05:00
|
|
|
await this.jobRepository.pause(QueueName.SMART_SEARCH);
|
2023-12-08 11:15:46 -05:00
|
|
|
|
2023-12-16 11:50:46 -05:00
|
|
|
let { isActive } = await this.jobRepository.getQueueStatus(QueueName.SMART_SEARCH);
|
2023-12-08 11:15:46 -05:00
|
|
|
while (isActive) {
|
|
|
|
|
this.logger.verbose('Waiting for CLIP encoding queue to stop...');
|
|
|
|
|
await setTimeout(1000).then(async () => {
|
2023-12-16 11:50:46 -05:00
|
|
|
({ isActive } = await this.jobRepository.getQueueStatus(QueueName.SMART_SEARCH));
|
2023-12-08 11:15:46 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
|
|
|
|
|
2023-12-27 18:36:51 -05:00
|
|
|
await this.databaseRepository.withLock(DatabaseLock.CLIPDimSize, () =>
|
|
|
|
|
this.repository.init(machineLearning.clip.modelName),
|
|
|
|
|
);
|
2023-12-08 11:15:46 -05:00
|
|
|
|
2023-12-16 11:50:46 -05:00
|
|
|
await this.jobRepository.resume(QueueName.SMART_SEARCH);
|
2023-12-08 11:15:46 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
async handleQueueEncodeClip({ force }: IBaseJob) {
|
2023-08-25 00:15:03 -04:00
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
2023-08-29 09:58:00 -04:00
|
|
|
if (!machineLearning.enabled || !machineLearning.clip.enabled) {
|
2023-08-25 00:15:03 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
|
return force
|
|
|
|
|
? this.assetRepository.getAll(pagination)
|
|
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.CLIP_ENCODING);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.ENCODE_CLIP, data: { id: asset.id } });
|
2023-03-20 11:55:28 -04:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|
2023-05-26 15:43:24 -04:00
|
|
|
|
|
|
|
|
return true;
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|
2023-03-18 08:44:42 -05:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
async handleEncodeClip({ id }: IEntityJob) {
|
2023-08-25 00:15:03 -04:00
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
2023-08-29 09:58:00 -04:00
|
|
|
if (!machineLearning.enabled || !machineLearning.clip.enabled) {
|
2023-08-25 00:15:03 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-03-18 08:44:42 -05:00
|
|
|
|
2023-08-25 00:15:03 -04:00
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
|
|
|
|
if (!asset.resizePath) {
|
2023-05-26 15:43:24 -04:00
|
|
|
return false;
|
2023-03-18 08:44:42 -05:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 09:58:00 -04:00
|
|
|
const clipEmbedding = await this.machineLearning.encodeImage(
|
|
|
|
|
machineLearning.url,
|
|
|
|
|
{ imagePath: asset.resizePath },
|
|
|
|
|
machineLearning.clip,
|
|
|
|
|
);
|
|
|
|
|
|
2023-12-27 18:36:51 -05:00
|
|
|
if (this.databaseRepository.isBusy(DatabaseLock.CLIPDimSize)) {
|
|
|
|
|
this.logger.verbose(`Waiting for CLIP dimension size to be updated`);
|
|
|
|
|
await this.databaseRepository.wait(DatabaseLock.CLIPDimSize);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 11:15:46 -05:00
|
|
|
await this.repository.upsert({ assetId: asset.id }, clipEmbedding);
|
2023-05-26 15:43:24 -04:00
|
|
|
|
|
|
|
|
return true;
|
2023-03-18 08:44:42 -05:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|