|
| 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 FirebaseAppCheckInterop |
| 16 | +import FirebaseAuthInterop |
| 17 | +import Foundation |
| 18 | + |
| 19 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 20 | +public final class ImagenModel { |
| 21 | + /// The resource name of the model in the backend; has the format "models/model-name". |
| 22 | + let modelResourceName: String |
| 23 | + |
| 24 | + /// The backing service responsible for sending and receiving model requests to the backend. |
| 25 | + let generativeAIService: GenerativeAIService |
| 26 | + |
| 27 | + /// Configuration parameters for sending requests to the backend. |
| 28 | + let requestOptions: RequestOptions |
| 29 | + |
| 30 | + init(name: String, |
| 31 | + projectID: String, |
| 32 | + apiKey: String, |
| 33 | + requestOptions: RequestOptions, |
| 34 | + appCheck: AppCheckInterop?, |
| 35 | + auth: AuthInterop?, |
| 36 | + urlSession: URLSession = .shared) { |
| 37 | + modelResourceName = name |
| 38 | + generativeAIService = GenerativeAIService( |
| 39 | + projectID: projectID, |
| 40 | + apiKey: apiKey, |
| 41 | + appCheck: appCheck, |
| 42 | + auth: auth, |
| 43 | + urlSession: urlSession |
| 44 | + ) |
| 45 | + self.requestOptions = requestOptions |
| 46 | + } |
| 47 | + |
| 48 | + public func generateImages(prompt: String) async throws |
| 49 | + -> ImageGenerationResponse<ImagenInlineDataImage> { |
| 50 | + return try await generateImages( |
| 51 | + prompt: prompt, |
| 52 | + parameters: imageGenerationParameters(storageURI: nil) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + public func generateImages(prompt: String, storageURI: String) async throws |
| 57 | + -> ImageGenerationResponse<ImagenFileDataImage> { |
| 58 | + return try await generateImages( |
| 59 | + prompt: prompt, |
| 60 | + parameters: imageGenerationParameters(storageURI: storageURI) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + func generateImages<T: Decodable>(prompt: String, |
| 65 | + parameters: ImageGenerationParameters) async throws |
| 66 | + -> ImageGenerationResponse<T> { |
| 67 | + let request = ImageGenerationRequest<T>( |
| 68 | + model: modelResourceName, |
| 69 | + options: requestOptions, |
| 70 | + instances: [ImageGenerationInstance(prompt: prompt)], |
| 71 | + parameters: parameters |
| 72 | + ) |
| 73 | + |
| 74 | + return try await generativeAIService.loadRequest(request: request) |
| 75 | + } |
| 76 | + |
| 77 | + func imageGenerationParameters(storageURI: String?) -> ImageGenerationParameters { |
| 78 | + // TODO(#14221): Add support for configuring these parameters. |
| 79 | + return ImageGenerationParameters( |
| 80 | + sampleCount: 1, |
| 81 | + storageURI: storageURI, |
| 82 | + seed: nil, |
| 83 | + negativePrompt: nil, |
| 84 | + aspectRatio: nil, |
| 85 | + safetyFilterLevel: nil, |
| 86 | + personGeneration: nil, |
| 87 | + outputOptions: nil, |
| 88 | + addWatermark: nil, |
| 89 | + includeResponsibleAIFilterReason: true |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
0 commit comments