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