-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Motsonashvili
committed
Feb 26, 2025
1 parent
ad17c02
commit 61886b9
Showing
8 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...app/src/main/kotlin/com/google/firebase/quickstart/vertexai/feature/image/ImagenScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.firebase.quickstart.vertexai.feature.image | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.CardDefaults | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.ElevatedCard | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import com.google.firebase.quickstart.vertexai.GenerativeViewModelFactory | ||
import com.google.firebase.quickstart.vertexai.R | ||
import com.google.firebase.quickstart.vertexai.ui.theme.GenerativeAISample | ||
|
||
@Composable | ||
internal fun ImagenRoute( | ||
imagenViewModel: ImagenViewModel = viewModel(factory = GenerativeViewModelFactory) | ||
) { | ||
val imagenUiState by imagenViewModel.uiState.collectAsState() | ||
|
||
ImagenScreen(imagenUiState, onImagenClicked = { inputText -> | ||
imagenViewModel.generateImage(inputText) | ||
}) | ||
} | ||
|
||
@Composable | ||
fun ImagenScreen( | ||
uiState: ImagenUiState = ImagenUiState.Loading, | ||
onImagenClicked: (String) -> Unit = {} | ||
) { | ||
var imagenPrompt by rememberSaveable { mutableStateOf("") } | ||
|
||
Column( | ||
modifier = Modifier | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
ElevatedCard( | ||
modifier = Modifier | ||
.padding(all = 16.dp) | ||
.fillMaxWidth(), | ||
shape = MaterialTheme.shapes.large | ||
) { | ||
OutlinedTextField( | ||
value = imagenPrompt, | ||
label = { Text(stringResource(R.string.imagen_label)) }, | ||
placeholder = { Text(stringResource(R.string.imagen_hint)) }, | ||
onValueChange = { imagenPrompt = it }, | ||
modifier = Modifier | ||
.padding(16.dp) | ||
.fillMaxWidth() | ||
) | ||
TextButton( | ||
onClick = { | ||
if (imagenPrompt.isNotBlank()) { | ||
onImagenClicked(imagenPrompt) | ||
} | ||
}, | ||
modifier = Modifier | ||
.padding(end = 16.dp, bottom = 16.dp) | ||
.align(Alignment.End) | ||
) { | ||
Text(stringResource(R.string.action_go)) | ||
} | ||
} | ||
|
||
when (uiState) { | ||
ImagenUiState.Initial -> { | ||
// Nothing is shown | ||
} | ||
|
||
ImagenUiState.Loading -> { | ||
Box( | ||
contentAlignment = Alignment.Center, | ||
modifier = Modifier | ||
.padding(all = 8.dp) | ||
.align(Alignment.CenterHorizontally) | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
} | ||
|
||
is ImagenUiState.Success -> { | ||
Card( | ||
modifier = Modifier | ||
.padding(start = 16.dp, end = 16.dp, bottom = 16.dp) | ||
.fillMaxWidth(), | ||
shape = MaterialTheme.shapes.large, | ||
colors = CardDefaults.cardColors( | ||
containerColor = MaterialTheme.colorScheme.onSecondaryContainer | ||
) | ||
) { | ||
Image(bitmap = uiState.image.asImageBitmap(), "") | ||
} | ||
} | ||
|
||
is ImagenUiState.Error -> { | ||
Card( | ||
modifier = Modifier | ||
.padding(horizontal = 16.dp) | ||
.fillMaxWidth(), | ||
shape = MaterialTheme.shapes.large, | ||
colors = CardDefaults.cardColors( | ||
containerColor = MaterialTheme.colorScheme.errorContainer | ||
) | ||
) { | ||
Text( | ||
text = uiState.errorMessage, | ||
color = MaterialTheme.colorScheme.error, | ||
modifier = Modifier.padding(all = 16.dp) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@Preview(showSystemUi = true) | ||
fun ImagenScreenPreview() { | ||
GenerativeAISample(darkTheme = true) { | ||
ImagenScreen() | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...pp/src/main/kotlin/com/google/firebase/quickstart/vertexai/feature/image/ImagenUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.firebase.quickstart.vertexai.feature.image | ||
|
||
import android.graphics.Bitmap | ||
|
||
/** | ||
* A sealed hierarchy describing the state of the text generation. | ||
*/ | ||
sealed interface ImagenUiState { | ||
|
||
/** | ||
* Empty state when the screen is first shown | ||
*/ | ||
data object Initial : ImagenUiState | ||
|
||
/** | ||
* Still loading | ||
*/ | ||
data object Loading : ImagenUiState | ||
|
||
/** | ||
* Text has been generated | ||
*/ | ||
data class Success( | ||
val image: Bitmap | ||
) : ImagenUiState | ||
|
||
/** | ||
* There was an error generating an image | ||
*/ | ||
data class Error( | ||
val errorMessage: String | ||
) : ImagenUiState | ||
} |
51 changes: 51 additions & 0 deletions
51
.../src/main/kotlin/com/google/firebase/quickstart/vertexai/feature/image/ImagenViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.firebase.quickstart.vertexai.feature.image | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.firebase.vertexai.ImagenModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class ImagenViewModel( | ||
private val imageModel: ImagenModel | ||
) : ViewModel() { | ||
|
||
private val _uiState: MutableStateFlow<ImagenUiState> = | ||
MutableStateFlow(ImagenUiState.Initial) | ||
val uiState: StateFlow<ImagenUiState> = _uiState.asStateFlow() | ||
|
||
fun generateImage(inputText: String) { | ||
_uiState.value = ImagenUiState.Loading | ||
|
||
viewModelScope.launch { | ||
// Non-streaming | ||
try { | ||
val imageResponse = imageModel.generateImages( | ||
inputText | ||
) | ||
_uiState.value = | ||
ImagenUiState.Success(imageResponse.images.first().asBitmap()) | ||
} catch (e: Exception) { | ||
_uiState.value = ImagenUiState.Error(e.localizedMessage ?: "") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters