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