Skip to content

Commit 269cda9

Browse files
author
David Motsonashvili
committed
Merge branch 'davidmotson.imagen_support' into davidmotson.imagen_docs
2 parents 3bfde82 + 26b2516 commit 269cda9

File tree

14 files changed

+41
-101
lines changed

14 files changed

+41
-101
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/FirebaseVertexAI.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import com.google.firebase.auth.internal.InternalAuthProvider
2424
import com.google.firebase.inject.Provider
2525
import com.google.firebase.vertexai.type.Content
2626
import com.google.firebase.vertexai.type.GenerationConfig
27-
import com.google.firebase.vertexai.type.ImageSafetySettings
28-
import com.google.firebase.vertexai.type.ImagenModelConfig
27+
import com.google.firebase.vertexai.type.ImagenGenerationConfig
28+
import com.google.firebase.vertexai.type.ImagenSafetySettings
2929
import com.google.firebase.vertexai.type.InvalidLocationException
3030
import com.google.firebase.vertexai.type.RequestOptions
3131
import com.google.firebase.vertexai.type.SafetySetting
@@ -93,8 +93,8 @@ internal constructor(
9393
@JvmOverloads
9494
public fun imageModel(
9595
modelName: String,
96-
generationConfig: ImagenModelConfig? = null,
97-
safetySettings: ImageSafetySettings? = null,
96+
generationConfig: ImagenGenerationConfig? = null,
97+
safetySettings: ImagenSafetySettings? = null,
9898
requestOptions: RequestOptions = RequestOptions(),
9999
): ImageModel {
100100
if (location.trim().isEmpty() || location.contains("/")) {

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/ImageModel.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ import com.google.firebase.vertexai.internal.util.toInternal
2828
import com.google.firebase.vertexai.internal.util.toPublicGCS
2929
import com.google.firebase.vertexai.internal.util.toPublicInline
3030
import com.google.firebase.vertexai.type.FirebaseVertexAIException
31-
import com.google.firebase.vertexai.type.ImageSafetySettings
31+
import com.google.firebase.vertexai.type.ImagenSafetySettings
3232
import com.google.firebase.vertexai.type.ImagenGCSImage
3333
import com.google.firebase.vertexai.type.ImagenGenerationConfig
3434
import com.google.firebase.vertexai.type.ImagenGenerationResponse
35-
import com.google.firebase.vertexai.type.ImagenImageRepresentible
3635
import com.google.firebase.vertexai.type.ImagenInlineImage
37-
import com.google.firebase.vertexai.type.ImagenModelConfig
3836
import com.google.firebase.vertexai.type.PromptBlockedException
3937
import com.google.firebase.vertexai.type.RequestOptions
4038
import kotlin.time.Duration
@@ -48,16 +46,16 @@ import kotlinx.coroutines.tasks.await
4846
public class ImageModel
4947
internal constructor(
5048
private val modelName: String,
51-
private val generationConfig: ImagenModelConfig? = null,
52-
private val safetySettings: ImageSafetySettings? = null,
49+
private val generationConfig: ImagenGenerationConfig? = null,
50+
private val safetySettings: ImagenSafetySettings? = null,
5351
private val controller: APIController,
5452
) {
5553
@JvmOverloads
5654
internal constructor(
5755
modelName: String,
5856
apiKey: String,
59-
generationConfig: ImagenModelConfig? = null,
60-
safetySettings: ImageSafetySettings? = null,
57+
generationConfig: ImagenGenerationConfig? = null,
58+
safetySettings: ImagenSafetySettings? = null,
6159
requestOptions: RequestOptions = RequestOptions(),
6260
appCheckTokenProvider: InteropAppCheckTokenProvider? = null,
6361
internalAuthProvider: InternalAuthProvider? = null,
@@ -115,10 +113,9 @@ internal constructor(
115113
*/
116114
public suspend fun generateImage(
117115
prompt: String,
118-
config: ImagenGenerationConfig?,
119116
): ImagenGenerationResponse<ImagenInlineImage> =
120117
try {
121-
controller.generateImage(constructRequest(prompt, null, config)).toPublicInline().validate()
118+
controller.generateImage(constructRequest(prompt, null, generationConfig)).toPublicInline().validate()
122119
} catch (e: Throwable) {
123120
throw FirebaseVertexAIException.from(e)
124121
}
@@ -132,10 +129,9 @@ internal constructor(
132129
public suspend fun generateImage(
133130
prompt: String,
134131
gcsUri: String,
135-
config: ImagenGenerationConfig?,
136132
): ImagenGenerationResponse<ImagenGCSImage> =
137133
try {
138-
controller.generateImage(constructRequest(prompt, gcsUri, config)).toPublicGCS().validate()
134+
controller.generateImage(constructRequest(prompt, gcsUri, generationConfig)).toPublicGCS().validate()
139135
} catch (e: Throwable) {
140136
throw FirebaseVertexAIException.from(e)
141137
}
@@ -168,7 +164,7 @@ internal constructor(
168164
}
169165
}
170166

171-
private fun <T : ImagenImageRepresentible> ImagenGenerationResponse<T>.validate():
167+
private fun <T> ImagenGenerationResponse<T>.validate():
172168
ImagenGenerationResponse<T> {
173169
if (images.isEmpty()) {
174170
throw PromptBlockedException(message = filteredReason ?: ImageModel.DEFAULT_FILTERED_ERROR)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/GenerateImageResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal data class GenerateImageResponse(val predictions: List<ImagenImageRespo
2323

2424
@Serializable
2525
internal data class ImagenImageResponse(
26-
val bytesBase64Encoded: String?,
27-
val gcsUri: String?,
26+
val bytesBase64Encoded: String? = null,
27+
val gcsUri: String? = null,
2828
val mimeType: String,
2929
)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ internal fun com.google.firebase.vertexai.type.Schema.toInternal(): Schema =
205205
items?.toInternal(),
206206
)
207207

208-
internal fun com.google.firebase.vertexai.type.ImageFormat.toInternal(): ImageOutputOptions =
208+
internal fun com.google.firebase.vertexai.type.ImagenImageFormat.toInternal(): ImageOutputOptions =
209209
ImageOutputOptions(mimeType, compressionQuality)
210210

211211
internal fun JSONObject.toInternal() = Json.decodeFromString<JsonObject>(toString())

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenGCSImage.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,4 @@ package com.google.firebase.vertexai.type
2222
* @param gcsUri Contains the gs:// uri for the image.
2323
* @param mimeType Contains the mime type of the image eg. "image/png"
2424
*/
25-
public class ImagenGCSImage(public val gcsUri: String, public val mimeType: String) :
26-
ImagenImageRepresentible {
27-
28-
override fun asImagenImage(): ImagenImage {
29-
return ImagenImage(null, gcsUri, mimeType)
30-
}
31-
}
25+
public class ImagenGCSImage(public val gcsUri: String, public val mimeType: String) {}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenGenerationConfig.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ package com.google.firebase.vertexai.type
2222
* generated images.
2323
* @param numberOfImages How many images should be generated.
2424
* @param aspectRatio The aspect ratio of the generated images.
25+
* @param imageFormat The file format/compression of the generated images.
26+
* @param addWatermark Adds an invisible watermark to mark the image as AI generated.
2527
*/
2628
public class ImagenGenerationConfig(
27-
public val negativePrompt: String? = null,
28-
public val numberOfImages: Int = 1,
29-
public val aspectRatio: ImagenAspectRatio? = null,
29+
public val negativePrompt: String? = null,
30+
public val numberOfImages: Int? = 1,
31+
public val aspectRatio: ImagenAspectRatio? = null,
32+
public val imageFormat: ImagenImageFormat? = null,
33+
public val addWatermark: Boolean? = null
3034
) {}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenGenerationResponse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package com.google.firebase.vertexai.type
2323
* @param filteredReason if fewer images were generated than were requested, this field will contain
2424
* the reason they were filtered out.
2525
*/
26-
public class ImagenGenerationResponse<T : ImagenImageRepresentible>(
26+
public class ImagenGenerationResponse<T>(
2727
public val images: List<T>,
2828
public val filteredReason: String?,
2929
) {}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenImage.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ package com.google.firebase.vertexai.type
2424
* @param mimeType Contains the mime type of the image eg. "image/png"
2525
*/
2626
public class ImagenImage(
27-
public val data: ByteArray?,
28-
public val gcsUri: String?,
29-
public val mimeType: String,
30-
) : ImagenImageRepresentible {
31-
32-
override fun asImagenImage(): ImagenImage {
33-
return this
34-
}
35-
}
27+
internal val data: ByteArray?,
28+
internal val gcsUri: String?,
29+
internal val mimeType: String,
30+
) {}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImageFormat.kt renamed to firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenImageFormat.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ package com.google.firebase.vertexai.type
2323
* number meaning the image is permitted to be lower quality to reduce size. This parameter is not
2424
* relevant for every mimetype
2525
*/
26-
public class ImageFormat
26+
public class ImagenImageFormat
2727
private constructor(public val mimeType: String, public val compressionQuality: Int?) {
2828

2929
public companion object {
3030
/**
31-
* An [ImageFormat] representing a JPEG image.
31+
* An [ImagenImageFormat] representing a JPEG image.
3232
* @param compressionQuality an int (1-100) representing how the quality of the image, a lower
3333
* number meaning the image is permitted to be lower quality to reduce size.
3434
*/
35-
public fun jpeg(compressionQuality: Int?): ImageFormat {
36-
return ImageFormat("image/jpeg", compressionQuality)
35+
public fun jpeg(compressionQuality: Int? = null): ImagenImageFormat {
36+
return ImagenImageFormat("image/jpeg", compressionQuality)
3737
}
3838

39-
/** An [ImageFormat] representing a PNG image */
40-
public fun png(): ImageFormat {
41-
return ImageFormat("image/png", null)
39+
/** An [ImagenFormat] representing a PNG image */
40+
public fun png(): ImagenImageFormat {
41+
return ImagenImageFormat("image/png", null)
4242
}
4343
}
4444
}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenImageRepresentible.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)