Skip to content

Commit af7b3c8

Browse files
author
David Motsonashvili
committed
fixes for comments
1 parent bc4bcc6 commit af7b3c8

File tree

4 files changed

+20
-72
lines changed

4 files changed

+20
-72
lines changed

firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/FirebaseAISamples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ val FIREBASE_AI_SAMPLES = listOf(
362362
allowEmptyPrompt = false,
363363
editingMode = EditingMode.TEMPLATE,
364364
// To make this work on your project, create an `Input + System Instructions` template in your project with this name
365-
templateId = "textgen-test-template",
365+
templateId = "input-system-instructions",
366366
templateKey = "customerName"
367367
),
368368

firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/media/imagen/ImagenViewModel.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ImagenViewModel(
8080

8181
// Firebase AI Logic
8282
private val imagenModel: ImagenModel
83+
private val templateImagenModel: TemplateImagenModel
8384

8485
init {
8586
val config = imagenGenerationConfig {
@@ -97,28 +98,30 @@ class ImagenViewModel(
9798
generationConfig = config,
9899
safetySettings = settings
99100
)
101+
templateImagenModel = Firebase.ai.templateImagenModel()
100102
}
101103

102104
fun generateImages(inputText: String) {
103105
viewModelScope.launch {
104106
_isLoading.value = true
107+
_errorMessage.value = null // clear error message
105108
try {
106109
val imageResponse = when(sample.editingMode) {
107110
EditingMode.INPAINTING -> inpaint(imagenModel, inputText)
108111
EditingMode.OUTPAINTING -> outpaint(imagenModel, inputText)
109112
EditingMode.SUBJECT_REFERENCE -> drawReferenceSubject(imagenModel, inputText)
110113
EditingMode.STYLE_TRANSFER -> transferStyle(imagenModel, inputText)
111-
EditingMode.TEMPLATE -> generateWithTemplate(Firebase.ai.templateImagenModel(), templateId!!, templateKey!!, inputText)
114+
EditingMode.TEMPLATE -> generateWithTemplate(templateImagenModel, templateId!!, mapOf(templateKey!! to inputText))
112115
else -> generate(imagenModel, inputText)
113116
}
114117
_generatedBitmaps.value = imageResponse.images.map { it.asBitmap() }
115-
_errorMessage.value = null // clear error message
116118
} catch (e: Exception) {
117-
val errorMessage = if ((e.localizedMessage?.contains("not found") == true) && (templateId != null)) {
118-
"Template was not found, please add a template named $templateId to your project."
119-
} else {
120-
e.localizedMessage
121-
}
119+
val errorMessage =
120+
if ((e.localizedMessage?.contains("not found") == true) && sample.editingMode == EditingMode.TEMPLATE) {
121+
"Template was not found, please verify that your project contains a template named \"$templateId\"."
122+
} else {
123+
e.localizedMessage
124+
}
122125
_errorMessage.value = errorMessage
123126
} finally {
124127
_isLoading.value = false
@@ -227,9 +230,8 @@ class ImagenViewModel(
227230
suspend fun generateWithTemplate(
228231
model: TemplateImagenModel,
229232
templateId: String,
230-
templateKey: String,
231-
inputText: String,
233+
inputMap: Map<String, String>
232234
): ImagenGenerationResponse<ImagenInlineImage> {
233-
return model.generateImages(templateId, mapOf(templateKey to inputText))
235+
return model.generateImages(templateId, inputMap)
234236
}
235237
}

firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/text/TextGenScreen.kt

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ fun TextGenScreen(
8686
textGenViewModel.generate(textPrompt)
8787
}
8888
},
89-
modifier = Modifier
90-
.padding(end = 16.dp, bottom = 16.dp)
89+
modifier = Modifier.padding(end = 16.dp, bottom = 16.dp)
9190
) {
9291
Text("Generate")
9392
}
@@ -141,57 +140,3 @@ fun TextGenScreen(
141140
}
142141
}
143142
}
144-
145-
@Composable
146-
fun DropDownMenu(items: List<String>, onClick: (String) -> Unit) {
147-
148-
val isDropDownExpanded = remember {
149-
mutableStateOf(false)
150-
}
151-
152-
val itemPosition = remember {
153-
mutableIntStateOf(0)
154-
}
155-
156-
157-
Column(
158-
horizontalAlignment = Alignment.Start,
159-
verticalArrangement = Arrangement.Top,
160-
modifier = Modifier.padding(horizontal = 10.dp)
161-
) {
162-
163-
Box {
164-
Row(
165-
horizontalArrangement = Arrangement.Start,
166-
verticalAlignment = Alignment.Top,
167-
modifier = Modifier.clickable {
168-
isDropDownExpanded.value = true
169-
}
170-
) {
171-
Text(text = items[itemPosition.intValue])
172-
Image(
173-
painter = painterResource(id = R.drawable.round_arrow_drop_down_24),
174-
contentDescription = "Dropdown Icon"
175-
)
176-
}
177-
DropdownMenu(
178-
expanded = isDropDownExpanded.value,
179-
onDismissRequest = {
180-
isDropDownExpanded.value = false
181-
}) {
182-
items.forEachIndexed { index, item ->
183-
DropdownMenuItem(
184-
text = {
185-
Text(text = item)
186-
},
187-
onClick = {
188-
isDropDownExpanded.value = false
189-
itemPosition.intValue = index
190-
onClick(item)
191-
})
192-
}
193-
}
194-
}
195-
196-
}
197-
}

firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/text/TextGenViewModel.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
1313
import kotlinx.coroutines.flow.StateFlow
1414
import kotlinx.coroutines.launch
1515
import com.google.firebase.ai.GenerativeModel
16+
import com.google.firebase.ai.TemplateGenerativeModel
1617

1718
@OptIn(PublicPreviewAPI::class)
1819
class TextGenViewModel(
@@ -28,19 +29,18 @@ class TextGenViewModel(
2829
private val _isLoading = MutableStateFlow(false)
2930
val isLoading: StateFlow<Boolean> = _isLoading
3031

31-
3232
val allowEmptyPrompt = sample.allowEmptyPrompt
3333

3434
val templateId = sample.templateId
3535

3636
val templateKey = sample.templateKey
3737

38-
3938
private val _generatedText = MutableStateFlow<String?>(null)
4039
val generatedText: StateFlow<String?> = _generatedText
4140

4241
// Firebase AI Logic
4342
private val generativeModel: GenerativeModel
43+
private val templateGenerativeModel: TemplateGenerativeModel
4444

4545
init {
4646
generativeModel = Firebase.ai(
@@ -51,24 +51,25 @@ class TextGenViewModel(
5151
generationConfig = sample.generationConfig,
5252
tools = sample.tools
5353
)
54+
templateGenerativeModel = Firebase.ai.templateGenerativeModel()
5455
}
5556

5657
fun generate(inputText: String) {
5758
viewModelScope.launch {
5859
_isLoading.value = true
60+
_errorMessage.value = null // clear error message
5961
try {
6062
val generativeResponse = if (templateId != null) {
61-
Firebase.ai.templateGenerativeModel()
63+
templateGenerativeModel
6264
.generateContent(templateId, mapOf(templateKey!! to inputText))
6365
} else {
6466
generativeModel.generateContent(inputText)
6567
}
6668
_generatedText.value = generativeResponse.text
67-
_errorMessage.value = null // clear error message
6869
} catch (e: Exception) {
6970
val errorMessage =
7071
if ((e.localizedMessage?.contains("not found") == true) && (templateId != null)) {
71-
"Template was not found, please add a template named $templateId to your project."
72+
"Template was not found, please verify that your project contains a template named \"$templateId\"."
7273
} else {
7374
e.localizedMessage
7475
}

0 commit comments

Comments
 (0)