Skip to content

Commit 67c37ed

Browse files
committed
Add ImagenImage and ImagenImageRepresentable
1 parent 5fabd1d commit 67c37ed

File tree

9 files changed

+344
-204
lines changed

9 files changed

+344
-204
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
16+
protocol DecodableImagenImage: ImagenImage, Decodable {
17+
init(mimeType: String, bytesBase64Encoded: String?, gcsURI: String?)
18+
}
19+
20+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
21+
enum ImagenImageCodingKeys: String, CodingKey {
22+
case mimeType
23+
case bytesBase64Encoded
24+
case gcsURI = "gcsUri"
25+
}
26+
27+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
28+
extension DecodableImagenImage {
29+
init(from decoder: any Decoder) throws {
30+
let container = try decoder.container(keyedBy: ImagenImageCodingKeys.self)
31+
let mimeType = try container.decode(String.self, forKey: .mimeType)
32+
let bytesBase64Encoded = try container.decodeIfPresent(
33+
String.self,
34+
forKey: .bytesBase64Encoded
35+
)
36+
let gcsURI = try container.decodeIfPresent(String.self, forKey: .gcsURI)
37+
guard bytesBase64Encoded != nil || gcsURI != nil else {
38+
throw DecodingError.dataCorrupted(
39+
DecodingError.Context(
40+
codingPath: [ImagenImageCodingKeys.bytesBase64Encoded, ImagenImageCodingKeys.gcsURI],
41+
debugDescription: """
42+
Expected one of \(ImagenImageCodingKeys.bytesBase64Encoded.rawValue) or \
43+
\(ImagenImageCodingKeys.gcsURI.rawValue); both are nil.
44+
"""
45+
)
46+
)
47+
}
48+
guard bytesBase64Encoded == nil || gcsURI == nil else {
49+
throw DecodingError.dataCorrupted(
50+
DecodingError.Context(
51+
codingPath: [ImagenImageCodingKeys.bytesBase64Encoded, ImagenImageCodingKeys.gcsURI],
52+
debugDescription: """
53+
Expected one of \(ImagenImageCodingKeys.bytesBase64Encoded.rawValue) or \
54+
\(ImagenImageCodingKeys.gcsURI.rawValue); both are specified.
55+
"""
56+
)
57+
)
58+
}
59+
60+
self.init(mimeType: mimeType, bytesBase64Encoded: bytesBase64Encoded, gcsURI: gcsURI)
61+
}
62+
}

FirebaseVertexAI/Sources/Types/Internal/Imagen/ImageGenerationResponse.swift

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,12 @@ import Foundation
1616

1717
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1818
struct ImageGenerationResponse {
19-
let images: [Image]
19+
let images: [InternalImagenImage]
2020
let raiFilteredReason: String?
2121
}
2222

23-
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
24-
extension ImageGenerationResponse {
25-
struct Image: Equatable {
26-
let mimeType: String
27-
let bytesBase64Encoded: String?
28-
let gcsURI: String?
29-
}
30-
}
31-
32-
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
33-
extension ImageGenerationResponse {
34-
struct RAIFilteredReason {
35-
let raiFilteredReason: String
36-
}
37-
}
38-
3923
// MARK: - Codable Conformances
4024

41-
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
42-
extension ImageGenerationResponse.Image: Decodable {
43-
enum CodingKeys: String, CodingKey {
44-
case mimeType
45-
case bytesBase64Encoded
46-
case gcsURI = "gcsUri"
47-
}
48-
49-
init(from decoder: any Decoder) throws {
50-
let container = try decoder.container(keyedBy: CodingKeys.self)
51-
mimeType = try container.decode(String.self, forKey: .mimeType)
52-
bytesBase64Encoded = try container.decodeIfPresent(String.self, forKey: .bytesBase64Encoded)
53-
gcsURI = try container.decodeIfPresent(String.self, forKey: .gcsURI)
54-
guard bytesBase64Encoded != nil || gcsURI != nil else {
55-
throw DecodingError.dataCorrupted(
56-
DecodingError.Context(
57-
codingPath: [CodingKeys.bytesBase64Encoded, CodingKeys.gcsURI],
58-
debugDescription: """
59-
Expected one of \(CodingKeys.bytesBase64Encoded.rawValue) or \
60-
\(CodingKeys.gcsURI.rawValue); both are nil.
61-
"""
62-
)
63-
)
64-
}
65-
guard bytesBase64Encoded == nil || gcsURI == nil else {
66-
throw DecodingError.dataCorrupted(
67-
DecodingError.Context(
68-
codingPath: [CodingKeys.bytesBase64Encoded, CodingKeys.gcsURI],
69-
debugDescription: """
70-
Expected one of \(CodingKeys.bytesBase64Encoded.rawValue) or \
71-
\(CodingKeys.gcsURI.rawValue); both are specified.
72-
"""
73-
)
74-
)
75-
}
76-
}
77-
}
78-
79-
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
80-
extension ImageGenerationResponse.RAIFilteredReason: Decodable {
81-
enum CodingKeys: CodingKey {
82-
case raiFilteredReason
83-
}
84-
}
85-
8625
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
8726
extension ImageGenerationResponse: Decodable {
8827
enum CodingKeys: CodingKey {
@@ -99,10 +38,10 @@ extension ImageGenerationResponse: Decodable {
9938
}
10039
var predictionsContainer = try container.nestedUnkeyedContainer(forKey: .predictions)
10140

102-
var images = [Image]()
41+
var images = [InternalImagenImage]()
10342
var raiFilteredReasons = [String]()
10443
while !predictionsContainer.isAtEnd {
105-
if let image = try? predictionsContainer.decode(Image.self) {
44+
if let image = try? predictionsContainer.decode(InternalImagenImage.self) {
10645
images.append(image)
10746
} else if let filterReason = try? predictionsContainer.decode(RAIFilteredReason.self) {
10847
raiFilteredReasons.append(filterReason.raiFilteredReason)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
16+
struct InternalImagenImage {
17+
let mimeType: String
18+
let bytesBase64Encoded: String?
19+
let gcsURI: String?
20+
21+
init(mimeType: String, bytesBase64Encoded: String?, gcsURI: String?) {
22+
self.mimeType = mimeType
23+
self.bytesBase64Encoded = bytesBase64Encoded
24+
self.gcsURI = gcsURI
25+
}
26+
}
27+
28+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
29+
extension InternalImagenImage: DecodableImagenImage {}
30+
31+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
32+
extension InternalImagenImage: Equatable {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
16+
struct RAIFilteredReason {
17+
let raiFilteredReason: String
18+
}
19+
20+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
21+
extension RAIFilteredReason: Decodable {
22+
enum CodingKeys: CodingKey {
23+
case raiFilteredReason
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
18+
public protocol ImagenImage: ImagenImageRepresentable {
19+
var mimeType: String { get }
20+
var bytesBase64Encoded: String? { get }
21+
var gcsURI: String? { get }
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
18+
public protocol ImagenImageRepresentable {
19+
var imagenImage: any ImagenImage { get }
20+
}
21+
22+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
23+
public extension ImagenImage {
24+
var imagenImage: any ImagenImage {
25+
return self
26+
}
27+
}

0 commit comments

Comments
 (0)