From 54a2ef58e6f3cdc9e9d18aac31a51ca7e4653dc7 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Thu, 19 Jun 2025 17:33:04 +0545 Subject: [PATCH] Simplified language selection for Apple Native models --- VoiceInk/Models/PredefinedModels.swift | 29 +++++++++++-------- .../NativeAppleTranscriptionService.swift | 20 ++++++++++--- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/VoiceInk/Models/PredefinedModels.swift b/VoiceInk/Models/PredefinedModels.swift index 94f3376..1e3d7c7 100644 --- a/VoiceInk/Models/PredefinedModels.swift +++ b/VoiceInk/Models/PredefinedModels.swift @@ -27,10 +27,15 @@ import Foundation } enum PredefinedModels { - static func getLanguageDictionary(isMultilingual: Bool) -> [String: String] { + static func getLanguageDictionary(isMultilingual: Bool, provider: ModelProvider = .local) -> [String: String] { if !isMultilingual { return ["en": "English"] } else { + // For Apple Native models, return only supported languages in simple format + if provider == .nativeApple { + let appleSupportedCodes = ["en", "es", "fr", "de"] + return allLanguages.filter { appleSupportedCodes.contains($0.key) } + } return allLanguages } } @@ -105,14 +110,14 @@ import Foundation displayName: "Apple Speech", description: "Uses the native Apple Speech framework for transcription. Available on macOS Sonoma 14+.", isMultilingualModel: true, - supportedLanguages: appleNativeLanguages + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .nativeApple) ), // Local Models LocalModel( name: "ggml-tiny", displayName: "Tiny", size: "75 MiB", - supportedLanguages: getLanguageDictionary(isMultilingual: true), + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .local), description: "Tiny model, fastest, least accurate", speed: 0.95, accuracy: 0.6, @@ -123,7 +128,7 @@ import Foundation name: "ggml-tiny.en", displayName: "Tiny (English)", size: "75 MiB", - supportedLanguages: getLanguageDictionary(isMultilingual: false), + supportedLanguages: getLanguageDictionary(isMultilingual: false, provider: .local), description: "Tiny model optimized for English, fastest, least accurate", speed: 0.95, accuracy: 0.65, @@ -134,7 +139,7 @@ import Foundation name: "ggml-base.en", displayName: "Base (English)", size: "142 MiB", - supportedLanguages: getLanguageDictionary(isMultilingual: false), + supportedLanguages: getLanguageDictionary(isMultilingual: false, provider: .local), description: "Base model optimized for English, good balance between speed and accuracy", speed: 0.85, accuracy: 0.75, @@ -145,7 +150,7 @@ import Foundation name: "ggml-large-v2", displayName: "Large v2", size: "2.9 GiB", - supportedLanguages: getLanguageDictionary(isMultilingual: true), + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .local), description: "Large model v2, slower than Medium but more accurate", speed: 0.3, accuracy: 0.96, @@ -156,7 +161,7 @@ import Foundation name: "ggml-large-v3", displayName: "Large v3", size: "2.9 GiB", - supportedLanguages: getLanguageDictionary(isMultilingual: true), + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .local), description: "Large model v3, very slow but most accurate", speed: 0.3, accuracy: 0.98, @@ -167,7 +172,7 @@ import Foundation name: "ggml-large-v3-turbo", displayName: "Large v3 Turbo", size: "1.5 GiB", - supportedLanguages: getLanguageDictionary(isMultilingual: true), + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .local), description: "Large model v3 Turbo, faster than v3 with similar accuracy", speed: 0.75, @@ -179,7 +184,7 @@ import Foundation name: "ggml-large-v3-turbo-q5_0", displayName: "Large v3 Turbo (Quantized)", size: "547 MiB", - supportedLanguages: getLanguageDictionary(isMultilingual: true), + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .local), description: "Quantized version of Large v3 Turbo, faster with slightly lower accuracy", speed: 0.75, accuracy: 0.95, @@ -196,7 +201,7 @@ import Foundation speed: 0.65, accuracy: 0.96, isMultilingual: true, - supportedLanguages: getLanguageDictionary(isMultilingual: true) + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .groq) ), CloudModel( name: "scribe_v1", @@ -206,7 +211,7 @@ import Foundation speed: 0.7, accuracy: 0.98, isMultilingual: true, - supportedLanguages: getLanguageDictionary(isMultilingual: true) + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .elevenLabs) ), CloudModel( name: "nova-2", @@ -216,7 +221,7 @@ import Foundation speed: 0.9, accuracy: 0.95, isMultilingual: true, - supportedLanguages: getLanguageDictionary(isMultilingual: true) + supportedLanguages: getLanguageDictionary(isMultilingual: true, provider: .deepgram) ), ] diff --git a/VoiceInk/Services/NativeAppleTranscriptionService.swift b/VoiceInk/Services/NativeAppleTranscriptionService.swift index 62d4c85..9444815 100644 --- a/VoiceInk/Services/NativeAppleTranscriptionService.swift +++ b/VoiceInk/Services/NativeAppleTranscriptionService.swift @@ -11,6 +11,17 @@ import Speech class NativeAppleTranscriptionService: TranscriptionService { private let logger = Logger(subsystem: "com.prakashjoshipax.voiceink", category: "NativeAppleTranscriptionService") + /// Maps simple language codes to Apple's BCP-47 locale format + private func mapToAppleLocale(_ simpleCode: String) -> String { + let mapping = [ + "en": "en-US", + "es": "es-ES", + "fr": "fr-FR", + "de": "de-DE" + ] + return mapping[simpleCode] ?? "en-US" + } + enum ServiceError: Error, LocalizedError { case unsupportedOS case transcriptionFailed @@ -46,9 +57,10 @@ class NativeAppleTranscriptionService: TranscriptionService { let audioFile = try AVAudioFile(forReading: audioURL) - // Use the user's selected language directly, assuming BCP-47 format. - let selectedLanguage = UserDefaults.standard.string(forKey: "SelectedLanguage") ?? "en-US" - let locale = Locale(identifier: selectedLanguage) + // Get the user's selected language in simple format and convert to BCP-47 format + let selectedLanguage = UserDefaults.standard.string(forKey: "SelectedLanguage") ?? "en" + let appleLocale = mapToAppleLocale(selectedLanguage) + let locale = Locale(identifier: appleLocale) // Check for locale support and asset installation status. let supportedLocales = await SpeechTranscriber.supportedLocales @@ -73,7 +85,7 @@ class NativeAppleTranscriptionService: TranscriptionService { let logMessage = """ --- Native Speech Transcription --- - Locale: '\(locale.identifier)' + Selected Language: '\(selectedLanguage)' → Apple Locale: '\(locale.identifier)' Status: \(statusMessage) ------------------------------------ Supported Locales: [\(supportedIdentifiers)]