import 'dart:io'; import 'package:cutdex_native_flutter/processing.dart'; import 'package:path/path.dart' as p; /// App deployment configuration for non-realtime native processing. final class ProcessingConfiguration { const ProcessingConfiguration({ required this.backend, this.transcriptionModel, this.translationModel, this.vocalSeparationModel, this.backgroundRemovalModel, }); final CutdexProcessing backend; final Uri? transcriptionModel; final Uri? translationModel; final Uri? vocalSeparationModel; final Uri? backgroundRemovalModel; static String get _defaultResourcesRoot { if (Platform.isMacOS) { return p.join( Platform.environment['HOME'] ?? '', 'Documents', 'Cutdex', 'Resources', ); } if (Platform.isWindows) { return p.join( Platform.environment['USERPROFILE'] ?? '', 'Documents', 'Cutdex', 'Resources', ); } return p.join( Platform.environment['HOME'] ?? '', 'Documents', 'Cutdex', 'Resources', ); } static String get _bundledRuntimeRoot { final executable = File(Platform.resolvedExecutable); if (Platform.isMacOS) { return p.normalize( p.join(executable.parent.path, '..', 'Resources', 'processing'), ); } return p.join(executable.parent.path, 'processing'); } static ProcessingConfiguration? configured({ String? resourcesRoot, DiagnosticSink? diagnostics, }) { const definedRoot = String.fromEnvironment('CUTDEX_PROCESSING_ROOT'); final root = definedRoot.isNotEmpty ? definedRoot : Platform.environment['CUTDEX_PROCESSING_ROOT'] ?? _bundledRuntimeRoot; final modelsRoot = p.join( resourcesRoot ?? _defaultResourcesRoot, 'Processing', ); final transcriptionPath = p.join( modelsRoot, 'Transcription', 'whisper-turbo', '1', ); final vocalPath = p.join( modelsRoot, 'VocalSeparation', 'melband', '1', 'melband.pt', ); final translationPath = p.join( modelsRoot, 'Translation', 'm2m100-418m', '1', ); final backgroundRemovalPath = p.join( modelsRoot, 'BackgroundRemoval', 'birefnet-matting', '1', 'model.onnx', ); return fromPaths( root: root, transcriptionPath: transcriptionPath, vocalPath: vocalPath, translationPath: translationPath, backgroundRemovalPath: backgroundRemovalPath, diagnostics: diagnostics, ); } /// Resolves each installed capability independently; absent files disable only that capability. static ProcessingConfiguration? fromPaths({ required String root, required String transcriptionPath, required String vocalPath, required String translationPath, String? backgroundRemovalPath, DiagnosticSink? diagnostics, }) { if (!p.isAbsolute(root)) return null; final exe = Platform.isWindows ? '.exe' : ''; final executable = p.join(root, 'bin', 'cutdex-processing-worker$exe'); final ffmpeg = p.join(root, 'bin', 'ffmpeg$exe'); if (!File(executable).existsSync()) { return null; } final asrWorker = p.join(root, 'workers', 'asr', 'cutdex-asr-worker$exe'); final transcriptionModel = p.isAbsolute(transcriptionPath) && File(ffmpeg).existsSync() && File(asrWorker).existsSync() && File(p.join(transcriptionPath, 'ggml-model.bin')).existsSync() && File(p.join(transcriptionPath, 'silero_vad.onnx')).existsSync() ? Uri.directory(transcriptionPath) : null; final audioWorker = p.join( root, 'workers', 'audio', 'cutdex-audio-worker$exe', ); final m2m100Worker = p.join( root, 'workers', 'translation', 'cutdex-m2m100-worker$exe', ); final translationModel = p.isAbsolute(translationPath) && File(m2m100Worker).existsSync() && File(p.join(translationPath, 'model.bin')).existsSync() && File(p.join(translationPath, 'config.json')).existsSync() && File(p.join(translationPath, 'shared_vocabulary.json')) .existsSync() && File(p.join(translationPath, 'sentencepiece.bpe.model')) .existsSync() ? Uri.directory(translationPath) : null; final vocalSeparationModel = p.isAbsolute(vocalPath) && File(ffmpeg).existsSync() && File(audioWorker).existsSync() && File(vocalPath).existsSync() ? Uri.file(vocalPath) : null; final backgroundRemovalModel = backgroundRemovalPath != null && p.isAbsolute(backgroundRemovalPath) && File(ffmpeg).existsSync() && File(backgroundRemovalPath).existsSync() ? Uri.file(backgroundRemovalPath) : null; return ProcessingConfiguration( backend: CutdexProcessing.fromPackage( Directory(root), diagnostics: diagnostics, ), transcriptionModel: transcriptionModel, translationModel: translationModel, vocalSeparationModel: vocalSeparationModel, backgroundRemovalModel: backgroundRemovalModel, ); } }