-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c40a26e
commit 63118c2
Showing
7 changed files
with
177 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Sources/OpenAIService/Models/Images/Variation/OpenAIImageVariationBody.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// OpenAIImageVariationBody.swift | ||
// OpenAIDemo | ||
// | ||
// Created by Gusakovsky, Sergey on 5.03.23. | ||
// | ||
|
||
import Foundation | ||
#if os(iOS) | ||
import UIKit | ||
#endif | ||
|
||
/// https://platform.openai.com/docs/api-reference/images/create-variation | ||
public struct OpenAIImageVariationBody { | ||
public let image: FormData | ||
public let numberOfImages: Int | ||
public let size: OpenAIGenerationImageSize | ||
public let responseFormat: OpenAIGenerationImageResponseFormat | ||
public let user: String? | ||
|
||
public init( | ||
image: Data, | ||
numberOfImages: Int = 1, | ||
size: OpenAIGenerationImageSize = .large, | ||
responseFormat: OpenAIGenerationImageResponseFormat = .url, | ||
user: String? = nil | ||
) { | ||
self.image = FormData(data: image, mimeType: "image/png", fileName: "image.png") | ||
self.numberOfImages = numberOfImages | ||
self.size = size | ||
self.responseFormat = responseFormat | ||
self.user = user | ||
} | ||
|
||
#if os(iOS) | ||
public init?( | ||
image: UIImage, | ||
numberOfImages: Int = 1, | ||
size: OpenAIGenerationImageSize = .large, | ||
responseFormat: OpenAIGenerationImageResponseFormat = .url, | ||
user: String? = nil | ||
) { | ||
guard let imageData = image.pngData() else { return nil } | ||
self.image = FormData(data: imageData, mimeType: "image/png", fileName: "image.png") | ||
self.numberOfImages = numberOfImages | ||
self.size = size | ||
self.responseFormat = responseFormat | ||
self.user = user | ||
} | ||
#endif | ||
|
||
public var body: [String: Any] { | ||
var result: [String: Any] = [ | ||
"image": self.image, | ||
"n": self.numberOfImages, | ||
"size": self.size.rawValue, | ||
"response_format": self.responseFormat.rawValue | ||
] | ||
if let user = self.user { | ||
result["user"] = user | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters