|
| 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.ImagenModel |
| 22 | +import com.google.firebase.vertexai.type.ImagenGCSImage |
| 23 | +import com.google.firebase.vertexai.type.ImagenGenerationResponse |
| 24 | +import com.google.firebase.vertexai.type.ImagenInlineImage |
| 25 | + |
| 26 | +/** |
| 27 | + * Wrapper class providing Java compatible methods for [ImagenModel]. |
| 28 | + * |
| 29 | + * @see [ImagenModel] |
| 30 | + */ |
| 31 | +public abstract class ImagenModelFutures internal constructor() { |
| 32 | + /** |
| 33 | + * Generates an image, returning the result directly to the caller. |
| 34 | + * |
| 35 | + * @param prompt The main text prompt from which the image is generated. |
| 36 | + */ |
| 37 | + public abstract fun generateImages( |
| 38 | + prompt: String, |
| 39 | + ): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>> |
| 40 | + |
| 41 | + /** |
| 42 | + * Generates an image, storing the result in Google Cloud Storage and returning a URL |
| 43 | + * |
| 44 | + * @param prompt The main text prompt from which the image is generated. |
| 45 | + * @param gcsUri Specifies the GCS bucket in which to store the image. |
| 46 | + */ |
| 47 | + public abstract fun generateImages( |
| 48 | + prompt: String, |
| 49 | + gcsUri: String, |
| 50 | + ): ListenableFuture<ImagenGenerationResponse<ImagenGCSImage>> |
| 51 | + |
| 52 | + /** Returns the [ImagenModel] object wrapped by this object. */ |
| 53 | + public abstract fun getImageModel(): ImagenModel |
| 54 | + |
| 55 | + private class FuturesImpl(private val model: ImagenModel) : ImagenModelFutures() { |
| 56 | + override fun generateImages( |
| 57 | + prompt: String, |
| 58 | + ): ListenableFuture<ImagenGenerationResponse<ImagenInlineImage>> = |
| 59 | + SuspendToFutureAdapter.launchFuture { model.generateImages(prompt) } |
| 60 | + |
| 61 | + override fun generateImages( |
| 62 | + prompt: String, |
| 63 | + gcsUri: String, |
| 64 | + ): ListenableFuture<ImagenGenerationResponse<ImagenGCSImage>> = |
| 65 | + SuspendToFutureAdapter.launchFuture { model.generateImages(prompt, gcsUri) } |
| 66 | + |
| 67 | + override fun getImageModel(): ImagenModel = model |
| 68 | + } |
| 69 | + |
| 70 | + public companion object { |
| 71 | + |
| 72 | + /** @return a [ImagenModelFutures] created around the provided [ImagenModel] */ |
| 73 | + @JvmStatic public fun from(model: ImagenModel): ImagenModelFutures = FuturesImpl(model) |
| 74 | + } |
| 75 | +} |
0 commit comments