File tree Expand file tree Collapse file tree 7 files changed +84
-10
lines changed
Applications/StableDiffusionExample Expand file tree Collapse file tree 7 files changed +84
-10
lines changed Original file line number Diff line number Diff line change @@ -105,8 +105,18 @@ actor ModelFactory {
105
105
case loaded( ModelContainer < TextToImageGenerator > )
106
106
}
107
107
108
- enum SDError : Error {
108
+ enum SDError : LocalizedError {
109
109
case unableToLoad
110
+
111
+ var errorDescription : String ? {
112
+ switch self {
113
+ case . unableToLoad:
114
+ return String (
115
+ localized:
116
+ " Unable to load the Stable Diffusion model. Please check your internet connection or available storage space. "
117
+ )
118
+ }
119
+ }
110
120
}
111
121
112
122
public nonisolated let configuration = StableDiffusionConfiguration . presetSDXLTurbo
Original file line number Diff line number Diff line change 2
2
3
3
import Foundation
4
4
5
- enum LoRADataError : Error {
5
+ enum LoRADataError : LocalizedError {
6
6
case fileNotFound( URL , String )
7
+
8
+ var errorDescription : String ? {
9
+ switch self {
10
+ case . fileNotFound( let directory, let name) :
11
+ return String (
12
+ localized: " Could not find data file ' \( name) ' in directory ' \( directory. path ( ) ) '. " )
13
+ }
14
+ }
7
15
}
8
16
9
17
/// Load a LoRA data file.
Original file line number Diff line number Diff line change @@ -4,9 +4,16 @@ import Foundation
4
4
import Hub
5
5
import Tokenizers
6
6
7
- public enum ModelFactoryError : Error {
7
+ public enum ModelFactoryError : LocalizedError {
8
8
case unsupportedModelType( String )
9
9
case unsupportedProcessorType( String )
10
+
11
+ public var errorDescription : String ? {
12
+ switch self {
13
+ case . unsupportedModelType( let type) : " Unsupported model type: \( type) "
14
+ case . unsupportedProcessorType( let type) : " Unsupported processor type: \( type) "
15
+ }
16
+ }
10
17
}
11
18
12
19
/// Context of types that work together to provide a ``LanguageModel``.
Original file line number Diff line number Diff line change @@ -175,10 +175,21 @@ public protocol UserInputProcessor {
175
175
func prepare( input: UserInput ) async throws -> LMInput
176
176
}
177
177
178
- private enum UserInputError : Error {
178
+ private enum UserInputError : LocalizedError {
179
179
case notImplemented
180
180
case unableToLoad( URL )
181
181
case arrayError( String )
182
+
183
+ var errorDescription : String ? {
184
+ switch self {
185
+ case . notImplemented:
186
+ return String ( localized: " This functionality is not implemented. " )
187
+ case . unableToLoad( let url) :
188
+ return String ( localized: " Unable to load image from URL: \( url. path) . " )
189
+ case . arrayError( let message) :
190
+ return String ( localized: " Error processing image array: \( message) . " )
191
+ }
192
+ }
182
193
}
183
194
184
195
/// A do-nothing ``UserInputProcessor``.
Original file line number Diff line number Diff line change @@ -6,12 +6,27 @@ import MLX
6
6
import MLXLMCommon
7
7
import Tokenizers
8
8
9
- public enum VLMError : Error {
9
+ public enum VLMError : LocalizedError {
10
10
case imageRequired
11
11
case maskRequired
12
12
case singleImageAllowed
13
13
case imageProcessingFailure( String )
14
14
case processing( String )
15
+
16
+ public var errorDescription : String ? {
17
+ switch self {
18
+ case . imageRequired:
19
+ return String ( localized: " An image is required for this operation. " )
20
+ case . maskRequired:
21
+ return String ( localized: " An image mask is required for this operation. " )
22
+ case . singleImageAllowed:
23
+ return String ( localized: " Only a single image is allowed for this operation. " )
24
+ case . imageProcessingFailure( let details) :
25
+ return String ( localized: " Failed to process the image: \( details) " )
26
+ case . processing( let details) :
27
+ return String ( localized: " Processing error: \( details) " )
28
+ }
29
+ }
15
30
}
16
31
17
32
public struct BaseProcessorConfiguration : Codable , Sendable {
Original file line number Diff line number Diff line change @@ -7,9 +7,18 @@ import ImageIO
7
7
import MLX
8
8
import UniformTypeIdentifiers
9
9
10
- enum ImageError : Error {
10
+ enum ImageError : LocalizedError {
11
11
case failedToSave
12
12
case unableToOpen
13
+
14
+ var errorDescription : String ? {
15
+ switch self {
16
+ case . failedToSave:
17
+ return String ( localized: " Failed to save the image to the specified location. " )
18
+ case . unableToOpen:
19
+ return String ( localized: " Unable to open the image file. " )
20
+ }
21
+ }
13
22
}
14
23
15
24
/// Conversion utilities for moving between `MLXArray`, `CGImage` and files.
Original file line number Diff line number Diff line change @@ -100,12 +100,26 @@ public protocol ImageToImageGenerator: ImageGenerator {
100
100
-> DenoiseIterator
101
101
}
102
102
103
- enum ModelContainerError : Error {
104
- /// unable to create the particular type of model, e.g. it doesn't support image to image
103
+ enum ModelContainerError : LocalizedError {
104
+ /// Unable to create the particular type of model, e.g. it doesn't support image to image
105
105
case unableToCreate( String , String )
106
-
107
- /// when operating in conserveMemory mode it tried to use a model that had been discarded
106
+ /// When operating in conserveMemory mode, it tried to use a model that had been discarded
108
107
case modelDiscarded
108
+
109
+ var errorDescription : String ? {
110
+ switch self {
111
+ case . unableToCreate( let modelId, let generatorType) :
112
+ return String (
113
+ localized:
114
+ " Unable to create a \( generatorType) with model ID ' \( modelId) '. The model may not support this operation type. "
115
+ )
116
+ case . modelDiscarded:
117
+ return String (
118
+ localized:
119
+ " The model has been discarded to conserve memory and is no longer available. Please recreate the model container. "
120
+ )
121
+ }
122
+ }
109
123
}
110
124
111
125
/// Container for models that guarantees single threaded access.
You can’t perform that action at this time.
0 commit comments