CloudStorageSnippets.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if SWIFT_PACKAGE // The FirebaseStorage dependency has only been added in Package.swift.
  15. import FirebaseAILogic
  16. import FirebaseCore
  17. import FirebaseStorage
  18. // These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used
  19. // as compilation tests.
  20. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  21. final class CloudStorageSnippets {
  22. let model: GenerativeModel! = nil
  23. func cloudStorageFile_referenceMIMEType() async throws {
  24. // Upload an image file using Cloud Storage for Firebase.
  25. let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
  26. guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
  27. fatalError("File 'image.jpg' not found in main bundle.")
  28. }
  29. let metadata = try await storageRef.putFileAsync(from: imageURL)
  30. // Get the MIME type and Cloud Storage for Firebase URL.
  31. guard let mimeType = metadata.contentType else {
  32. fatalError("The MIME type of the uploaded image is nil.")
  33. }
  34. // Construct a URL in the required format.
  35. let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"
  36. let prompt = "What's in this picture?"
  37. // Construct the imagePart with the MIME type and the URL.
  38. let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)
  39. // To generate text output, call generateContent with the prompt and the imagePart.
  40. let result = try await model.generateContent(prompt, imagePart)
  41. if let text = result.text {
  42. print(text)
  43. }
  44. }
  45. func cloudStorageFile_explicitMIMEType() async throws {
  46. let prompt = "What's in this picture?"
  47. // Construct an imagePart that explicitly includes the MIME type and
  48. // Cloud Storage for Firebase URL values.
  49. let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")
  50. // To generate text output, call generateContent with the prompt and imagePart.
  51. let result = try await model.generateContent(prompt, imagePart)
  52. if let text = result.text {
  53. print(text)
  54. }
  55. }
  56. }
  57. #endif // SWIFT_PACKAGE