mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
A TestFlight tester (build 120, iPhone 15 Pro Max, iOS 26.6) had Experiments ›
Multilingual model (Parakeet) on: voice input "started demanding Set up", then
sat at "Downloading 632.2 MB · Wi-Fi recommended" with no movement.
Why it blocked: with the experiment on and the dictation language one of
Parakeet Ultra's 25 (English included), Ultra became Hex's engine at once,
before it was downloaded. Hex's phase stayed on preparing (or failed, which
shows Set up) for the whole 632 MB download and the first Core ML compile, and
the keyboard's Dictate and Hex key opened Hex for it whether or not the model
was there, so nothing dictated anywhere until it finished.
Why it sat still: the download ran in prepare() on a foreground URLSession,
one 594 MB encoder file, shown only as a bar at 70% scale under a label that
never changed. Leaving Hex (the keyboard's handoff sends you back to the app you
type in) or Auto-Lock suspends it; on return a dead connection showed nothing
for up to 60-90 s, then failed to Set up.
What changed:
- Until Ultra is ready, Hex and the keyboard dictate with the engine they use
with the experiment off. ParakeetUltraDownload (new) downloads beside it and
ParakeetUltraTranscriber.prepare only loads (throws notDownloaded otherwise).
A first load (Core ML's compile) runs beside the usual engine
(DictationController.loadMultilingualParakeetBeside) and switches to Ultra
when loaded; launches load Ultra directly only once downloaded and compiled
for this iOS (multilingualStep). Where V2 dictates and leaves no room
(English before iOS 26), Ultra takes V2's place for that load.
- The keyboard opens Hex for the experiment only once Hex has loaded it
(MultilingualParakeetSettings.isModelReady in the App Group, set after a
load, cleared while missing, set aside or off).
- The download shows bytes ("Downloading the multilingual model: 123.4 MB of
632.2 MB" with a bar) on home and under the switch; resumes every file from its
.partial (launch, retry, return to the front); a drop or 20 s without bytes
tries again after 2, 4, 8, 16, 30 s, starting over whenever it got further; in
the background it waits and picks up at once on return (fresh connection after
25 s away). After 6 fruitless tries, a refused request or a full iPhone it says
what happened in plain words with Try again and Turn off.
- A failed Ultra load or a load that ended Hex (ParakeetLoadGuard) falls back
to the usual engine and says so with Try again (no longer only by turning the
switch off, which deletes the 632 MB).
- A launch deletes a leftover download only when the switch is stored as off
(isTurnedOff), not when the App Group reads empty (a launch before the first
unlock would have deleted a good model). Turning it off now stops the download
and waits for it before deleting.
- Removed: ParakeetUltraTranscriber.downloadDetail, its .downloadFailed error
and the download/StallWatch inside its prepare (replaced by the above);
strings "Downloading %@ · Wi-Fi recommended" and the old switch detail.
New strings have zh-Hans. README describes the experiment's new behaviour.
- DEBUG builds read HEX_ULTRA_MODEL_BASE to download from a stand-in server.
Verified:
- xcodebuild test, iPhone 17 Pro Max iOS 26.5 simulator on the Mac Studio:
753 tests, 0 failures, 3 skipped (new cases in DictationControllerTests:
resume from .partial with Range, drop + stall retries, a moving download never
running out of tries, 503 x6 / 404 failures with their words, stop before
delete, keyboard flag, stored-off rule, multilingualStep).
- iPhone 15 Pro Max iOS 26.5 simulator, experiment on, a local stand-in server
(Range, throttled, drop/stall switches): Dictate stayed available with the
usual engine (V2 here; the sim has no Apple Speech) while the status counted
MB; a dropped connection said "Trying again in 1 s" and resumed with Range; a
20 s stall said so and resumed; Hex killed mid-download resumed from the
partial at relaunch; backgrounded with the connection cut, it resumed at once
on return; after a kill the resumed download from real Hugging Face finished
from a partial and passed the hashes; Ultra loaded beside and took over (setup
sheet "English · Parakeet Ultra", keyboard flag set); a forced load guard
showed the failure with Try again, which loaded Ultra; deleting the model with
the flag set cleared it at launch and downloaded again; the keyboard logged
"multilingual model on, Dictate opens Hex" only when ready (else the sim's own
"speech model missing" reason) and its Hex key went straight to recording
instead of the download screen; turning it off deleted the folder and cleared
the flag; with it off nothing shows or downloads.
- Not verified: a real iPhone, a real slow or flaky network, iOS's own
suspension timing, the V2-replacement path (sims report no memory limit), and
real audio (the Studio has no microphone).
149 lines
6.4 KiB
Swift
149 lines
6.4 KiB
Swift
import FluidAudio
|
||
import Foundation
|
||
import os
|
||
|
||
/// Experiments › Multilingual model (Parakeet): Parakeet Ultra (`ParakeetUltraModel`) through
|
||
/// FluidAudio's v3 decoder, in Hex's own process. It hears its 25 languages without being told
|
||
/// which, mixed within a sentence, so `languageIdentifier` only names the dictation language
|
||
/// for the steps after it (formatting, lists, cleanup).
|
||
actor ParakeetUltraTranscriber: LocalTranscribing {
|
||
enum TranscriberError: HexDescribedError {
|
||
case notPrepared
|
||
case emptyTranscript
|
||
case notEnoughMemory
|
||
/// Not on the iPhone yet: `ParakeetUltraDownload` gets it beside the usual engine.
|
||
case notDownloaded
|
||
|
||
var errorDescription: String? {
|
||
switch self {
|
||
case .notPrepared:
|
||
String(localized: "Parakeet Ultra is not ready.")
|
||
case .emptyTranscript:
|
||
String(localized: "Parakeet did not detect any speech.")
|
||
case .notEnoughMemory:
|
||
String(localized: "Parakeet Ultra needs more memory than Hex has here.")
|
||
case .notDownloaded:
|
||
String(localized: "Parakeet Ultra isn't downloaded yet.")
|
||
}
|
||
}
|
||
}
|
||
|
||
static let name = "Parakeet Ultra"
|
||
/// After a load that ended Hex (`ParakeetLoadGuard`), beside Try again.
|
||
nonisolated static var setAsideMessage: String {
|
||
String(localized: "Hex closed while getting the multilingual model ready, likely short of memory.")
|
||
}
|
||
/// `ParakeetLoadGuard`'s and `ParakeetCompileRecord`'s keys, apart from V2's.
|
||
static let loadGuardModel = "parakeetUltra"
|
||
static let compileRecordKey = "parakeetUltra.compiledForSystem"
|
||
|
||
/// What loading it takes in Hex, as `ParakeetTranscriber.loadMemory` for V2: the encoder
|
||
/// is 594 MB of int8 weights, which the Neural Engine may hold as 16-bit (1.2 GB) before
|
||
/// the A17, then the other three models (a joint four times V2's) and a first transcription.
|
||
nonisolated static let loadMemory: UInt64 = 1_500 << 20
|
||
|
||
/// Whether `available` (`os_proc_available_memory`) leaves room to load it. 0 means iOS
|
||
/// didn't say (the simulator).
|
||
nonisolated static func hasRoomToLoad(available: UInt64) -> Bool {
|
||
available == 0 || available >= loadMemory
|
||
}
|
||
|
||
nonisolated let displayName = ParakeetUltraTranscriber.name
|
||
let languageIdentifier: String
|
||
private var manager: AsrManager?
|
||
|
||
init(language: String) {
|
||
languageIdentifier = language
|
||
}
|
||
|
||
var languageName: String {
|
||
SpeechLanguagePicker.displayName(for: languageIdentifier)
|
||
}
|
||
|
||
/// The language the experiment dictates in now, or nil for the engine Hex uses without it:
|
||
/// off, an iPhone without the memory, a language it doesn't know, a load that ended Hex
|
||
/// (`ParakeetLoadGuard`, until the experiment is turned off and on), or less memory left
|
||
/// than loading takes (`os_proc_available_memory`, 0 where iOS sets no limit).
|
||
nonisolated static func chosenLanguage(
|
||
language: String? = MultilingualParakeetSettings().language(),
|
||
availableMemory: UInt64 = UInt64(max(0, os_proc_available_memory())),
|
||
setAside: Bool = ParakeetLoadGuard(model: loadGuardModel).lastLoadWasInterrupted
|
||
) -> String? {
|
||
guard let language, !setAside, hasRoomToLoad(available: availableMemory) else { return nil }
|
||
return language
|
||
}
|
||
|
||
/// "632.2 MB", kept on one line.
|
||
nonisolated static var downloadSize: String {
|
||
ByteCountFormatter.string(fromByteCount: ParakeetUltraModel.totalBytes, countStyle: .file)
|
||
.replacingOccurrences(of: " ", with: "\u{00A0}")
|
||
}
|
||
|
||
/// Whether Core ML compiled it for this iOS already, so loading it takes seconds rather
|
||
/// than the minute or more of a first load.
|
||
nonisolated static var isCompiled: Bool {
|
||
ParakeetCompileRecord(key: compileRecordKey).isCurrent
|
||
}
|
||
|
||
/// Loads the downloaded model; the download itself is `ParakeetUltraDownload`'s, beside
|
||
/// the engine Hex dictates with meanwhile.
|
||
func prepare(
|
||
progressHandler: @escaping @Sendable (ModelPreparationProgress) -> Void
|
||
) async throws {
|
||
guard manager == nil else { return }
|
||
guard ParakeetUltraModel.isDownloaded() else { throw TranscriberError.notDownloaded }
|
||
|
||
let compiled = ParakeetCompileRecord(key: Self.compileRecordKey)
|
||
let progress = ParakeetProgressFeed(
|
||
ParakeetPreparationProgress(downloads: false, compiledBefore: compiled.isCurrent),
|
||
emit: progressHandler
|
||
)
|
||
let ticker = Task {
|
||
while !Task.isCancelled {
|
||
progress.tick()
|
||
try? await Task.sleep(for: .milliseconds(250))
|
||
}
|
||
}
|
||
defer { ticker.cancel() }
|
||
|
||
// As V2: only the load, where iOS can end Hex short of memory, counts for the guard,
|
||
// and too little left to load it sets it aside for the engine Hex uses without it.
|
||
let loads = ParakeetLoadGuard(model: Self.loadGuardModel)
|
||
guard Self.hasRoomToLoad(available: UInt64(max(0, os_proc_available_memory()))) else {
|
||
loads.setAside()
|
||
throw TranscriberError.notEnoughMemory
|
||
}
|
||
loads.begin()
|
||
var loaded = false
|
||
defer { loads.end(loaded: loaded) }
|
||
progress.update { $0.startLoading(at: Date.now.timeIntervalSinceReferenceDate) }
|
||
let models = try ParakeetUltraModel.load {
|
||
let now = Date.now.timeIntervalSinceReferenceDate
|
||
progress.update { $0.finishModel(at: now) }
|
||
}
|
||
let manager = AsrManager(config: .default)
|
||
try await manager.loadModels(models)
|
||
self.manager = manager
|
||
loaded = true
|
||
compiled.markCurrent()
|
||
progress.update { $0.finish() }
|
||
}
|
||
|
||
func transcribe(_ audioURL: URL) async throws -> String {
|
||
guard let manager else {
|
||
throw TranscriberError.notPrepared
|
||
}
|
||
let decoderLayers = await manager.decoderLayerCount
|
||
var decoderState = TdtDecoderState.make(decoderLayers: decoderLayers)
|
||
let result = try await manager.transcribe(audioURL, decoderState: &decoderState)
|
||
let text = result.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
guard !text.isEmpty else {
|
||
throw TranscriberError.emptyTranscript
|
||
}
|
||
return text
|
||
}
|
||
}
|
||
|
||
extension ParakeetUltraTranscriber.TranscriberError: NoSpeechError {
|
||
var meansNoSpeech: Bool { self == .emptyTranscript }
|
||
}
|