Skip to content

Commit

Permalink
fix #113 -- fall back to local files if offline (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkoski authored Sep 27, 2024
1 parent 30e3063 commit 0907d7f
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
25 changes: 19 additions & 6 deletions Applications/StableDiffusionExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,25 @@ actor ModelFactory {
switch loadState {
case .idle:
let task = Task {
try await configuration.download { progress in
if progress.fractionCompleted < 0.99 {
reportProgress(
.init(
title: "Download", current: progress.fractionCompleted * 100,
limit: 100))
do {
try await configuration.download { progress in
if progress.fractionCompleted < 0.99 {
reportProgress(
.init(
title: "Download", current: progress.fractionCompleted * 100,
limit: 100))
}
}
} catch {
let nserror = error as NSError
if nserror.domain == NSURLErrorDomain
&& nserror.code == NSURLErrorNotConnectedToInternet
{
// Internet connection appears to be offline -- fall back to loading from
// the local directory
reportProgress(.init(title: "Offline", current: 100, limit: 100))
} else {
throw error
}
}

Expand Down
9 changes: 9 additions & 0 deletions Libraries/LLM/Load.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func prepareModelDirectory(
// an authorizationRequired means (typically) that the named repo doesn't exist on
// on the server so retry with local only configuration
return configuration.modelDirectory(hub: hub)
} catch {
let nserror = error as NSError
if nserror.domain == NSURLErrorDomain && nserror.code == NSURLErrorNotConnectedToInternet {
// Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline."
// fall back to the local directory
return configuration.modelDirectory(hub: hub)
} else {
throw error
}
}
}

Expand Down
21 changes: 19 additions & 2 deletions Libraries/LLM/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,25 @@ func loadTokenizerConfig(configuration: ModelConfiguration, hub: HubApi) async t

switch configuration.id {
case .id(let id):
config = LanguageModelConfigurationFromHub(
modelName: configuration.tokenizerId ?? id, hubApi: hub)
do {
// the load can fail (async when we try to use it)
let loaded = LanguageModelConfigurationFromHub(
modelName: configuration.tokenizerId ?? id, hubApi: hub)
_ = try await loaded.tokenizerConfig
config = loaded
} catch {
let nserror = error as NSError
if nserror.domain == NSURLErrorDomain
&& nserror.code == NSURLErrorNotConnectedToInternet
{
// Internet connection appears to be offline -- fall back to loading from
// the local directory
config = LanguageModelConfigurationFromHub(
modelFolder: configuration.modelDirectory(hub: hub), hubApi: hub)
} else {
throw error
}
}
case .directory(let directory):
config = LanguageModelConfigurationFromHub(modelFolder: directory, hubApi: hub)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C36BF0002BC5CE55002D4AFE"
BuildableName = "StableDiffusionExample.app"
BlueprintName = "StableDiffusionExample"
ReferencedContainer = "container:mlx-swift-examples.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C36BF0002BC5CE55002D4AFE"
BuildableName = "StableDiffusionExample.app"
BlueprintName = "StableDiffusionExample"
ReferencedContainer = "container:mlx-swift-examples.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C36BF0002BC5CE55002D4AFE"
BuildableName = "StableDiffusionExample.app"
BlueprintName = "StableDiffusionExample"
ReferencedContainer = "container:mlx-swift-examples.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

0 comments on commit 0907d7f

Please sign in to comment.