Skip to content

Commit

Permalink
Compile on SPM only
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Nov 22, 2024
1 parent 97f7f1d commit acb0bef
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,56 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import FirebaseCore
import FirebaseStorage
import FirebaseVertexAI

// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used as
// compilation tests.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class CloudStorageSnippets {
let model: GenerativeModel! = nil

func cloudStorageFile_referenceMIMEType() async throws {
// Upload an image file using Cloud Storage for Firebase.
let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
fatalError("File 'image.jpg' not found in main bundle.")
}
let metadata = try await storageRef.putFileAsync(from: imageURL)
#if SWIFT_PACKAGE // The FirebaseStorage dependency has only been added in Package.swift.

// Get the MIME type and Cloud Storage for Firebase URL.
guard let mimeType = metadata.contentType else {
fatalError("The MIME type of the uploaded image is nil.")
}
// Construct a URL in the required format.
let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"
import FirebaseCore
import FirebaseStorage
import FirebaseVertexAI

// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used
// as compilation tests.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class CloudStorageSnippets {
let model: GenerativeModel! = nil

func cloudStorageFile_referenceMIMEType() async throws {
// Upload an image file using Cloud Storage for Firebase.
let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
fatalError("File 'image.jpg' not found in main bundle.")
}
let metadata = try await storageRef.putFileAsync(from: imageURL)

let prompt = "What's in this picture?"
// Construct the imagePart with the MIME type and the URL.
let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)
// Get the MIME type and Cloud Storage for Firebase URL.
guard let mimeType = metadata.contentType else {
fatalError("The MIME type of the uploaded image is nil.")
}
// Construct a URL in the required format.
let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"

// To generate text output, call generateContent with the prompt and the imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
let prompt = "What's in this picture?"
// Construct the imagePart with the MIME type and the URL.
let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)

// To generate text output, call generateContent with the prompt and the imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
}
}
}

func cloudStorageFile_explicitMIMEType() async throws {
let prompt = "What's in this picture?"
// Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase
// URL values.
let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")
func cloudStorageFile_explicitMIMEType() async throws {
let prompt = "What's in this picture?"
// Construct an imagePart that explicitly includes the MIME type and Cloud Storage for
// Firebase URL values.
let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")

// To generate text output, call generateContent with the prompt and imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
// To generate text output, call generateContent with the prompt and imagePart.
let result = try await model.generateContent(prompt, imagePart)
if let text = result.text {
print(text)
}
}
}
}

#endif // SWIFT_PACKAGE

0 comments on commit acb0bef

Please sign in to comment.