|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import FirebaseCore |
16 | | -import FirebaseStorage |
17 | | -import FirebaseVertexAI |
18 | | - |
19 | | -// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used as |
20 | | -// compilation tests. |
21 | | -@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
22 | | -final class CloudStorageSnippets { |
23 | | - let model: GenerativeModel! = nil |
24 | | - |
25 | | - func cloudStorageFile_referenceMIMEType() async throws { |
26 | | - // Upload an image file using Cloud Storage for Firebase. |
27 | | - let storageRef = Storage.storage().reference(withPath: "images/image.jpg") |
28 | | - guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { |
29 | | - fatalError("File 'image.jpg' not found in main bundle.") |
30 | | - } |
31 | | - let metadata = try await storageRef.putFileAsync(from: imageURL) |
| 15 | +#if SWIFT_PACKAGE // The FirebaseStorage dependency has only been added in Package.swift. |
32 | 16 |
|
33 | | - // Get the MIME type and Cloud Storage for Firebase URL. |
34 | | - guard let mimeType = metadata.contentType else { |
35 | | - fatalError("The MIME type of the uploaded image is nil.") |
36 | | - } |
37 | | - // Construct a URL in the required format. |
38 | | - let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" |
| 17 | + import FirebaseCore |
| 18 | + import FirebaseStorage |
| 19 | + import FirebaseVertexAI |
| 20 | + |
| 21 | + // These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used |
| 22 | + // as compilation tests. |
| 23 | + @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 24 | + final class CloudStorageSnippets { |
| 25 | + let model: GenerativeModel! = nil |
| 26 | + |
| 27 | + func cloudStorageFile_referenceMIMEType() async throws { |
| 28 | + // Upload an image file using Cloud Storage for Firebase. |
| 29 | + let storageRef = Storage.storage().reference(withPath: "images/image.jpg") |
| 30 | + guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { |
| 31 | + fatalError("File 'image.jpg' not found in main bundle.") |
| 32 | + } |
| 33 | + let metadata = try await storageRef.putFileAsync(from: imageURL) |
39 | 34 |
|
40 | | - let prompt = "What's in this picture?" |
41 | | - // Construct the imagePart with the MIME type and the URL. |
42 | | - let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) |
| 35 | + // Get the MIME type and Cloud Storage for Firebase URL. |
| 36 | + guard let mimeType = metadata.contentType else { |
| 37 | + fatalError("The MIME type of the uploaded image is nil.") |
| 38 | + } |
| 39 | + // Construct a URL in the required format. |
| 40 | + let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" |
43 | 41 |
|
44 | | - // To generate text output, call generateContent with the prompt and the imagePart. |
45 | | - let result = try await model.generateContent(prompt, imagePart) |
46 | | - if let text = result.text { |
47 | | - print(text) |
| 42 | + let prompt = "What's in this picture?" |
| 43 | + // Construct the imagePart with the MIME type and the URL. |
| 44 | + let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) |
| 45 | + |
| 46 | + // To generate text output, call generateContent with the prompt and the imagePart. |
| 47 | + let result = try await model.generateContent(prompt, imagePart) |
| 48 | + if let text = result.text { |
| 49 | + print(text) |
| 50 | + } |
48 | 51 | } |
49 | | - } |
50 | 52 |
|
51 | | - func cloudStorageFile_explicitMIMEType() async throws { |
52 | | - let prompt = "What's in this picture?" |
53 | | - // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase |
54 | | - // URL values. |
55 | | - let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") |
| 53 | + func cloudStorageFile_explicitMIMEType() async throws { |
| 54 | + let prompt = "What's in this picture?" |
| 55 | + // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for |
| 56 | + // Firebase URL values. |
| 57 | + let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") |
56 | 58 |
|
57 | | - // To generate text output, call generateContent with the prompt and imagePart. |
58 | | - let result = try await model.generateContent(prompt, imagePart) |
59 | | - if let text = result.text { |
60 | | - print(text) |
| 59 | + // To generate text output, call generateContent with the prompt and imagePart. |
| 60 | + let result = try await model.generateContent(prompt, imagePart) |
| 61 | + if let text = result.text { |
| 62 | + print(text) |
| 63 | + } |
61 | 64 | } |
62 | 65 | } |
63 | | -} |
| 66 | + |
| 67 | +#endif // SWIFT_PACKAGE |
0 commit comments