Fix Soniox vocabulary integration to read from SwiftData
This commit is contained in:
parent
60125c316b
commit
4e551926e7
@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
import os
|
||||
|
||||
enum CloudTranscriptionError: Error, LocalizedError {
|
||||
@ -34,14 +35,19 @@ enum CloudTranscriptionError: Error, LocalizedError {
|
||||
}
|
||||
|
||||
class CloudTranscriptionService: TranscriptionService {
|
||||
|
||||
private let modelContext: ModelContext
|
||||
|
||||
init(modelContext: ModelContext) {
|
||||
self.modelContext = modelContext
|
||||
}
|
||||
|
||||
private lazy var groqService = GroqTranscriptionService()
|
||||
private lazy var elevenLabsService = ElevenLabsTranscriptionService()
|
||||
private lazy var deepgramService = DeepgramTranscriptionService()
|
||||
private lazy var mistralService = MistralTranscriptionService()
|
||||
private lazy var geminiService = GeminiTranscriptionService()
|
||||
private lazy var openAICompatibleService = OpenAICompatibleTranscriptionService()
|
||||
private lazy var sonioxService = SonioxTranscriptionService()
|
||||
private lazy var sonioxService = SonioxTranscriptionService(modelContext: modelContext)
|
||||
|
||||
func transcribe(audioURL: URL, model: any TranscriptionModel) async throws -> String {
|
||||
var text: String
|
||||
|
||||
@ -1,7 +1,13 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
class SonioxTranscriptionService {
|
||||
private let apiBase = "https://api.soniox.com/v1"
|
||||
private let modelContext: ModelContext
|
||||
|
||||
init(modelContext: ModelContext) {
|
||||
self.modelContext = modelContext
|
||||
}
|
||||
|
||||
func transcribe(audioURL: URL, model: any TranscriptionModel) async throws -> String {
|
||||
let config = try getAPIConfig(for: model)
|
||||
@ -170,16 +176,16 @@ class SonioxTranscriptionService {
|
||||
}
|
||||
|
||||
private func getCustomDictionaryTerms() -> [String] {
|
||||
guard let data = UserDefaults.standard.data(forKey: "CustomVocabularyItems") else {
|
||||
// Fetch vocabulary words from SwiftData
|
||||
let descriptor = FetchDescriptor<VocabularyWord>(sortBy: [SortDescriptor(\.word)])
|
||||
guard let vocabularyWords = try? modelContext.fetch(descriptor) else {
|
||||
return []
|
||||
}
|
||||
// Decode without depending on UI layer types; extract "word" strings
|
||||
guard let json = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else {
|
||||
return []
|
||||
}
|
||||
let words = json.compactMap { $0["word"] as? String }
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
|
||||
let words = vocabularyWords
|
||||
.map { $0.word.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
|
||||
// De-duplicate while preserving order
|
||||
var seen = Set<String>()
|
||||
var unique: [String] = []
|
||||
|
||||
@ -12,7 +12,7 @@ class TranscriptionServiceRegistry {
|
||||
modelsDirectory: modelsDirectory,
|
||||
whisperState: whisperState
|
||||
)
|
||||
private(set) lazy var cloudTranscriptionService = CloudTranscriptionService()
|
||||
private(set) lazy var cloudTranscriptionService = CloudTranscriptionService(modelContext: whisperState.modelContext)
|
||||
private(set) lazy var nativeAppleTranscriptionService = NativeAppleTranscriptionService()
|
||||
private(set) lazy var parakeetTranscriptionService = ParakeetTranscriptionService()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user