-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Vertex AI] Make
ImageGenerationResponse
generic and add image types (
- Loading branch information
1 parent
e773941
commit 9d0a8d8
Showing
9 changed files
with
329 additions
and
225 deletions.
There are no files selected for viewing
62 changes: 0 additions & 62 deletions
62
FirebaseVertexAI/Sources/Types/Internal/Imagen/DecodableImagenImage.swift
This file was deleted.
Oops, something went wrong.
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
53 changes: 53 additions & 0 deletions
53
FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenFileDataImage.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,53 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
public struct ImagenFileDataImage { | ||
public let mimeType: String | ||
public let gcsURI: String | ||
|
||
init(mimeType: String, gcsURI: String) { | ||
self.mimeType = mimeType | ||
self.gcsURI = gcsURI | ||
} | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenFileDataImage: ImagenImageRepresentable { | ||
public var imagenImage: any ImagenImage { | ||
InternalImagenImage(mimeType: mimeType, bytesBase64Encoded: nil, gcsURI: gcsURI) | ||
} | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenFileDataImage: Equatable {} | ||
|
||
// MARK: - Codable Conformances | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenFileDataImage: Decodable { | ||
enum CodingKeys: String, CodingKey { | ||
case mimeType | ||
case gcsURI = "gcsUri" | ||
} | ||
|
||
public init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let mimeType = try container.decode(String.self, forKey: .mimeType) | ||
let gcsURI = try container.decode(String.self, forKey: .gcsURI) | ||
self.init(mimeType: mimeType, gcsURI: gcsURI) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenInlineDataImage.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,61 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
public struct ImagenInlineDataImage { | ||
public let mimeType: String | ||
public let data: Data | ||
|
||
init(mimeType: String, bytesBase64Encoded: String) { | ||
self.mimeType = mimeType | ||
guard let data = Data(base64Encoded: bytesBase64Encoded) else { | ||
// TODO(#14221): Add error handling for invalid base64 bytes. | ||
fatalError("Creating a `Data` from `bytesBase64Encoded` failed.") | ||
} | ||
self.data = data | ||
} | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenInlineDataImage: ImagenImageRepresentable { | ||
public var imagenImage: any ImagenImage { | ||
InternalImagenImage( | ||
mimeType: mimeType, | ||
bytesBase64Encoded: data.base64EncodedString(), | ||
gcsURI: nil | ||
) | ||
} | ||
} | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenInlineDataImage: Equatable {} | ||
|
||
// MARK: - Codable Conformances | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImagenInlineDataImage: Decodable { | ||
enum CodingKeys: CodingKey { | ||
case mimeType | ||
case bytesBase64Encoded | ||
} | ||
|
||
public init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let mimeType = try container.decode(String.self, forKey: .mimeType) | ||
let bytesBase64Encoded = try container.decode(String.self, forKey: .bytesBase64Encoded) | ||
self.init(mimeType: mimeType, bytesBase64Encoded: bytesBase64Encoded) | ||
} | ||
} |
Oops, something went wrong.