Skip to content

[Vertex AI] Make ImagenImageRepresentable internal #14341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

import Foundation

// TODO(andrewheard): Make this public when the SDK supports Imagen operations that take images as
// input (upscaling / editing).
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public protocol ImagenImageRepresentable {
var _imagenImage: _ImagenImage { get }
protocol ImagenImageRepresentable {
/// Internal representation of the image for use with the Imagen model.
///
/// - Important: Not needed by SDK users.
var _internalImagenImage: _InternalImagenImage { get }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@

import Foundation

/// Internal representation of an image for the Imagen model.
///
/// - Important: For internal use by types conforming to ``ImagenImageRepresentable``; all
/// properties are `internal` and are not needed by SDK users.
///
/// TODO(andrewheard): Make this public when the SDK supports Imagen operations that take images as
/// input (upscaling / editing).
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct _ImagenImage {
struct _InternalImagenImage {
let mimeType: String
let bytesBase64Encoded: String?
let gcsURI: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public struct ImagenFileDataImage {

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension ImagenFileDataImage: ImagenImageRepresentable {
public var _imagenImage: _ImagenImage {
_ImagenImage(mimeType: mimeType, bytesBase64Encoded: nil, gcsURI: gcsURI)
// TODO(andrewheard): Make this public when the SDK supports Imagen operations that take images as
// input (upscaling / editing).
var _internalImagenImage: _InternalImagenImage {
_InternalImagenImage(mimeType: mimeType, bytesBase64Encoded: nil, gcsURI: gcsURI)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Foundation

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
public struct ImagenGenerationResponse<T: ImagenImageRepresentable> {
public struct ImagenGenerationResponse<T> {
public let images: [T]
public let filteredReason: String?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public struct ImagenInlineDataImage {

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension ImagenInlineDataImage: ImagenImageRepresentable {
public var _imagenImage: _ImagenImage {
_ImagenImage(
// TODO(andrewheard): Make this public when the SDK supports Imagen operations that take images as
// input (upscaling / editing).
var _internalImagenImage: _InternalImagenImage {
_InternalImagenImage(
mimeType: mimeType,
bytesBase64Encoded: data.base64EncodedString(),
gcsURI: nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public final class ImagenModel {
)
}

func generateImages<T: Decodable>(prompt: String,
parameters: ImageGenerationParameters) async throws
-> ImagenGenerationResponse<T> {
func generateImages<T>(prompt: String,
parameters: ImageGenerationParameters) async throws
-> ImagenGenerationResponse<T> where T: Decodable, T: ImagenImageRepresentable {
let request = ImagenGenerationRequest<T>(
model: modelResourceName,
options: requestOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ final class ImagenFileDataImageTests: XCTestCase {

XCTAssertEqual(image.mimeType, mimeType)
XCTAssertEqual(image.gcsURI, gcsURI)
XCTAssertEqual(image._imagenImage.mimeType, mimeType)
XCTAssertEqual(image._imagenImage.gcsURI, gcsURI)
XCTAssertNil(image._imagenImage.bytesBase64Encoded)
XCTAssertEqual(image._internalImagenImage.mimeType, mimeType)
XCTAssertEqual(image._internalImagenImage.gcsURI, gcsURI)
XCTAssertNil(image._internalImagenImage.bytesBase64Encoded)
}

func testDecodeImage_missingGCSURI_throws() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ final class ImagenInlineDataImageTests: XCTestCase {

XCTAssertEqual(image.mimeType, mimeType)
XCTAssertEqual(image.data.base64EncodedString(), bytesBase64Encoded)
XCTAssertEqual(image._imagenImage.mimeType, mimeType)
XCTAssertEqual(image._imagenImage.bytesBase64Encoded, bytesBase64Encoded)
XCTAssertNil(image._imagenImage.gcsURI)
XCTAssertEqual(image._internalImagenImage.mimeType, mimeType)
XCTAssertEqual(image._internalImagenImage.bytesBase64Encoded, bytesBase64Encoded)
XCTAssertNil(image._internalImagenImage.gcsURI)
}

func testDecodeImage_missingBytesBase64Encoded_throws() throws {
Expand Down
Loading