import Foundation import SstCore import WhisperKit public final class WhisperKitTranscriber: Transcriber { public enum TranscriberError: Error, LocalizedError { case notLoaded public var errorDescription: String? { "Spraakmodel is nog niet geladen." } } private let modelName: String private var pipeline: WhisperKit? public init(model: String = "large-v3-v20240930_turbo") { self.modelName = model } /// Downloads the model on first run (≈1.5 GB) and loads it into memory. public func load() async throws { pipeline = try await WhisperKit(WhisperKitConfig(model: modelName)) } public func transcribe(_ samples: [Float]) async throws -> String { guard let pipeline else { throw TranscriberError.notLoaded } // language nil + detectLanguage: Whisper picks NL or EN per utterance. let options = DecodingOptions( task: .transcribe, language: nil, detectLanguage: true, skipSpecialTokens: true, withoutTimestamps: true ) let results = try await pipeline.transcribe(audioArray: samples, decodeOptions: options) return results .map(\.text) .joined(separator: " ") .trimmingCharacters(in: .whitespacesAndNewlines) } }