All files / lib classification.svelte.ts

0% Statements 0/25
0% Branches 0/22
0% Functions 0/1
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                                                                                                                                                                       
import type { PROCEDURES } from '$worker/procedures.js';
import type { CancelablePromise, SwarpcClient } from 'swarpc';
 
import { RequestCancelledError } from 'swarpc';
 
import { databaseHandle, tables } from './idb.svelte.js';
import { inferenceModelId } from './inference.js';
import { storeMetadataErrors } from './metadata/storage.js';
import { metadataDefinitionComparator } from './protocols.js';
import { uiState } from './uistate.svelte.js';
import { safeJSONStringify } from './utils.js';
 
/**
 * Classifies an image using the current protocol with all configured classification models.
 */
export async function classifyImage(
	swarpc: SwarpcClient<typeof PROCEDURES>,
	id: string,
	cancellers: Map<string, CancelablePromise['cancel']>
) {
	if (!uiState.currentProtocol) {
		throw new Error('Aucun protocole sélectionné');
	}
 
	// Get all classification metadata for the current protocol
	const allClassificationMetadata = uiState.enabledClassificationMetadata
		// Sort them to have a consistent metadata inference order,
		// which matters when there are conflicting cascades
		.toSorted(metadataDefinitionComparator(uiState.currentProtocol));
 
	if (allClassificationMetadata.length === 0) {
		console.warn(
			'No metadata with neural inference defined, not analyzing image. Configure neural inference on enum metadata (set metadata.<your metadata id>.infer.neural) if this was not intentional.'
		);
		return;
	}
 
	// Classify with all metadata
	// Do it sequentially to avoid race conditions when storing metadata
	// see flakiness in e.g. https://github.com/cigaleapp/cigale/actions/runs/28614994005/job/84858867027#step:11:39
	for (const metadata of allClassificationMetadata) {
		const modelIndex = uiState.selectedClassificationModels[metadata.id] ?? 0;
		const allModels = uiState.allClassificationModels[metadata.id];
 
		if (!allModels || !allModels[modelIndex]) {
			console.warn(
				`No model found for metadata ${metadata.id} at index ${modelIndex}, skipping`
			);
			return;
		}
 
		const settings = $state.snapshot(allModels[modelIndex]);
 
		// Generate the inference session ID based on the protocol and model
		const inferenceSessionId = inferenceModelId(uiState.currentProtocol.id, settings.model);
 
		const taskSettings = {
			...settings,
			output: {
				name: settings.output?.name ?? 'output0',
				// XXX: $state.snapshot turns the type of output.select into a string, idk why cuz at runtime it isnt
				select: allModels[modelIndex].output?.select?.toJSON(),
			},
		};
 
		const { cancel, request: done } = swarpc.classify.cancelable({
			imageId: id,
			taskSettings,
			inferenceSessionId,
			metadataIds: {
				cropbox: uiState.cropMetadataId,
				target: metadata.id,
			},
		});
 
		cancellers?.set(`${id}:${metadata.id}`, cancel);
 
		try {
			await done;
		} catch (error) {
			if (error instanceof RequestCancelledError) throw error;
			if (!uiState.currentSessionId) throw error;
 
			await storeMetadataErrors(
				{
					db: databaseHandle(),
					subjectId: id,
					sessionId: uiState.currentSessionId,
					metadataId: metadata.id,
				},
				{
					kind: 'inference',
					message: error instanceof Error ? error.message : String(error),
					stack: error instanceof Error ? (error.stack ?? '') : '',
					ignored: false,
					details: {
						taskSettings,
						image: await tables.Image.get(id),
						fullError: safeJSONStringify(error),
					},
				}
			);
		}
	}
 
	await tables.Image.refresh(uiState.currentSessionId);
}