Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd60e15

Browse files
update
1 parent ac4601f commit cd60e15

File tree

6 files changed

+120
-62
lines changed

6 files changed

+120
-62
lines changed

‎Sources/openai-async-image-swiftui/OpenAIAsyncImage.swift

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,32 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
4444
/// Optional custom view builder template
4545
let tpl : ImageProcess?
4646

47+
/// Dall-e model type
48+
let model : DalleModel
49+
4750
// MARK: - Life cycle
4851

52+
/// Initializes a view model for generating images using the OpenAI API with customizable parameters.
4953
/// - Parameters:
50-
/// - prompt: A text description of the desired image(s). The maximum length is 1000 characters
51-
/// - size: The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024
52-
/// - tpl: Custom view builder template
53-
/// - loader: Custom loader conforming to `IOpenAILoader`
54+
/// - prompt: A `Binding` to a `String` that represents a text description of the desired image(s).
55+
/// The maximum length for the prompt is 1000 characters.
56+
/// - size: The size of the generated images, specified as an `OpenAIImageSize`.
57+
/// Defaults to `.dpi256`. Must be one of `.dpi256` (256x256), `.dpi512` (512x512), or `.dpi1024` (1024x1024).
58+
/// - model: The `DalleModel` specifying which model to use for generating the image(s).
59+
/// Defaults to `.dalle2`.
60+
/// - tpl: A custom SwiftUI `ViewBuilder` template for processing or rendering the generated image(s).
61+
/// - loader: A custom loader conforming to the `IOpenAILoader` protocol, responsible for handling
62+
/// the image generation process, such as communicating with the OpenAI API.
5463
public init(
55-
prompt : Binding<String>,
56-
size : OpenAIImageSize = .dpi256,
57-
@ViewBuilder tpl : @escaping ImageProcess,
58-
loader : T
59-
){
64+
prompt: Binding<String>,
65+
size: OpenAIImageSize = .dpi256,
66+
model: DalleModel = .dalle2,
67+
@ViewBuilder tpl: @escaping ImageProcess,
68+
loader: T
69+
) {
6070
self._prompt = prompt
6171
self.size = size
72+
self.model = model
6273
self.tpl = tpl
6374
self.loader = loader
6475
}
@@ -97,32 +108,41 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
97108
return .loading
98109
}
99110

100-
/// Load using the default loader
111+
/// Loads an image using the default loader.
101112
/// - Parameters:
102-
/// - prompt: The text prompt for generating the image
103-
/// - size: The desired size of the image
104-
/// - Returns: OpenAI image
105-
private func loadImageDefault(_ prompt : String, with size : ImageSize) async throws -> Image{
106-
try await defaultLoader.load(prompt, with: size)
113+
/// - prompt: The text prompt describing the desired image content.
114+
/// - size: The dimensions of the generated image, specified as `ImageSize`.
115+
/// - model: The `DalleModel` specifying the AI model to use for image generation.
116+
/// - Returns: A generated `Image` object if successful.
117+
/// - Throws: An error if the image generation fails.
118+
private func loadImageDefault(
119+
_ prompt: String,
120+
with size: ImageSize,
121+
model: DalleModel
122+
) async throws -> Image {
123+
try await defaultLoader.load(prompt, with: size, model: model)
107124
}
108-
109-
/// Load image using the provided or default loader
125+
126+
/// Loads an image using a provided loader, or falls back to the default loader if none is provided.
110127
/// - Parameters:
111-
/// - prompt: The text prompt for generating the image
112-
/// - size: The desired size of the image
113-
/// - Returns: OpenAI image if successful, otherwise nil
114-
private func loadImage(_ prompt : String, with size : ImageSize) async -> Image?{
115-
do{
116-
if let loader = loader{
117-
return try await loader.load(prompt, with: size)
128+
/// - prompt: The text prompt describing the desired image content.
129+
/// - size: The dimensions of the generated image, specified as `ImageSize`.
130+
/// - model: The `DalleModel` specifying the AI model to use for image generation.
131+
/// - Returns: An `Image` object if successful, or `nil` if the operation fails or is cancelled.
132+
private func loadImage(
133+
_ prompt: String,
134+
with size: ImageSize,
135+
model: DalleModel
136+
) async -> Image? {
137+
do {
138+
if let loader = loader {
139+
return try await loader.load(prompt, with: size, model: model)
118140
}
119-
120-
return try await loadImageDefault(prompt, with: size)
121-
}catch{
122-
if !Task.isCancelled{
141+
return try await loadImageDefault(prompt, with: size, model: model)
142+
} catch {
143+
if !Task.isCancelled {
123144
self.error = error
124145
}
125-
126146
return nil
127147
}
128148
}
@@ -151,7 +171,7 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
151171
/// - Returns: A task that fetches the OpenAI image
152172
private func getTask() -> Task<Void, Never>{
153173
Task{
154-
if let image = await loadImage(prompt, with: size){
174+
if let image = await loadImage(prompt, with: size, model: model){
155175
await setImage(image)
156176
}
157177
}
@@ -162,35 +182,42 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
162182

163183
public extension OpenAIAsyncImage where Content == EmptyView, T == OpenAIDefaultLoader{
164184

165-
/// Convenience initializer for default loader without custom view template
185+
/// Convenience initializer for creating an instance with the default loader and no custom view template.
166186
/// - Parameters:
167-
/// - prompt: The text prompt for generating the image
168-
/// - size: The desired size of the image
187+
/// - prompt: A `Binding` to a `String` containing the text prompt that describes the desired image content.
188+
/// - size: The desired size of the generated image, specified as an `OpenAIImageSize`.
189+
/// Defaults to `.dpi256`.
190+
/// - model: The `DalleModel` specifying the AI model to use for image generation. Defaults to `.dalle2`.
169191
init(
170-
prompt : Binding<String>,
171-
size : OpenAIImageSize = .dpi256
172-
){
192+
prompt: Binding<String>,
193+
size: OpenAIImageSize = .dpi256,
194+
model: DalleModel = .dalle2
195+
) {
173196
self._prompt = prompt
174197
self.size = size
198+
self.model = model
175199
self.tpl = nil
176200
self.loader = nil
177201
}
178202
}
179203

180204
public extension OpenAIAsyncImage where T == OpenAIDefaultLoader{
181205

182-
/// Convenience initializer for default loader with custom view template
206+
/// Convenience initializer for creating an instance with the default loader and a custom view template.
183207
/// - Parameters:
184-
/// - prompt: The text prompt for generating the image
185-
/// - size: The desired size of the image
186-
/// - tpl: Custom view template
208+
/// - prompt: A `Binding` to a `String` containing the text prompt that describes the desired image content.
209+
/// - size: The desired size of the generated image, specified as an `OpenAIImageSize`. Defaults to `.dpi256`.
210+
/// - model: The `DalleModel` specifying the AI model to use for image generation. Defaults to `.dalle2`.
211+
/// - tpl: A SwiftUI `@ViewBuilder` closure that provides a custom view template for processing or rendering the generated image.
187212
init(
188-
prompt : Binding<String>,
189-
size : OpenAIImageSize = .dpi256,
190-
@ViewBuilder tpl : @escaping ImageProcess
191-
){
213+
prompt: Binding<String>,
214+
size: OpenAIImageSize = .dpi256,
215+
model: DalleModel = .dalle2,
216+
@ViewBuilder tpl: @escaping ImageProcess
217+
) {
192218
self._prompt = prompt
193219
self.size = size
220+
self.model = model
194221
self.tpl = tpl
195222
self.loader = nil
196223
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DalleModel.swift
3+
// openai-async-image-swiftui
4+
//
5+
// Created by Igor on 26.11.24.
6+
//
7+
8+
public enum DalleModel: String{
9+
10+
case dalle2 = "dall-e-2"
11+
12+
case dalle3 = "dall-e-3"
13+
}

‎Sources/openai-async-image-swiftui/enum/OpenAIImageSize.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ public enum OpenAIImageSize: String, Encodable{
1616
case dpi512 = "512x512"
1717

1818
case dpi1024 = "1024x1024"
19+
20+
case dpi1792x1024 = "1792x1024"
21+
22+
case dpi1024x1792 = "1024x1792"
1923
}

‎Sources/openai-async-image-swiftui/model/Input.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import Foundation
1212
/// Given a prompt and/or an input image, the model will generate a new image
1313
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
1414
struct Input: Encodable{
15+
16+
/// dall-e model
17+
let model : String
1518

1619
/// A text description of the desired image(s). The maximum length is 1000 characters
1720
let prompt: String

‎Sources/openai-async-image-swiftui/protocol/IOpenAILoader.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import SwiftUI
1111
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
1212
public protocol IOpenAILoader {
1313

14-
/// Asynchronously loads an image based on a provided text promptand size
14+
/// Asynchronously generates an image using a given text prompt, size, and model.
1515
/// - Parameters:
16-
/// - prompt: The text prompt describing the desired image
17-
/// - size: The size of the generated image
18-
/// - Returns: The generated OpenAI image
19-
func load(_ prompt: String, with size: OpenAIImageSize) async throws -> Image
16+
/// - prompt: A descriptive text prompt that defines the content of the desired image.
17+
/// - size: The dimensions of the generated image, specified as an `OpenAIImageSize`.
18+
/// - model: The `DalleModel` used for image generation.
19+
/// - Returns: A generated `Image` based on the provided prompt and size.
20+
/// - Throws: An error if the image generation process fails, such as issues with the prompt, model, or network.
21+
func load(_ prompt: String, with size: OpenAIImageSize,
22+
model: DalleModel) async throws -> Image
2023
}

‎Sources/openai-async-image-swiftui/viewModel/OpenAIDefaultLoader.swift

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,45 @@ public final class OpenAIDefaultLoader: IOpenAILoader, Sendable {
3838
client = Http.Proxy(baseURL: url)
3939
}
4040

41-
/// Loads an image from the OpenAI API based on a text prompt
41+
/// Asynchronously loads an image from the OpenAI API using a text prompt and specified parameters.
4242
/// - Parameters:
43-
/// - prompt: The text prompt describing the desired image
44-
/// - size: The size of the generated image
45-
/// - Returns: OpenAI Image
43+
/// - prompt: The text prompt describing the desired image content.
44+
/// - size: The dimensions of the generated image, specified as `OpenAIImageSize`.
45+
/// - model: The `DalleModel` used for generating the image.
46+
/// - Returns: A generated `Image` object based on the prompt and size.
47+
/// - Throws: An `AsyncImageErrors` if the client is undefined, the request fails,
48+
/// or the OpenAI API returns an error.
4649
public func load(
4750
_ prompt: String,
48-
with size: OpenAIImageSize
51+
with size: OpenAIImageSize,
52+
model: DalleModel
4953
) async throws -> Image {
5054

5155
guard let client = client else {
5256
throw AsyncImageErrors.clientIsNotDefined
5357
}
5458

5559
do {
56-
let (path, body, headers) = prepareRequest(prompt: prompt, size: size)
60+
let (path, body, headers) = prepareRequest(prompt: prompt, size: size, model: model)
5761
let result: Http.Response<Output> = try await client.post(path: path, body: body, headers: headers)
5862
return try imageBase64(from: result.value)
5963

6064
} catch {
6165
throw AsyncImageErrors.handleRequest(error)
6266
}
6367
}
64-
65-
/// Prepares the request with the necessary parameters
68+
69+
/// Prepares the API request for generating an image with the given parameters.
6670
/// - Parameters:
67-
/// - prompt: The text prompt describing the desired image
68-
/// - size: The size of the generated image
69-
/// - Returns: A tuple containing the path, body, and headers for the request
70-
private func prepareRequest(prompt: String, size: OpenAIImageSize) -> (String, Input, [String: String]) {
71-
let body = Input(prompt: prompt, size: size, response_format: .b64, n: 1)
71+
/// - prompt: The descriptive text prompt for generating the image.
72+
/// - size: The dimensions of the image to be generated, as `OpenAIImageSize`.
73+
/// - model: The `DalleModel` specifying the AI model to use for generation.
74+
/// - Returns: A tuple containing:
75+
/// - `path`: The API endpoint path as a `String`.
76+
/// - `body`: The request payload as an `Input` object, containing model, prompt, size, and other parameters.
77+
/// - `headers`: A dictionary of HTTP headers required for the request.
78+
private func prepareRequest(prompt: String, size: OpenAIImageSize, model: DalleModel) -> (String, Input, [String: String]) {
79+
let body = Input(model: model.rawValue, prompt: prompt, size: size, response_format: .b64, n: 1)
7280
let headers = ["Content-Type": "application/json", "Authorization": "Bearer \(endpoint.apiKey)"]
7381
let path = endpoint.path
7482
return (path, body, headers)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /