Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix(dictionary): preserve voice training data

altic-devbf236434e9dd
diff --git a/Sources/Fluid/Persistence/BackupService.swift b/Sources/Fluid/Persistence/BackupService.swift
index 58795c47..60a85710 100644
--- a/Sources/Fluid/Persistence/BackupService.swift
+++ b/Sources/Fluid/Persistence/BackupService.swift
@@ -88,6 +88,7 @@ struct SettingsBackupPayload: Codable, Equatable {
let contextAwareCapitalizationEnabled: Bool?
let pauseMediaDuringTranscription: Bool
let automaticDictionaryLearningEnabled: Bool?
+ let pronunciationMatchingEnabled: Bool?
let vocabularyBoostingEnabled: Bool
let customDictionaryEntries: [SettingsStore.CustomDictionaryEntry]
let selectedDictationPromptID: String?
@@ -108,6 +109,9 @@ struct AppBackupDocument: Codable, Equatable {
let promptProfiles: [SettingsStore.DictationPromptProfile]
let appPromptBindings: [SettingsStore.AppPromptBinding]
let transcriptionHistory: [TranscriptionHistoryEntry]
+ // Optional so backups created before pronunciation matching still decode.
+ // swiftlint:disable:next discouraged_optional_collection
+ let pronunciationProfiles: [PronunciationDictionaryProfile]?
}
enum BackupServiceError: LocalizedError {
@@ -124,20 +128,23 @@ enum BackupServiceError: LocalizedError {
}
}
+@MainActor
final class BackupService {
static let shared = BackupService()
private init() {}
- func makeBackupDocument() -> AppBackupDocument {
- AppBackupDocument(
+ func makeBackupDocument() async -> AppBackupDocument {
+ let pronunciationProfiles = await PronunciationDictionaryStore.shared.allProfiles()
+ return AppBackupDocument(
schemaVersion: .current,
appVersion: Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "Unknown",
exportedAt: Date(),
settings: SettingsStore.shared.makeBackupPayload(),
promptProfiles: SettingsStore.shared.dictationPromptProfiles,
appPromptBindings: SettingsStore.shared.appPromptBindings,
- transcriptionHistory: TranscriptionHistoryStore.shared.makeBackupPayload()
+ transcriptionHistory: TranscriptionHistoryStore.shared.makeBackupPayload(),
+ pronunciationProfiles: pronunciationProfiles
)
}
@@ -164,8 +171,11 @@ final class BackupService {
}
}
- func restore(_ document: AppBackupDocument) throws {
+ func restore(_ document: AppBackupDocument) async throws {
try self.validate(document)
+ if let pronunciationProfiles = document.pronunciationProfiles {
+ try await PronunciationDictionaryStore.shared.replaceAllProfiles(pronunciationProfiles)
+ }
SettingsStore.shared.restore(
from: document.settings,
promptProfiles: document.promptProfiles,
diff --git a/Sources/Fluid/Persistence/SettingsStore.swift b/Sources/Fluid/Persistence/SettingsStore.swift
index 074486b2..bdf97656 100644
--- a/Sources/Fluid/Persistence/SettingsStore.swift
+++ b/Sources/Fluid/Persistence/SettingsStore.swift
@@ -2969,6 +2969,7 @@ final class SettingsStore: ObservableObject {
contextAwareCapitalizationEnabled: self.contextAwareCapitalizationEnabled,
pauseMediaDuringTranscription: self.pauseMediaDuringTranscription,
automaticDictionaryLearningEnabled: self.automaticDictionaryLearningEnabled,
+ pronunciationMatchingEnabled: self.pronunciationMatchingEnabled,
vocabularyBoostingEnabled: self.vocabularyBoostingEnabled,
customDictionaryEntries: self.customDictionaryEntries,
selectedDictationPromptID: self.selectedDictationPromptID,
@@ -3099,6 +3100,9 @@ final class SettingsStore: ObservableObject {
if let automaticDictionaryLearningEnabled = payload.automaticDictionaryLearningEnabled {
self.automaticDictionaryLearningEnabled = automaticDictionaryLearningEnabled
}
+ if let pronunciationMatchingEnabled = payload.pronunciationMatchingEnabled {
+ self.pronunciationMatchingEnabled = pronunciationMatchingEnabled
+ }
self.vocabularyBoostingEnabled = payload.vocabularyBoostingEnabled
self.customDictionaryEntries = payload.customDictionaryEntries
diff --git a/Sources/Fluid/Services/PronunciationDictionaryStore.swift b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
index ba7714a8..4ba56ffb 100644
--- a/Sources/Fluid/Services/PronunciationDictionaryStore.swift
+++ b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
@@ -1,12 +1,12 @@
import Foundation
-struct PronunciationEnrollmentCapture: Codable, Sendable {
+struct PronunciationEnrollmentCapture: Codable, Equatable, Sendable {
let values: [Float]
let sourceFrameCount: Int
let modelKey: String
}
-struct PronunciationDictionaryProfile: Codable, Identifiable, Sendable {
+struct PronunciationDictionaryProfile: Codable, Equatable, Identifiable, Sendable {
let dictionaryEntryID: UUID
var label: String
let modelKey: String
@@ -45,6 +45,15 @@ actor PronunciationDictionaryStore {
self.fileURL = appURL.appendingPathComponent("pronunciation-dictionary-v1.json")
}
+ init(fileURL: URL) {
+ self.fileURL = fileURL
+ }
+
+ func allProfiles() -> [PronunciationDictionaryProfile] {
+ self.loadIfNeeded()
+ return self.document?.profiles ?? []
+ }
+
func profiles(modelKey: String) -> [PronunciationDictionaryProfile] {
self.loadIfNeeded()
return self.document?.profiles.filter { $0.modelKey == modelKey } ?? []
@@ -69,16 +78,24 @@ actor PronunciationDictionaryStore {
}
self.loadIfNeeded()
var profiles = self.document?.profiles ?? []
+ let existingIndex = profiles.firstIndex(where: {
+ $0.dictionaryEntryID == dictionaryEntryID && $0.modelKey == modelKey
+ })
+ let existingEnrollments = existingIndex.map { profiles[$0].enrollments } ?? []
+ let combinedEnrollments = existingEnrollments + enrollments
+ guard combinedEnrollments.allSatisfy({
+ $0.modelKey == modelKey && $0.values.count == first.values.count
+ }) else {
+ throw PronunciationDictionaryStoreError.inconsistentEnrollment
+ }
let profile = PronunciationDictionaryProfile(
dictionaryEntryID: dictionaryEntryID,
label: label,
modelKey: modelKey,
hiddenSize: first.values.count,
- enrollments: Array(enrollments.suffix(10))
+ enrollments: Array(combinedEnrollments.suffix(10))
)
- if let index = profiles.firstIndex(where: {
- $0.dictionaryEntryID == dictionaryEntryID && $0.modelKey == modelKey
- }) {
+ if let index = existingIndex {
profiles[index] = profile
} else {
profiles.append(profile)
@@ -86,6 +103,19 @@ actor PronunciationDictionaryStore {
try self.persist(Document(version: 1, profiles: profiles))
}
+ func replaceAllProfiles(_ profiles: [PronunciationDictionaryProfile]) throws {
+ guard profiles.allSatisfy({ profile in
+ profile.hiddenSize > 0 &&
+ !profile.enrollments.isEmpty &&
+ profile.enrollments.allSatisfy {
+ $0.modelKey == profile.modelKey && $0.values.count == profile.hiddenSize
+ }
+ }) else {
+ throw PronunciationDictionaryStoreError.inconsistentEnrollment
+ }
+ try self.persist(Document(version: 1, profiles: profiles))
+ }
+
func updateLabel(dictionaryEntryID: UUID, label: String) throws {
self.loadIfNeeded()
var profiles = self.document?.profiles ?? []
diff --git a/Sources/Fluid/UI/CustomDictionaryView.swift b/Sources/Fluid/UI/CustomDictionaryView.swift
index 5e050f15..cc6a2c0a 100644
--- a/Sources/Fluid/UI/CustomDictionaryView.swift
+++ b/Sources/Fluid/UI/CustomDictionaryView.swift
@@ -1820,6 +1820,7 @@ struct CustomDictionaryView: View {
let transcript = await self.asr.stop(forDictionaryTraining: true)
self.isTrainingProcessing = false
if self.pronunciationMatchingEnabled,
+ CustomDictionaryTrainingMerge.normalizedTrigger(transcript) != nil,
let enrollment = self.asr.lastDictionaryTrainingResult?.pronunciationEnrollment
{
self.trainingPronunciationEnrollments.append(enrollment)
diff --git a/Sources/Fluid/UI/SettingsView.swift b/Sources/Fluid/UI/SettingsView.swift
index 730e2a76..356b5a7e 100644
--- a/Sources/Fluid/UI/SettingsView.swift
+++ b/Sources/Fluid/UI/SettingsView.swift
@@ -1606,6 +1606,10 @@ struct SettingsView: View {
}
private func exportBackup() {
+ Task { await self.performBackupExport() }
+ }
+
+ private func performBackupExport() async {
do {
let panel = NSSavePanel()
panel.canCreateDirectories = true
@@ -1614,7 +1618,7 @@ struct SettingsView: View {
guard panel.runModal() == .OK, let url = panel.url else { return }
- let document = BackupService.shared.makeBackupDocument()
+ let document = await BackupService.shared.makeBackupDocument()
let data = try BackupService.shared.encode(document)
try data.write(to: url, options: .atomic)
@@ -1631,6 +1635,10 @@ struct SettingsView: View {
}
private func importBackup() {
+ Task { await self.performBackupImport() }
+ }
+
+ private func performBackupImport() async {
do {
let panel = NSOpenPanel()
panel.canChooseDirectories = false
@@ -1661,7 +1669,7 @@ struct SettingsView: View {
guard confirm.runModal() == .alertFirstButtonReturn else { return }
- try BackupService.shared.restore(document)
+ try await BackupService.shared.restore(document)
self.syncLocalSettingsAfterBackupRestore()
self.presentInfoAlert(
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index 240ed67e..e05ba5f2 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -797,6 +797,67 @@ final class DictationE2ETests: XCTestCase {
}
}
+ func testPronunciationStoreRetainsPriorEnrollmentsWhenRetrained() async throws {
+ let fileURL = FileManager.default.temporaryDirectory
+ .appendingPathComponent("PronunciationStore-\(UUID().uuidString).json")
+ defer { try? FileManager.default.removeItem(at: fileURL) }
+ let store = PronunciationDictionaryStore(fileURL: fileURL)
+ let entryID = UUID()
+
+ let initialEnrollments = (0 ..< 8).map { value in
+ PronunciationEnrollmentCapture(
+ values: [Float(value), Float(value)],
+ sourceFrameCount: 1,
+ modelKey: "model-a"
+ )
+ }
+ let retrainedEnrollments = (8 ..< 13).map { value in
+ PronunciationEnrollmentCapture(
+ values: [Float(value), Float(value)],
+ sourceFrameCount: 1,
+ modelKey: "model-a"
+ )
+ }
+
+ try await store.upsert(
+ dictionaryEntryID: entryID,
+ label: "Barath",
+ modelKey: "model-a",
+ enrollments: initialEnrollments
+ )
+ try await store.upsert(
+ dictionaryEntryID: entryID,
+ label: "Barath",
+ modelKey: "model-a",
+ enrollments: retrainedEnrollments
+ )
+
+ let profiles = await store.profiles(modelKey: "model-a")
+ XCTAssertEqual(profiles.count, 1)
+ XCTAssertEqual(profiles.first?.enrollments.compactMap(\.values.first), (3 ..< 13).map { Float($0) })
+ }
+
+ func testPronunciationStoreRestoreRejectsMalformedProfiles() async {
+ let fileURL = FileManager.default.temporaryDirectory
+ .appendingPathComponent("PronunciationStore-\(UUID().uuidString).json")
+ defer { try? FileManager.default.removeItem(at: fileURL) }
+ let store = PronunciationDictionaryStore(fileURL: fileURL)
+ let malformedProfile = PronunciationDictionaryProfile(
+ dictionaryEntryID: UUID(),
+ label: "Barath",
+ modelKey: "model-a",
+ hiddenSize: 2,
+ enrollments: [PronunciationEnrollmentCapture(values: [1], sourceFrameCount: 1, modelKey: "model-a")]
+ )
+
+ do {
+ try await store.replaceAllProfiles([malformedProfile])
+ XCTFail("Expected malformed profile validation to fail")
+ } catch {
+ XCTAssertEqual(error as? PronunciationDictionaryStoreError, .inconsistentEnrollment)
+ }
+ }
+
func testProgressiveDownloaderRetainsFileByMovingIt() throws {
let source = FileManager.default.temporaryDirectory
.appendingPathComponent("FluidVoiceDownloadSource-\(UUID().uuidString)")