-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[Vertex AI] Add Imagen integration tests for GCS and filtering #14403
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
FirebaseVertexAI/Tests/TestApp/Tests/Integration/ImagenIntegrationTests.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,142 @@ | ||
// Copyright 2025 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 FirebaseAuth | ||
import FirebaseCore | ||
import FirebaseStorage | ||
import FirebaseVertexAI | ||
import Testing | ||
import VertexAITestApp | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#endif // canImport(UIKit) | ||
|
||
@Suite( | ||
.enabled( | ||
if: ProcessInfo.processInfo.environment["VTXIntegrationImagen"] != nil, | ||
"Only runs if the environment variable VTXIntegrationImagen is set." | ||
), | ||
.serialized | ||
) | ||
struct ImagenIntegrationTests { | ||
var vertex: VertexAI | ||
var storage: Storage | ||
var userID1: String | ||
|
||
init() async throws { | ||
let authResult = try await Auth.auth().signIn( | ||
withEmail: Credentials.emailAddress1, | ||
password: Credentials.emailPassword1 | ||
) | ||
userID1 = authResult.user.uid | ||
|
||
vertex = VertexAI.vertexAI() | ||
storage = Storage.storage() | ||
} | ||
|
||
@Test func generateImage_inlineImage() async throws { | ||
let generationConfig = ImagenGenerationConfig( | ||
negativePrompt: "snow, frost", | ||
aspectRatio: .portrait3x4, | ||
imageFormat: .png(), | ||
addWatermark: false | ||
) | ||
let model = vertex.imagenModel( | ||
modelName: "imagen-3.0-generate-002", | ||
generationConfig: generationConfig, | ||
safetySettings: ImagenSafetySettings( | ||
safetyFilterLevel: .blockLowAndAbove, | ||
personFilterLevel: .allowAdult | ||
) | ||
) | ||
let imagePrompt = "A woman, 35mm portrait, in front of a mountain range" | ||
|
||
let response = try await model.generateImages(prompt: imagePrompt) | ||
|
||
#expect(response.filteredReason == nil) | ||
#expect(response.images.count == 1) | ||
let image = try #require(response.images.first) | ||
#expect(image.mimeType == "image/png") | ||
#expect(image.data.isEmpty == false) | ||
#if canImport(UIKit) | ||
let uiImage = try #require(UIImage(data: image.data)) | ||
#expect(uiImage.size.width == 896.0) | ||
#expect(uiImage.size.height == 1280.0) | ||
#endif // canImport(UIKit) | ||
} | ||
|
||
@Test func generateImages_gcsImages() async throws { | ||
let generationConfig = ImagenGenerationConfig( | ||
numberOfImages: 3, | ||
aspectRatio: .landscape16x9, | ||
imageFormat: .jpeg(compressionQuality: 60), | ||
addWatermark: true | ||
) | ||
let model = vertex.imagenModel( | ||
modelName: "imagen-3.0-fast-generate-001", | ||
generationConfig: generationConfig, | ||
safetySettings: ImagenSafetySettings( | ||
safetyFilterLevel: .blockMediumAndAbove, | ||
personFilterLevel: .blockAll | ||
) | ||
) | ||
let prompt = "A dense jungle with light streaming through the treetops" | ||
let storageRef = storage.reference( | ||
withPath: "/vertexai/imagen/authenticated/user/\(userID1)" | ||
) | ||
|
||
let response = try await model.generateImages(prompt: prompt, gcsUri: storageRef.gsURI) | ||
|
||
#expect(response.filteredReason == nil) | ||
#expect(response.images.count == generationConfig.numberOfImages) | ||
for image in response.images { | ||
#expect(image.mimeType == "image/jpeg") | ||
let imageRef = storage.reference(forURL: image.gcsURI) | ||
let imageData = try await imageRef.data(maxSize: 1_000_000) // ~1MB | ||
#expect(imageData.isEmpty == false) | ||
#if canImport(UIKit) | ||
let uiImage = try #require(UIImage(data: imageData)) | ||
#expect(uiImage.size.width == 1408.0) | ||
#expect(uiImage.size.height == 768.0) | ||
#endif // canImport(UIKit) | ||
try await imageRef.delete() | ||
} | ||
} | ||
|
||
@Test func generateImage_allImagesFilteredOut() async throws { | ||
let generationConfig = ImagenGenerationConfig(numberOfImages: 2, imageFormat: .jpeg()) | ||
let model = vertex.imagenModel( | ||
modelName: "imagen-3.0-fast-generate-001", | ||
generationConfig: generationConfig, | ||
safetySettings: ImagenSafetySettings( | ||
safetyFilterLevel: .blockLowAndAbove, | ||
personFilterLevel: .blockAll | ||
) | ||
) | ||
let imagePrompt = "A woman, 35mm portrait, in front of a mountain range" | ||
|
||
let response = try await model.generateImages(prompt: imagePrompt) | ||
|
||
#expect(response.images.isEmpty) | ||
let filteredReason = try #require(response.filteredReason) | ||
// 39322892: Detects a person or face when it isn't allowed due to the request safety settings. | ||
#expect(filteredReason.contains("39322892")) | ||
// TODO(#14221): Update implementation and test to throw an exception when all filtered out. | ||
} | ||
|
||
// TODO(#14221): Add an integration test for the prompt being blocked. | ||
|
||
// TODO(#14221): Add integration tests for validating that Storage Rules are enforced. | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh! such an ugly api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed! Just for reference, the full message (for now) is:
I figure it will be most reliable to match on the support code since it's less likely to change than the message wording.
The code
63236870
isn't in the docs so I picked39322892
.