Skip to content
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

Add imagen generation example to VertexAI Sample #14545

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
- [feature] The Firebase Vertex AI SDK no longer requires `@preconcurrency` when imported in Swift 6.
- [feature] The Vertex AI SDK no longer requires `@preconcurrency` when imported in Swift 6.
- [feature] The Vertex AI Sample App now includes an image generation example.

# 11.9.0
- [feature] **Public Preview**: Added support for generating images using the
Expand Down
67 changes: 67 additions & 0 deletions FirebaseVertexAI/Sample/ImagenScreen/ImagenScreen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 SwiftUI

struct ImagenScreen: View {
@StateObject var viewModel = ImagenViewModel()

enum FocusedField: Hashable {
case message
}

@FocusState
var focusedField: FocusedField?

var body: some View {
VStack {
TextField("Enter a prompt to generate an image", text: $viewModel.userInput)
.focused($focusedField, equals: .message)
.textFieldStyle(.roundedBorder)
.onSubmit {
onGenerateTapped()
}
.padding()

Button("Generate") {
onGenerateTapped()
}
.padding()
ForEach(viewModel.images, id: \.self) {
Image(uiImage: $0)
.resizable()
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.aspectRatio(nil, contentMode: .fit)
.clipped()
}
}
.navigationTitle("Imagen sample")
.onAppear {
focusedField = .message
}
}

private func onGenerateTapped() {
focusedField = nil

Task {
await viewModel.generateImage(prompt: viewModel.userInput)
}
}
}

#Preview {
ImagenScreen()
}
85 changes: 85 additions & 0 deletions FirebaseVertexAI/Sample/ImagenScreen/ImagenViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 FirebaseVertexAI
import Foundation
import OSLog
import SwiftUI

@MainActor
class ImagenViewModel: ObservableObject {
private var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "generative-ai")

@Published
var userInput: String = ""

@Published
var images = [UIImage]()

@Published
var errorMessage: String?

@Published
var inProgress = false

private let model: ImagenModel

// 1. Initialize the Vertex AI service
private let vertexAI = VertexAI.vertexAI()

init() {
// 2. Configure Imagen settings
let modelName = "imagen-3.0-generate-002"
let safetySettings = ImagenSafetySettings(
safetyFilterLevel: .blockLowAndAbove
)
var generationConfig = ImagenGenerationConfig()
generationConfig.numberOfImages = 4
generationConfig.aspectRatio = .landscape4x3
generationConfig.addWatermark = false
generationConfig.negativePrompt = "people"

// 3. Initialize the Imagen model
model = vertexAI.imagenModel(
modelName: modelName,
generationConfig: generationConfig,
safetySettings: safetySettings
)
}

func generateImage(prompt: String) async {
guard !inProgress else {
print("Already generating images...")
return
}
do {
defer {
inProgress = false
}
inProgress = true
// 4. Call generateImages with the text prompt
let response = try await model.generateImages(prompt: prompt)

// 5. Print the reason images were filtered out, if any.
if let filteredReason = response.filteredReason {
print("Image(s) Blocked: \(filteredReason)")
}

// 6. Convert the image data to UIImage for display in the UI
images = response.images.compactMap { UIImage(data: $0.data) }
} catch {
logger.error("Error generating images: \(error)")
}
}
}
16 changes: 16 additions & 0 deletions FirebaseVertexAI/Sample/VertexAISample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
886F95E02B17D5010036F07A /* ConversationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E10F562B1112F600C08E95 /* ConversationViewModel.swift */; };
886F95E12B17D5010036F07A /* ConversationScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E10F542B1112CA00C08E95 /* ConversationScreen.swift */; };
886F95E32B17D6630036F07A /* GenerativeAIUIComponents in Frameworks */ = {isa = PBXBuildFile; productRef = 886F95E22B17D6630036F07A /* GenerativeAIUIComponents */; };
DEFECAA92D7B4CCD00EF9621 /* ImagenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFECAA72D7B4CCD00EF9621 /* ImagenViewModel.swift */; };
DEFECAAA2D7B4CCD00EF9621 /* ImagenScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFECAA62D7B4CCD00EF9621 /* ImagenScreen.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -60,6 +62,8 @@
88E10F582B11131900C08E95 /* ChatMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessage.swift; sourceTree = "<group>"; };
88E10F5A2B11133E00C08E95 /* MessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageView.swift; sourceTree = "<group>"; };
88E10F5C2B11135000C08E95 /* BouncingDots.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BouncingDots.swift; sourceTree = "<group>"; };
DEFECAA62D7B4CCD00EF9621 /* ImagenScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagenScreen.swift; sourceTree = "<group>"; };
DEFECAA72D7B4CCD00EF9621 /* ImagenViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagenViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -146,6 +150,7 @@
8848C8262B0D04BC007B434F = {
isa = PBXGroup;
children = (
DEFECAA82D7B4CCD00EF9621 /* ImagenScreen */,
88B8A9352B0FCBA700424728 /* GenerativeAIUIComponents */,
869200B22B879C4F00482873 /* GoogleService-Info.plist */,
8848C8312B0D04BC007B434F /* VertexAISample */,
Expand Down Expand Up @@ -279,6 +284,15 @@
path = Screens;
sourceTree = "<group>";
};
DEFECAA82D7B4CCD00EF9621 /* ImagenScreen */ = {
isa = PBXGroup;
children = (
DEFECAA62D7B4CCD00EF9621 /* ImagenScreen.swift */,
DEFECAA72D7B4CCD00EF9621 /* ImagenViewModel.swift */,
);
path = ImagenScreen;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -374,6 +388,8 @@
886F95E02B17D5010036F07A /* ConversationViewModel.swift in Sources */,
886F95DD2B17D5010036F07A /* MessageView.swift in Sources */,
886F95DC2B17BAEF0036F07A /* PhotoReasoningScreen.swift in Sources */,
DEFECAA92D7B4CCD00EF9621 /* ImagenViewModel.swift in Sources */,
DEFECAAA2D7B4CCD00EF9621 /* ImagenScreen.swift in Sources */,
886F95DB2B17BAEF0036F07A /* PhotoReasoningViewModel.swift in Sources */,
886F95E12B17D5010036F07A /* ConversationScreen.swift in Sources */,
88263BF02B239C09008AB09B /* ErrorView.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions FirebaseVertexAI/Sample/VertexAISample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ struct ContentView: View {
} label: {
Label("Function Calling", systemImage: "function")
}
NavigationLink {
ImagenScreen()
} label: {
Label("Imagen", systemImage: "camera.circle")
}
}
.navigationTitle("Generative AI Samples")
}
Expand Down
Loading