Skip to content

Instantly share code, notes, and snippets.

@lzell
Last active July 31, 2024 05:04
Show Gist options
  • Save lzell/a878b787f24cc0dd87a31f4dceccd092 to your computer and use it in GitHub Desktop.
Save lzell/a878b787f24cc0dd87a31f4dceccd092 to your computer and use it in GitHub Desktop.
StabilityUltra.swift
import Foundation
import SwiftUI
import AIProxy
final actor ImageLoader {
static func create(fromPrompt prompt: String) async -> UIImage? {
let service = AIProxy.stabilityAIService(
partialKey: "your-partial-key",
serviceURL: "your-service-url")
do {
let body = StabilityAIUltraRequestBody(prompt: prompt)
let response = try await service.ultraRequest(body: body)
return UIImage(data: response.imageData)
} catch AIProxyError.unsuccessfulRequest(let statusCode, let responseBody) {
print("Received non-200 status code: \(statusCode) with response body: \(responseBody)")
} catch {
print(error.localizedDescription)
}
return nil
}
}
@Observable
final class ViewModel {
var myImage: UIImage?
init() {
Task {
await self.perform()
}
}
func perform() async {
await self.myImage = ImageLoader.create(fromPrompt: "Lighthouse on a cliff overlooking the ocean")
}
}
struct ContentView: View {
@State var viewModel = ViewModel()
var body: some View {
VStack {
if let image = viewModel.myImage {
Image(uiImage: image).imageScale(.large)
}
}
.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment