|
14 | 14 |
|
15 | 15 | import 'package:flutter/material.dart'; |
16 | 16 | import 'package:firebase_vertexai/firebase_vertexai.dart'; |
| 17 | +import 'package:firebase_storage/firebase_storage.dart'; |
17 | 18 | import '../widgets/message_widget.dart'; |
18 | 19 |
|
19 | 20 | class ImagenPage extends StatefulWidget { |
@@ -120,6 +121,20 @@ class _ImagenPageState extends State<ImagenPage> { |
120 | 121 | Icons.image_search, |
121 | 122 | color: Theme.of(context).colorScheme.primary, |
122 | 123 | ), |
| 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', |
123 | 138 | ) |
124 | 139 | else |
125 | 140 | const CircularProgressIndicator(), |
@@ -158,6 +173,36 @@ class _ImagenPageState extends State<ImagenPage> { |
158 | 173 | }); |
159 | 174 | } |
160 | 175 |
|
| 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 | + |
161 | 206 | void _showError(String message) { |
162 | 207 | showDialog<void>( |
163 | 208 | context: context, |
|
0 commit comments