| 
 | 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 | + | 
 | 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)  | 
 | 32 | + | 
 | 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)"  | 
 | 39 | + | 
 | 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)  | 
 | 43 | + | 
 | 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)  | 
 | 48 | +    }  | 
 | 49 | +  }  | 
 | 50 | + | 
 | 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")  | 
 | 56 | + | 
 | 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)  | 
 | 61 | +    }  | 
 | 62 | +  }  | 
 | 63 | +}  | 
0 commit comments