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 documentation to imagen #6616

Open
wants to merge 27 commits into
base: davidmotson.imagen_support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2c18396
Add documentation to imagen
Jan 6, 2025
3bfde82
formatting
Jan 7, 2025
269cda9
Merge branch 'davidmotson.imagen_support' into davidmotson.imagen_docs
Jan 21, 2025
316e187
format
Jan 21, 2025
fcd6bb6
minor doc fix in ImagenImageFormat
Jan 21, 2025
83a29c1
Merge branch 'davidmotson.imagen_support' into davidmotson.imagen_docs
Jan 28, 2025
67e1dfe
format
Jan 28, 2025
4d40615
format fixes
Jan 28, 2025
c3a3115
add copyright to appcheckheaderprovider
Jan 28, 2025
42326fe
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
8350453
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
800da6e
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
ef1f5f3
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
7830a1b
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
4416e1f
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
2e76e1e
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
9488bf8
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
9312a81
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
0c917b5
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
e20d2ed
Merge branch 'davidmotson.imagen_support' into davidmotson.imagen_docs
Jan 29, 2025
2dcce15
extra fixes for comments
Jan 29, 2025
7859a7f
add documentation to ImagenGenerationConfig.Builder
Jan 29, 2025
21d7082
update description of gcsURI
Jan 29, 2025
0c82fff
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
fe6ca34
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
b7ef0e1
Update firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai…
davidmotson Jan 29, 2025
51af553
update description for Imagen-generated images
Jan 29, 2025
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 @@ -81,6 +81,15 @@ internal constructor(
)
}

/**
* Instantiates a new [ImagenModel] given the provided parameters.
*
* @param modelName The name of the model to use, for example `"imagen-3.0-generate-001"`.
* @param generationConfig The configuration parameters to use for image generation.
* @param safetySettings The safety bounds the model will abide to during image generation.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* @param requestOptions Configuration options for sending requests to the backend.
* @return The initialized [ImagenModel] instance.
*/
@JvmOverloads
public fun imagenModel(
modelName: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.
*/

package com.google.firebase.vertexai

import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
Expand All @@ -20,13 +36,18 @@ import com.google.firebase.vertexai.type.ImagenInlineImage
import com.google.firebase.vertexai.type.ImagenSafetySettings
import com.google.firebase.vertexai.type.RequestOptions

/**
* Represents an image model (like Imagen), capable of generating images based on various input
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* types.
*/
public class ImagenModel
internal constructor(
private val modelName: String,
private val generationConfig: ImagenGenerationConfig? = null,
private val safetySettings: ImagenSafetySettings? = null,
private val controller: APIController,
) {
@JvmOverloads
internal constructor(
modelName: String,
apiKey: String,
Expand All @@ -48,8 +69,14 @@ internal constructor(
),
)

/**
* Generates an image, returning the result directly to the caller.
*
* @param prompt The input(s) given to the model as a prompt.
*/
public suspend fun generateImages(prompt: String): ImagenGenerationResponse<ImagenInlineImage> =
try {
controller.generateImage(constructRequest(prompt, null, generationConfig)).toPublicInline()
controller
.generateImage(constructRequest(prompt, null, generationConfig))
.validate()
Expand All @@ -58,6 +85,12 @@ internal constructor(
throw FirebaseVertexAIException.from(e)
}

/**
* Generates an image, storing the result in Google Cloud Storage and returning a URL
*
* @param prompt The input(s) given to the model as a prompt.
* @param gcsUri Specifies the GCS bucket in which to store the image.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public suspend fun generateImages(
prompt: String,
gcsUri: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.
*/

package com.google.firebase.vertexai.internal

import com.google.firebase.vertexai.common.Request
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.
*/

package com.google.firebase.vertexai.internal

import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.
*/

package com.google.firebase.vertexai.internal.util

import android.util.Log
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/** Represents the aspect ratio that the generated image should conform to. */
public class ImagenAspectRatio private constructor(internal val internalVal: String) {
public companion object {
/** A square image, useful for icons, profile pictures, etc. */
@JvmField public val SQUARE_1x1: ImagenAspectRatio = ImagenAspectRatio("1:1")
/** A portrait image in 3:4, the aspect ratio of older TVs. */
@JvmField public val PORTRAIT_3x4: ImagenAspectRatio = ImagenAspectRatio("3:4")
/** A landscape image in 4:3, the aspect ratio of older TVs. */
@JvmField public val LANDSCAPE_4x3: ImagenAspectRatio = ImagenAspectRatio("4:3")
/** A portrait image in 9:16, the aspect ratio of modern monitors and phone screens. */
@JvmField public val PORTRAIT_9x16: ImagenAspectRatio = ImagenAspectRatio("9:16")
/** A landscape image in 16:9, the aspect ratio of modern monitors and phone screens. */
@JvmField public val LANDSCAPE_16x9: ImagenAspectRatio = ImagenAspectRatio("16:9")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/**
* Represents an Imagen Image that is contained in Google Cloud Storage.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*
* @param gcsUri Contains the gs:// uri for the image.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* @param mimeType Contains the mime type of the image eg. "image/png"
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public class ImagenGCSImage
internal constructor(public val gcsUri: String, public val mimeType: String) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/**
* Contains extra settings to configure image generation.
*
* @param negativePrompt This string contains things that should be explicitly be excluded from
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* generated images.
* @param numberOfImages How many images should be generated.
* @param aspectRatio The aspect ratio of the generated images.
* @param imageFormat The file format/compression of the generated images.
* @param addWatermark Adds an invisible watermark to mark the image as AI generated.
*/
public class ImagenGenerationConfig(
public val negativePrompt: String? = null,
public val numberOfImages: Int? = 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/**
* Represents a response from Imagen call to [ImageModel#generateImages]
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*
* @param images contains the generated images
* @param filteredReason if fewer images were generated than were requested, this field will contain
* the reason they were filtered out.
*/
public class ImagenGenerationResponse<T>
internal constructor(public val images: List<T>, public val filteredReason: String?) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/**
* Represents an abstract Imagen Image that is either contained inline or in Google Cloud Storage
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*
* @param data Contains the raw bytes of the image, mutually exclusive with [gcsUri]
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* @param gcsUri Contains the gs:// uri for the image , mutually exclusive with [data]
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* @param mimeType Contains the mime type of the image eg. "image/png"
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public class ImagenImage(
internal val data: ByteArray?,
internal val gcsUri: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

/**
* Represents the format an image should be returned in.
* @param mimeType A string (like "image/jpeg") specifying the encoding mimetype of the image.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
* @param compressionQuality an int (1-100) representing how the quality of the image, a lower
* number meaning the image is permitted to be lower quality to reduce size. This parameter is not
* relevant for every mimetype
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public class ImagenImageFormat
private constructor(public val mimeType: String, public val compressionQuality: Int?) {

public companion object {
/**
* An [ImagenImageFormat] representing a JPEG image.
*
* @param compressionQuality an int (1-100) representing how the quality of the image, a lower
* number meaning the image is permitted to be lower quality to reduce size.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public fun jpeg(compressionQuality: Int? = null): ImagenImageFormat {
return ImagenImageFormat("image/jpeg", compressionQuality)
}

/** An [ImagenImageFormat] representing a PNG image */
public fun png(): ImagenImageFormat {
return ImagenImageFormat("image/png", null)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
/*
* 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.
*/

package com.google.firebase.vertexai.type

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64

/**
* Represents an Imagen Image that is contained inline
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*
* @param data Contains the raw bytes of the image
* @param mimeType Contains the mime type of the image eg. "image/png"
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public class ImagenInlineImage
internal constructor(public val data: ByteArray, public val mimeType: String) {

/**
* Returns the image as an android OS native [Bitmap] so that it can be saved or sent to the UI.
davidmotson marked this conversation as resolved.
Show resolved Hide resolved
*/
public fun asBitmap(): Bitmap {
val data = Base64.decode(data, Base64.NO_WRAP)
return BitmapFactory.decodeByteArray(data, 0, data.size)
Expand Down

This file was deleted.

Loading
Loading