Last active
July 31, 2024 05:04
-
-
Save lzell/a878b787f24cc0dd87a31f4dceccd092 to your computer and use it in GitHub Desktop.
StabilityUltra.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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