Skip to content

Commit 7703865

Browse files
Add individual README.md files to each sample
1 parent d4f08c6 commit 7703865

File tree

12 files changed

+389
-1
lines changed

12 files changed

+389
-1
lines changed

.gemini/styleguide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ sealed class ScreenState {
7070
}
7171

7272
```
73+
* **Strongly recommended:** When a view model or the business logic is modified in the code of a sample, verify that these changes are properly reflected in the README.md of this sample.
7374
* **Recommended:** Do not use `AndroidViewModel`. Use the `ViewModel` class. Avoid using the `Application` class in ViewModels; move the dependency to the UI or data layer.
7475
* **Recommended:** Don't use `LiveData`, use state flow instead.
7576
* **Recommended:** Expose a UI state. Use a single `uiState` property (a `StateFlow`) for data exposure. Multiple properties can be used for unrelated data. Use `stateIn` with `WhileSubscribed(5000)` for data streams. For simpler cases, use a `MutableStateFlow` exposed as an immutable `StateFlow`. Consider using a data class or sealed class for the `UiState`.

samples/gemini-image-chat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ val content = content {
2626
val response = chat.sendMessage(content)
2727
```
2828

29-
Read more about [image generation with Gemini](https://developer.android.com/ai/gemini/developer-api#generate-images) in the Android Documentation.
29+
Read more about [image generation with Gemini](https://developer.android.com/ai/gemini/developer-api#generate-images) in the Android Documentation.

samples/gemini-live-todo/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Gemini Live Todo Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to use the Gemini Live API for real-time, voice-based interactions in a simple ToDo application. Users can add, remove, and update tasks by speaking to the app, showcasing a hands-free, conversational user experience powered by the Gemini API.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Gemini Live Todo in action" src="gemini_live_todo.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the Firebase AI SDK (see [How to run](../../#how-to-run)) for Android to interact with the `gemini-2.0-flash-live-preview-04-09` model. The core logic is in the [`TodoScreenViewModel.kt`](./src/main/java/com/android/ai/samples/geminilivetodo/ui/TodoScreenViewModel.kt) file. A `liveModel` is initialized with a set of function declarations (`addTodo`, `removeTodo`, `toggleTodoStatus`, `getTodoList`) that allow the model to interact with the ToDo list. When the user starts a voice conversation, the model processes the spoken commands and executes the corresponding functions to manage the tasks.
16+
17+
Here is the key snippet of code that initializes the model and connects to a live session:
18+
19+
```kotlin
20+
val generativeModel = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel(
21+
"gemini-2.0-flash-live-preview-04-09",
22+
generationConfig = liveGenerationConfig,
23+
systemInstruction = systemInstruction,
24+
tools = listOf(
25+
Tool.functionDeclarations(
26+
listOf(getTodoList, addTodo, removeTodo, toggleTodoStatus),
27+
),
28+
),
29+
)
30+
31+
try {
32+
session = generativeModel.connect()
33+
} catch (e: Exception) {
34+
Log.e(TAG, "Error connecting to the model", e)
35+
liveSessionState.value = LiveSessionState.Error
36+
}
37+
```
38+
39+
Read more about the [Gemini Live API](https://developer.android.com/ai/gemini/live) in the Android Documentation.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Gemini Multimodal Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates a multimodal (image and text) prompt, using the `Gemini 2.5 Flash` model. Users can select an image and provide a text prompt, and the generative model will respond based on both inputs. This showcases how to build a simple, yet powerful, multimodal AI with the Gemini API.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Gemini Multimodal in action" src="gemini_multimodal.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the Firebase AI SDK (see [How to run](../../#how-to-run)) for Android to interact with the `gemini-2.5-flash` model. The core logic is in the [`GeminiDataSource.kt`](./src/main/java/com/android/ai/samples/geminimultimodal/data/GeminiDataSource.kt) file. A `generativeModel` is initialized, and then a `chat` session is started from it. When a user provides an image and a text prompt, they are combined into a multimodal prompt and sent to the model, which then generates a text response.
16+
17+
Here is the key snippet of code that initializes the generative model:
18+
19+
```kotlin
20+
private val generativeModel by lazy {
21+
Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
22+
"gemini-2.5-flash",
23+
generationConfig = generationConfig {
24+
temperature = 0.9f
25+
topK = 32
26+
topP = 1f
27+
maxOutputTokens = 4096
28+
},
29+
safetySettings = listOf(
30+
SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.MEDIUM_AND_ABOVE),
31+
SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE),
32+
SafetySetting(HarmCategory.SEXUALLY_EXPLICIT, HarmBlockThreshold.MEDIUM_AND_ABOVE),
33+
SafetySetting(HarmCategory.DANGEROUS_CONTENT, HarmBlockThreshold.MEDIUM_AND_ABOVE),
34+
),
35+
)
36+
}
37+
```
38+
39+
Here is the key snippet of code that calls the [`generateText`](./src/main/java/com/android/ai/samples/geminimultimodal/data/GeminiDataSource.kt) function:
40+
41+
```kotlin
42+
suspend fun generateText(bitmap: Bitmap, prompt: String): String {
43+
val multimodalPrompt = content {
44+
image(bitmap)
45+
text(prompt)
46+
}
47+
val result = generativeModel.generateContent(multimodalPrompt)
48+
return result.text ?: ""
49+
}
50+
```
51+
52+
Read more about [the Gemini API](https://developer.android.com/ai/gemini) in the Android Documentation.
53+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Gemini Video Metadata Creation Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to generate various types of video metadata (description, hashtags, chapters, account tags, links, and thumbnails) using the `Gemini 2.5 Flash` model. Users can select a video, and the generative model will analyze its content to provide relevant metadata, showcasing how to enrich video content with AI-powered insights.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Gemini Video Metadata Creation in action" src="gemini_video_metadata.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the Firebase AI SDK (see [How to run](../../#how-to-run)) for Android to interact with the `gemini-2.5-flash` model. The core logic involves several functions (e.g., [`generateDescription`](./src/main/java/com/android/ai/samples/geminivideometadatacreation/GenerateDescription.kt), `generateHashtags`, `generateChapters`, `generateAccountTags`, `generateLinks`, `generateThumbnails`) that send video content to the Gemini API for analysis. The model processes the video and returns structured metadata based on the specific prompt.
16+
17+
Here is a key snippet of code that generates a video description:
18+
19+
```kotlin
20+
suspend fun generateDescription(videoUri: Uri): @Composable () -> Unit {
21+
val response = Firebase.ai(backend = GenerativeBackend.vertexAI())
22+
.generativeModel(modelName = "gemini-2.5-flash")
23+
.generateContent(
24+
content {
25+
fileData(videoUri.toString(), "video/mp4")
26+
text(
27+
"""
28+
Provide a compelling and concise description for this video in less than 100 words.
29+
Don't assume if you don't know.
30+
The description should be engaging and accurately reflect the video's content.
31+
You should output your responses in HTML format. Use styling sparingly. You can use the following tags:
32+
* Bold: <b>
33+
* Italic: <i>
34+
* Underline: <u>
35+
* Bullet points: <ul>, <li>
36+
""".trimIndent(),
37+
)
38+
},
39+
)
40+
41+
val responseText = response.text
42+
return if (responseText != null) {
43+
{ DescriptionUi(responseText) }
44+
} else {
45+
{ ErrorUi(response.promptFeedback?.blockReasonMessage) }
46+
}
47+
}
48+
```
49+
50+
Read more about [the Gemini API](https://developer.android.com/ai/gemini) in the Android Documentation.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gemini Video Summarization Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to generate a text summary of a video using the `Gemini 2.5 Flash` model. Users can select a video, and the generative model will analyze its content to provide a concise summary, showcasing how to extract key information from video content with the Gemini API.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Gemini Video Summarization in action" src="gemini_video_summarization.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the Firebase AI SDK (see [How to run](../../#how-to-run)) for Android to interact with the `gemini-2.5-flash` model. The core logic is in the [`VideoSummarizationViewModel.kt`](./src/main/java/com/android/ai/samples/geminivideosummary/viewmodel/VideoSummarizationViewModel.kt) file. A `generativeModel` is initialized. When a user requests a summary, the video content and a text prompt are sent to the model, which then generates a text summary.
16+
17+
Here is the key snippet of code that calls the generative model:
18+
19+
```kotlin
20+
val generativeModel =
21+
Firebase.ai(backend = GenerativeBackend.vertexAI())
22+
.generativeModel("gemini-2.5-flash")
23+
24+
val requestContent = content {
25+
fileData(videoSource.toString(), "video/mp4")
26+
text(promptData)
27+
}
28+
val outputStringBuilder = StringBuilder()
29+
generativeModel.generateContentStream(requestContent).collect { response ->
30+
outputStringBuilder.append(response.text)
31+
}
32+
```
33+
34+
Read more about [getting started with Gemini](https://developer.android.com/ai/gemini) in the Android Documentation.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Image Description with Nano Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to generate short descriptions of images on-device using the GenAI API powered by Gemini Nano. Users can select an image, and the model will generate a descriptive text, showcasing the power of on-device multimodal AI.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Image Description with Nano in action" src="nano_image_description.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the ML Kit GenAI Image Description API to interact with the on-device Gemini Nano model. The core logic is in the [`GenAIImageDescriptionViewModel.kt`](https://github.com/android/ai-samples/blob/main/samples/genai-image-description/src/main/java/com/android/ai/samples/genai_image_description/GenAIImageDescriptionViewModel.kt) file. An `ImageDescriber` client is initialized. When a user provides an image, it's converted to a bitmap and sent to the `runInference` method, which streams back the generated description.
16+
17+
Here is the key snippet of code that calls the generative model:
18+
19+
```kotlin
20+
private var imageDescriber: ImageDescriber = ImageDescription.getClient(
21+
ImageDescriberOptions.builder(context).build(),
22+
)
23+
//...
24+
25+
private suspend fun generateImageDescription(imageUri: Uri) {
26+
_uiState.value = GenAIImageDescriptionUiState.Generating("")
27+
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
28+
val request = ImageDescriptionRequest.builder(bitmap).build()
29+
30+
imageDescriber.runInference(request) { newText ->
31+
_uiState.update {
32+
(it as? GenAIImageDescriptionUiState.Generating)?.copy(partialOutput = it.partialOutput + newText) ?: it
33+
}
34+
}.await()
35+
// ...
36+
}
37+
```
38+
39+
Read more about [Gemini Nano](https://developer.android.com/ai/gemini-nano/ml-kit-genai) in the Android Documentation.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Summarization with Nano Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to summarize articles and conversations on-device using the GenAI API powered by Gemini Nano. Users can input text, and the model will generate a concise summary, showcasing the power of on-device text processing with AI.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Summarization with Nano in action" src="nano_summarization.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the ML Kit GenAI Summarization API to interact with the on-device Gemini Nano model. The core logic is in the `GenAISummarizationViewModel.kt` file. A `Summarizer` client is initialized. When a user provides text, it's passed to the `runInference` method, which streams back the generated summary.
16+
17+
Here is the key snippet of code that calls the generative model from [`GenAISummarizationViewModel.kt`](./src/main/java/com/android/ai/samples/genai_summarization/GenAISummarizationViewModel.kt):
18+
19+
```kotlin
20+
private suspend fun generateSummarization(summarizer: Summarizer, textToSummarize: String) {
21+
_uiState.value = GenAISummarizationUiState.Generating("")
22+
val summarizationRequest = SummarizationRequest.builder(textToSummarize).build()
23+
24+
try {
25+
// Instead of using await() here, alternatively you can attach a FutureCallback<SummarizationResult>
26+
summarizer.runInference(summarizationRequest) { newText ->
27+
(_uiState.value as? GenAISummarizationUiState.Generating)?.let { generatingState ->
28+
_uiState.value = generatingState.copy(generatedOutput = generatingState.generatedOutput + newText)
29+
}
30+
}.await()
31+
} catch (genAiException: GenAiException) {
32+
// ...
33+
}
34+
// ...
35+
}
36+
```
37+
38+
Read more about [Gemini Nano](https://developer.android.com/ai/gemini-nano/ml-kit-genai) in the Android Documentation.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Writing Assistance with Nano Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to proofread and rewrite short content on-device using the ML Kit GenAI APIs powered by Gemini Nano. Users can input text and choose to either proofread it for grammar and spelling errors or rewrite it in various styles, showcasing on-device text manipulation with AI.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Writing Assistance with Nano in action" src="nano_rewrite.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the ML Kit GenAI Proofreading and Rewriting APIs to interact with the on-device Gemini Nano model. The core logic is in the [`GenAIWritingAssistanceViewModel.kt`](https://github.com/android/ai-samples/blob/main/samples/genai-writing-assistance/src/main/java/com/android/ai/samples/genai_writing_assistance/GenAIWritingAssistanceViewModel.kt) file. `Proofreader` and `Rewriter` clients are initialized. When a user provides text, it's passed to either the `runProofreadingInference` or `runRewritingInference` method, which then returns the polished text.
16+
17+
Here is the key snippet of code that runs the proofreading inference from [`GenAIWritingAssistanceViewModel.kt`](.src/main/java/com/android/ai/samples/genai_writing_assistance/GenAIWritingAssistanceViewModel.kt):
18+
19+
```kotlin
20+
private suspend fun runProofreadingInference(textToProofread: String) {
21+
val proofreadRequest = ProofreadingRequest.builder(textToProofread).build()
22+
// More than 1 result may be generated. Results are returned in descending order of
23+
// quality of confidence. Here we use the first result which has the highest quality
24+
// of confidence.
25+
_uiState.value = GenAIWritingAssistanceUiState.Generating
26+
val results = proofreader.runInference(proofreadRequest).await()
27+
_uiState.value = GenAIWritingAssistanceUiState.Success(results.results[0].text)
28+
}
29+
```
30+
31+
Read more about [Gemini Nano](https://developer.android.com/ai/gemini-nano/ml-kit-genai) in the Android Documentation.

samples/imagen-editing/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Imagen Image Editing Sample
2+
3+
This sample is part of the [AI Sample Catalog](../../). To build and run this sample, you should clone the entire repository.
4+
5+
## Description
6+
7+
This sample demonstrates how to edit images using the Imagen editing model. Users can generate an image, then draw a mask on it and provide a text prompt to inpaint (fill in) the masked area, showcasing advanced image manipulation capabilities with the Imagen API.
8+
9+
<div style="text-align: center;">
10+
<img width="320" alt="Imagen Image Editing in action" src="imagen_editing.png" />
11+
</div>
12+
13+
## How it works
14+
15+
The application uses the Firebase AI SDK (see [How to run](../../#how-to-run)) for Android to interact with the `imagen-4.0-ultra-generate-001` and `imagen-3.0-capability-001` models. The core logic is in the [`ImagenEditingDataSource.kt`](https://github.com/android/ai-samples/blob/main/samples/imagen-editing/src/main/java/com/android/ai/samples/imagenediting/data/ImagenEditingDataSource.kt) file. It first generates a base image using the generation model. Then, for editing, it takes the source image, a user-drawn mask, and a text prompt, and sends them to the editing model's `editImage` method to perform inpainting.
16+
17+
Here is the key snippet of code that performs inpainting from [`ImagenEditingDataSource.kt`](./src/main/java/com/android/ai/samples/imagenediting/data/ImagenEditingDataSource.kt):
18+
19+
```kotlin
20+
@OptIn(PublicPreviewAPI::class)
21+
suspend fun inpaintImageWithMask(sourceImage: Bitmap, maskImage: Bitmap, prompt: String, editSteps: Int = DEFAULT_EDIT_STEPS): Bitmap {
22+
val imageResponse = editingModel.editImage(
23+
referenceImages = listOf(
24+
ImagenRawImage(sourceImage.toImagenInlineImage()),
25+
ImagenRawMask(maskImage.toImagenInlineImage()),
26+
),
27+
prompt = prompt,
28+
config = ImagenEditingConfig(
29+
editMode = ImagenEditMode.INPAINT_INSERTION,
30+
editSteps = editSteps,
31+
),
32+
)
33+
return imageResponse.images.first().asBitmap()
34+
}
35+
```
36+
37+
Read more about [Imagen](https://developer.android.com/ai/imagen) in the Android Documentation.

0 commit comments

Comments
 (0)