Skip to content

Commit 0007a82

Browse files
author
David Motsonashvili
committed
Added java implementation for imagen
1 parent f826190 commit 0007a82

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.vertexai.java
18+
19+
import androidx.concurrent.futures.SuspendToFutureAdapter
20+
import com.google.common.util.concurrent.ListenableFuture
21+
import com.google.firebase.vertexai.ImageModel
22+
import com.google.firebase.vertexai.type.ImagenGCSImage
23+
import com.google.firebase.vertexai.type.ImagenGenerationConfig
24+
import com.google.firebase.vertexai.type.ImagenGenerationResponse
25+
import com.google.firebase.vertexai.type.ImagenInlineImage
26+
27+
/**
28+
* Wrapper class providing Java compatible methods for [ImageModel].
29+
*
30+
* @see [ImageModel]
31+
*/
32+
public abstract class ImageModelFutures internal constructor() {
33+
/**
34+
* Generates an image, returning the result directly to the caller.
35+
*
36+
* @param prompt The main text prompt from which the image is generated.
37+
* @param config contains secondary image generation parameters.
38+
*/
39+
public abstract fun generateImage(
40+
prompt: String,
41+
config: ImagenGenerationConfig?,
42+
): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>>
43+
44+
/**
45+
* Generates an image, storing the result in Google Cloud Storage and returning a URL
46+
*
47+
* @param prompt The main text prompt from which the image is generated.
48+
* @param gcsUri Specifies the GCS bucket in which to store the image.
49+
* @param config contains secondary image generation parameters.
50+
*/
51+
public abstract fun generateImage(
52+
prompt: String,
53+
gcsUri: String,
54+
config: ImagenGenerationConfig?,
55+
): ListenableFuture<ImagenGenerationResponse<ImagenGCSImage>>
56+
57+
/** Returns the [ImageModel] object wrapped by this object. */
58+
public abstract fun getImageModel(): ImageModel
59+
60+
private class FuturesImpl(private val model: ImageModel) : ImageModelFutures() {
61+
override fun generateImage(
62+
prompt: String,
63+
config: ImagenGenerationConfig?,
64+
): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>> =
65+
SuspendToFutureAdapter.launchFuture { model.generateImage(prompt, config) }
66+
67+
override fun generateImage(
68+
prompt: String,
69+
gcsUri: String,
70+
config: ImagenGenerationConfig?,
71+
): ListenableFuture<ImagenGenerationResponse<ImagenGCSImage>> =
72+
SuspendToFutureAdapter.launchFuture { model.generateImage(prompt, gcsUri, config) }
73+
74+
override fun getImageModel(): ImageModel = model
75+
}
76+
77+
public companion object {
78+
79+
/** @return a [ImageModelFutures] created around the provided [ImageModel] */
80+
@JvmStatic public fun from(model: ImageModel): ImageModelFutures = FuturesImpl(model)
81+
}
82+
}

0 commit comments

Comments
 (0)