Add image generation to your apps with Imagen 3

Available in Vertex AI in Firebase (Public Preview)

Vertex AI in Firebase allows developers to integrate generative AI into their applications by providing a streamlined, serverless, and secure way to access the Gemini AI models. Used by thousands of apps today, it handles the complexities of AI infrastructure, simplifies development, letting you focus on building great user experiences.

Now, we’re taking that further adding support for the Imagen models (Imagen 3 and Imagen 3 Fast), in addition to the Gemini family of models. This lets you add image generation directly to your Android, iOS, Flutter, and Web applications, bringing the same ease of use and scalability you expect.

What can you do with Imagen 3 in Firebase

The Vertex AI in Firebase SDKs allow to choose between speed and quality giving you access to both Imagen 3 models:

  • Imagen 3: A high-quality image generation model, capable of producing detailed images.
  • Imagen 3 Fast: A low-latency version of Imagen 3, optimized for speed. Choose this when responsiveness is paramount.

Here’s what’s possible, directly within your client-side code:

  • Generate images from text: String prompt = "A futuristic cityscape at sunset"; - that’s all it takes.
  • Various formats: create images in a range of resolutions, allowing you to optimize for storage and display.
  • Text within images: generate images that incorporate text, opening up possibilities for dynamic content creation within your app.
  • Watermarking: add watermarks for image attribution and provenance.
  • Safety settings: adjust safety filters to help control the content generated.
  • Up to 4 images: generate a maximum of four images per request to manage device resources.

Take a look at these photos generated with Imagen 3:

Getting started

To integrate Imagen 3 into your Firebase project, you should enable Vertex AI in Firebase, add the Firebase SDK to your project, and initialize the service. Once set up, generate an image by initializing your chosen Imagen model and calling generateImages() with your text prompt.

Generating an image
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Initialize with an Imagen 3 model that supports your use case
// like "imagen-3.0-generate-002" or "imagen-3.0-fast-generate-001"
let model = vertex.imagenModel(modelName: "imagen-3.0-generate-002")

// Provide an image generation prompt – this could come from user input
let prompt = "A cityscape at sunset"

// To generate an image, call generateImages with the text prompt
let response = try await model.generateImages(prompt: prompt)

// Handle the generated image
guard let image = response.images.first else {
fatalError("No image in the response.")
}
let uiImage = UIImage(data: image.data)
Copied!
Generating multiple images
// Specify the number of images
final model = FirebaseVertexAI.instance.imagenModel(
model: 'imagen-3.0-generate-002',
  generationConfig: ImagenGenerationConfig(numberOfImages: 4),
);
const prompt = 'An astronaut riding a horse.';

final response = await model.generateImages(prompt);

final images = response.images;
for(var image in images) {
// Process the image
}
Copied!

If you need more control over the image generation, you can configure the model and even set safety settings, for example:

Configuring image generation
const generationConfig = {
aspectRatio: ImagenAspectRatio.LANDSCAPE_16x9,
imageFormat: ImagenImageFormat.jpeg(100),
addWatermark: false
};

const imagenModel = getImagenModel(vertexAI, {
model: "imagen-3.0-generate-002",
generationConfig,
safetySettings: {
  safetyFilterLevel: ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
  personFilterLevel: ImagenPersonFilterLevel.ALLOW_ADULT,
}
});

const response = await imagenModel.generateImages("An astronaut riding a horse")

// If fewer images were generated than were requested,
// then filteredReason will describe the reason they were filtered out
if (response.filteredReason) {
console.log(response.filteredReason);
}
Copied!

Current limitations

While this release brings significant power to your client-side code, some advanced Imagen 3 features are not yet supported:

  • Image editing: features like upscaling are not available.
  • Including images as input: not supported on the client SDK.
  • Generating using a predefined style: not available.

What’s next?

This public preview is the first step. We’re actively working to expand Vertex AI in Firebase for image generation with more capabilities. Start building with Imagen 3 and Vertex AI in Firebase today! Check out the Vertex AI in Firebase documentation and the Firebase quickstart repositories on GitHub for examples of how you can use it.

We’re excited to see the innovative and creative applications you develop!