|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 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 | + * https://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.example.snippets.ai |
| 18 | + |
| 19 | +import android.content.ContentResolver |
| 20 | +import android.graphics.Bitmap |
| 21 | +import android.net.Uri |
| 22 | +import com.google.firebase.Firebase |
| 23 | +import com.google.firebase.ai.ai |
| 24 | +import com.google.firebase.ai.type.GenerativeBackend |
| 25 | +import com.google.firebase.ai.type.ImagePart |
| 26 | +import com.google.firebase.ai.type.ResponseModality |
| 27 | +import com.google.firebase.ai.type.content |
| 28 | +import com.google.firebase.ai.type.generationConfig |
| 29 | +import kotlinx.coroutines.CoroutineScope |
| 30 | +import kotlinx.coroutines.launch |
| 31 | + |
| 32 | +object GeminiDeveloperApi25FlashModelConfiguration { |
| 33 | + // [START android_gemini_developer_api_gemini_25_flash_model] |
| 34 | + // Start by instantiating a GenerativeModel and specifying the model name: |
| 35 | + val model = Firebase.ai(backend = GenerativeBackend.googleAI()) |
| 36 | + .generativeModel("gemini-2.5-flash") |
| 37 | + // [END android_gemini_developer_api_gemini_25_flash_model] |
| 38 | +} |
| 39 | + |
| 40 | +object Gemini25FlashImagePreviewModelConfiguration { |
| 41 | + // [START android_gemini_developer_api_gemini_25_flash_image_model] |
| 42 | + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( |
| 43 | + modelName = "gemini-2.5-flash-image-preview", |
| 44 | + // Configure the model to respond with text and images (required) |
| 45 | + generationConfig = generationConfig { |
| 46 | + responseModalities = listOf( |
| 47 | + ResponseModality.TEXT, |
| 48 | + ResponseModality.IMAGE |
| 49 | + ) |
| 50 | + } |
| 51 | + ) |
| 52 | + // [END android_gemini_developer_api_gemini_25_flash_image_model] |
| 53 | +} |
| 54 | + |
| 55 | +fun textOnlyInput(scope: CoroutineScope) { |
| 56 | + val model = GeminiDeveloperApi25FlashModelConfiguration.model |
| 57 | + // [START android_gemini_developer_api_text_only_input] |
| 58 | + scope.launch { |
| 59 | + val response = model.generateContent("Write a story about a magic backpack.") |
| 60 | + } |
| 61 | + // [END android_gemini_developer_api_text_only_input] |
| 62 | +} |
| 63 | + |
| 64 | +fun textAndImageInput(scope: CoroutineScope, bitmap: Bitmap) { |
| 65 | + val model = GeminiDeveloperApi25FlashModelConfiguration.model |
| 66 | + // [START android_gemini_developer_api_multimodal_input] |
| 67 | + scope.launch { |
| 68 | + val response = model.generateContent( |
| 69 | + content { |
| 70 | + image(bitmap) |
| 71 | + text("what is the object in the picture?") |
| 72 | + } |
| 73 | + ) |
| 74 | + } |
| 75 | + // [END android_gemini_developer_api_multimodal_input] |
| 76 | +} |
| 77 | + |
| 78 | +fun textAndAudioInput( |
| 79 | + scope: CoroutineScope, |
| 80 | + contentResolver: ContentResolver, |
| 81 | + audioUri: Uri |
| 82 | +) { |
| 83 | + val model = GeminiDeveloperApi25FlashModelConfiguration.model |
| 84 | + // [START android_gemini_developer_api_multimodal_audio_input] |
| 85 | + scope.launch { |
| 86 | + contentResolver.openInputStream(audioUri).use { stream -> |
| 87 | + stream?.let { |
| 88 | + val bytes = it.readBytes() |
| 89 | + |
| 90 | + val prompt = content { |
| 91 | + inlineData(bytes, "audio/mpeg") // Specify the appropriate audio MIME type |
| 92 | + text("Transcribe this audio recording.") |
| 93 | + } |
| 94 | + |
| 95 | + val response = model.generateContent(prompt) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + // [END android_gemini_developer_api_multimodal_audio_input] |
| 100 | +} |
| 101 | + |
| 102 | +fun textAndVideoInput( |
| 103 | + scope: CoroutineScope, |
| 104 | + contentResolver: ContentResolver, |
| 105 | + videoUri: Uri |
| 106 | +) { |
| 107 | + val model = GeminiDeveloperApi25FlashModelConfiguration.model |
| 108 | + // [START android_gemini_developer_api_multimodal_video_input] |
| 109 | + scope.launch { |
| 110 | + contentResolver.openInputStream(videoUri).use { stream -> |
| 111 | + stream?.let { |
| 112 | + val bytes = it.readBytes() |
| 113 | + |
| 114 | + val prompt = content { |
| 115 | + inlineData(bytes, "video/mp4") // Specify the appropriate video MIME type |
| 116 | + text("Describe the content of this video") |
| 117 | + } |
| 118 | + |
| 119 | + val response = model.generateContent(prompt) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + // [END android_gemini_developer_api_multimodal_video_input] |
| 124 | +} |
| 125 | + |
| 126 | +fun multiTurnChat(scope: CoroutineScope) { |
| 127 | + val model = GeminiDeveloperApi25FlashModelConfiguration.model |
| 128 | + // [START android_gemini_developer_api_multiturn_chat] |
| 129 | + val chat = model.startChat( |
| 130 | + history = listOf( |
| 131 | + content(role = "user") { text("Hello, I have 2 dogs in my house.") }, |
| 132 | + content(role = "model") { text("Great to meet you. What would you like to know?") } |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + scope.launch { |
| 137 | + val response = chat.sendMessage("How many paws are in my house?") |
| 138 | + } |
| 139 | + // [END android_gemini_developer_api_multiturn_chat] |
| 140 | +} |
| 141 | + |
| 142 | +fun generateImageFromText(scope: CoroutineScope) { |
| 143 | + val model = Gemini25FlashImagePreviewModelConfiguration.model |
| 144 | + // [START android_gemini_developer_api_generate_image_from_text] |
| 145 | + scope.launch { |
| 146 | + // Provide a text prompt instructing the model to generate an image |
| 147 | + val prompt = |
| 148 | + "A hyper realistic picture of a t-rex with a blue bag pack roaming a pre-historic forest." |
| 149 | + // To generate image output, call `generateContent` with the text input |
| 150 | + val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt) |
| 151 | + .candidates.first().content.parts.filterIsInstance<ImagePart>() |
| 152 | + .firstOrNull()?.image |
| 153 | + } |
| 154 | + // [END android_gemini_developer_api_generate_image_from_text] |
| 155 | +} |
| 156 | + |
| 157 | +fun editImage(scope: CoroutineScope, bitmap: Bitmap) { |
| 158 | + val model = Gemini25FlashImagePreviewModelConfiguration.model |
| 159 | + // [START android_gemini_developer_api_edit_image] |
| 160 | + scope.launch { |
| 161 | + // Provide a text prompt instructing the model to edit the image |
| 162 | + val prompt = content { |
| 163 | + image(bitmap) |
| 164 | + text("Edit this image to make it look like a cartoon") |
| 165 | + } |
| 166 | + // To edit the image, call `generateContent` with the prompt (image and text input) |
| 167 | + val generatedImageAsBitmap: Bitmap? = model.generateContent(prompt) |
| 168 | + .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image |
| 169 | + // Handle the generated text and image |
| 170 | + } |
| 171 | + // [END android_gemini_developer_api_edit_image] |
| 172 | +} |
| 173 | + |
| 174 | +fun editImageWithChat(scope: CoroutineScope, bitmap: Bitmap) { |
| 175 | + val model = Gemini25FlashImagePreviewModelConfiguration.model |
| 176 | + // [START android_gemini_developer_api_edit_image_chat] |
| 177 | + scope.launch { |
| 178 | + // Create the initial prompt instructing the model to edit the image |
| 179 | + val prompt = content { |
| 180 | + image(bitmap) |
| 181 | + text("Edit this image to make it look like a cartoon") |
| 182 | + } |
| 183 | + // Initialize the chat |
| 184 | + val chat = model.startChat() |
| 185 | + // To generate an initial response, send a user message with the image and text prompt |
| 186 | + var response = chat.sendMessage(prompt) |
| 187 | + // Inspect the returned image |
| 188 | + var generatedImageAsBitmap: Bitmap? = response |
| 189 | + .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image |
| 190 | + // Follow up requests do not need to specify the image again |
| 191 | + response = chat.sendMessage("But make it old-school line drawing style") |
| 192 | + generatedImageAsBitmap = response |
| 193 | + .candidates.first().content.parts.filterIsInstance<ImagePart>().firstOrNull()?.image |
| 194 | + } |
| 195 | + // [END android_gemini_developer_api_edit_image_chat] |
| 196 | +} |
0 commit comments