Skip to content

Commit d95abfe

Browse files
committed
Add example for gcs generation
1 parent 7b31d08 commit d95abfe

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

packages/firebase_vertexai/firebase_vertexai/example/lib/pages/imagen_page.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import 'package:flutter/material.dart';
1616
import 'package:firebase_vertexai/firebase_vertexai.dart';
17+
import 'package:firebase_storage/firebase_storage.dart';
1718
import '../widgets/message_widget.dart';
1819

1920
class ImagenPage extends StatefulWidget {
@@ -120,6 +121,20 @@ class _ImagenPageState extends State<ImagenPage> {
120121
Icons.image_search,
121122
color: Theme.of(context).colorScheme.primary,
122123
),
124+
tooltip: 'Imagen raw data',
125+
)
126+
else
127+
const CircularProgressIndicator(),
128+
if (!_loading)
129+
IconButton(
130+
onPressed: () async {
131+
await _testImagenGCS(_textController.text);
132+
},
133+
icon: Icon(
134+
Icons.imagesearch_roller,
135+
color: Theme.of(context).colorScheme.primary,
136+
),
137+
tooltip: 'Imagen GCS',
123138
)
124139
else
125140
const CircularProgressIndicator(),
@@ -158,6 +173,36 @@ class _ImagenPageState extends State<ImagenPage> {
158173
});
159174
}
160175

176+
Future<void> _testImagenGCS(String prompt) async {
177+
setState(() {
178+
_loading = true;
179+
});
180+
var gcsUrl = 'gs://vertex-ai-example-ef5a2.appspot.com/imagen';
181+
182+
var response = await _imagenModel.generateImagesGCS(prompt, gcsUrl);
183+
184+
if (response.images.isNotEmpty) {
185+
var imagenImage = response.images[0];
186+
final returnImageUri = imagenImage.gcsUri;
187+
final reference = FirebaseStorage.instance.refFromURL(returnImageUri);
188+
final downloadUrl = await reference.getDownloadURL();
189+
// Process the image
190+
_generatedContent.add(
191+
MessageData(
192+
image: Image(image: NetworkImage(downloadUrl)),
193+
text: prompt,
194+
fromUser: false,
195+
),
196+
);
197+
} else {
198+
// Handle the case where no images were generated
199+
_showError('Error: No images were generated.');
200+
}
201+
setState(() {
202+
_loading = false;
203+
});
204+
}
205+
161206
void _showError(String message) {
162207
showDialog<void>(
163208
context: context,

packages/firebase_vertexai/firebase_vertexai/example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies:
2020
# Use with the CupertinoIcons class for iOS style icons.
2121
cupertino_icons: ^1.0.6
2222
firebase_core: ^3.10.1
23+
firebase_storage: ^12.4.1
2324
firebase_vertexai: ^1.1.1
2425
flutter:
2526
sdk: flutter

0 commit comments

Comments
 (0)