Skip to content

Commit

Permalink
Add support for Mac Catalyst
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Dec 14, 2023
1 parent 8d6462c commit 5759e7b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let package = Package(
platforms: [
.iOS(.v15),
.macOS(.v12),
.macCatalyst(.v15),
],
products: [
// Products define the executables and libraries a package produces, making them visible to
Expand Down
35 changes: 17 additions & 18 deletions Sources/GoogleAI/PartsRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.

import Foundation
#if canImport(AppKit)
import AppKit // For NSImage extensions.
#elseif canImport(UIKit)
#if canImport(UIKit)
import UIKit // For UIImage extensions.
#elseif canImport(AppKit)
import AppKit // For NSImage extensions.
#endif

/// A protocol describing any data that could be interpreted as model input data.
Expand Down Expand Up @@ -46,7 +46,20 @@ extension [any PartsRepresentable]: PartsRepresentable {
}
}

#if canImport(AppKit)
#if canImport(UIKit)
/// Enables images to be representable as ``PartsRepresentable``.
extension UIImage: PartsRepresentable {
public var partsValue: [ModelContent.Part] {
guard let data = jpegData(compressionQuality: 0.8) else {
Logging.default.error("[GoogleGenerativeAI] Couldn't create JPEG from UIImage.")
return []
}

return [ModelContent.Part.data(mimetype: "image/jpeg", data)]
}
}

#elseif canImport(AppKit)
/// Enables images to be representable as ``PartsRepresentable``.
extension NSImage: PartsRepresentable {
public var partsValue: [ModelContent.Part] {
Expand All @@ -63,18 +76,4 @@ extension [any PartsRepresentable]: PartsRepresentable {
return [ModelContent.Part.data(mimetype: "image/jpeg", data)]
}
}

#elseif canImport(UIKit)
/// Enables images to be representable as ``PartsRepresentable``.
extension UIImage: PartsRepresentable {
public var partsValue: [ModelContent.Part] {
guard let data = jpegData(compressionQuality: 0.8) else {
Logging.default.error("[GoogleGenerativeAI] Couldn't create JPEG from UIImage.")
return []
}

return [ModelContent.Part.data(mimetype: "image/jpeg", data)]
}
}

#endif
38 changes: 19 additions & 19 deletions Tests/GoogleAITests/GoogleAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ final class GoogleGenerativeAITests: XCTestCase {
let _ = try await genAI.generateContent(str)
let _ = try await genAI.generateContent([str])
let _ = try await genAI.generateContent(str, "abc", "def")
#if canImport(AppKit)
_ = try await genAI.generateContent(NSImage())
_ = try await genAI.generateContent([NSImage()])
_ = try await genAI.generateContent(str, NSImage(), "def", NSImage())
_ = try await genAI.generateContent([str, NSImage(), "def", NSImage()])
#elseif canImport(UIKit)
#if canImport(UIKit)
_ = try await genAI.generateContent(UIImage())
_ = try await genAI.generateContent([UIImage()])
_ = try await genAI
Expand All @@ -76,6 +71,11 @@ final class GoogleGenerativeAITests: XCTestCase {
_ = try await genAI.generateContent([str, UIImage(), "def", UIImage()])
_ = try await genAI.generateContent([ModelContent("def", UIImage()),
ModelContent("def", UIImage())])
#elseif canImport(AppKit)
_ = try await genAI.generateContent(NSImage())
_ = try await genAI.generateContent([NSImage()])
_ = try await genAI.generateContent(str, NSImage(), "def", NSImage())
_ = try await genAI.generateContent([str, NSImage(), "def", NSImage()])
#endif

// PartsRepresentable combinations.
Expand All @@ -91,7 +91,19 @@ final class GoogleGenerativeAITests: XCTestCase {
mimetype: "foo",
Data()
)] as [any PartsRepresentable])
#if canImport(AppKit)
#if canImport(UIKit)
_ = ModelContent(role: "user", parts: UIImage())
_ = ModelContent(role: "user", parts: [UIImage()])
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot convert
// value of type `[Any]` to expected type `[any PartsRepresentable]`. Not sure if there's a
// way we can get it to work.
_ = ModelContent(parts: [str, UIImage()] as [any PartsRepresentable])
// Alternatively, you can explicitly declare the type in a variable and pass it in.
let representable2: [any PartsRepresentable] = [str, UIImage()]
_ = ModelContent(parts: representable2)
_ = ModelContent(parts: [str, UIImage(),
ModelContent.Part.text(str)] as [any PartsRepresentable])
#elseif canImport(AppKit)
_ = ModelContent(role: "user", parts: NSImage())
_ = ModelContent(role: "user", parts: [NSImage()])
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot convert
Expand All @@ -104,18 +116,6 @@ final class GoogleGenerativeAITests: XCTestCase {
_ =
ModelContent(parts: [str, NSImage(),
ModelContent.Part.text(str)] as [any PartsRepresentable])
#elseif canImport(UIKit)
_ = ModelContent(role: "user", parts: UIImage())
_ = ModelContent(role: "user", parts: [UIImage()])
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot convert
// value of type `[Any]` to expected type `[any PartsRepresentable]`. Not sure if there's a
// way we can get it to work.
_ = ModelContent(parts: [str, UIImage()] as [any PartsRepresentable])
// Alternatively, you can explicitly declare the type in a variable and pass it in.
let representable2: [any PartsRepresentable] = [str, UIImage()]
_ = ModelContent(parts: representable2)
_ = ModelContent(parts: [str, UIImage(),
ModelContent.Part.text(str)] as [any PartsRepresentable])
#endif

// countTokens API
Expand Down

0 comments on commit 5759e7b

Please sign in to comment.