TemplateImagenModel.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2025 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. import Foundation
  15. /// A type that represents a remote image generation model (like Imagen), with the ability to
  16. /// generate
  17. /// images based on various input types.
  18. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  19. public final class TemplateImagenModel: Sendable {
  20. let generativeAIService: GenerativeAIService
  21. let apiConfig: APIConfig
  22. init(generativeAIService: GenerativeAIService, apiConfig: APIConfig) {
  23. self.generativeAIService = generativeAIService
  24. self.apiConfig = apiConfig
  25. }
  26. /// Generates images from a prompt template and variables.
  27. ///
  28. /// - Parameters:
  29. /// - template: The prompt template to use.
  30. /// - variables: A dictionary of variables to substitute into the template.
  31. /// - Returns: The images generated by the model.
  32. /// - Throws: An error if the request failed.
  33. public func generateImages(template: String,
  34. variables: [String: Any],
  35. options: RequestOptions = RequestOptions()) async throws
  36. -> GenerateImagesResponse {
  37. let templateVariables = try variables.mapValues { try TemplateVariable(value: $0) }
  38. let projectID = generativeAIService.firebaseInfo.projectID
  39. let request = GenerateImagesRequest(
  40. template: template,
  41. variables: templateVariables,
  42. projectID: projectID,
  43. apiConfig: apiConfig,
  44. options: options
  45. )
  46. let response: GenerateImagesResponse = try await generativeAIService
  47. .loadRequest(request: request)
  48. return response
  49. }
  50. }
  51. // A placeholder for the response from an image generation request.
  52. public struct GenerateImagesResponse: Decodable, @unchecked Sendable {
  53. public let images: [Data]
  54. enum CodingKeys: String, CodingKey {
  55. case predictions
  56. }
  57. struct Prediction: Decodable {
  58. let images: [String]
  59. }
  60. public init(from decoder: Decoder) throws {
  61. let container = try decoder.container(keyedBy: CodingKeys.self)
  62. let predictions = try container.decode([Prediction].self, forKey: .predictions)
  63. guard let firstPrediction = predictions.first else {
  64. images = []
  65. return
  66. }
  67. images = firstPrediction.images.compactMap { Data(base64Encoded: $0) }
  68. }
  69. }