Skip to content

Commit

Permalink
Remove model code generation (replicate#25)
Browse files Browse the repository at this point in the history
* Remove generate-replicate-model target

Remove unused dependencies

* Update README
  • Loading branch information
mattt committed Apr 12, 2023
1 parent e165662 commit b88b1b2
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 597 deletions.
63 changes: 0 additions & 63 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,6 @@
"revision" : "56901f2af3625b38924d488b612e95fe8846ee9b",
"version" : "0.6.6"
}
},
{
"identity" : "openapikit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattt/OpenAPIKit",
"state" : {
"revision" : "ebd4e93d22ccc0404b4e431f19987b65523849d2",
"version" : "2.5.1-fork.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "9f39744e025c7d377987f30b03770805dcb0bcd1",
"version" : "1.1.4"
}
},
{
"identity" : "swift-format",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-format.git",
"state" : {
"revision" : "3dd9b517b9e9846435aa782d769ef5825e7c2d65",
"version" : "0.50700.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "04d4497be6b88e524a71778d828790e9589ae1c4",
"version" : "0.50700.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "836bc4557b74fe6d2660218d56e3ce96aff76574",
"version" : "1.1.1"
}
},
{
"identity" : "swift-tools-support-core",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-tools-support-core.git",
"state" : {
"revision" : "4f07be3dc201f6e2ee85b6942d0c220a16926811",
"version" : "0.2.7"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "9ff1cc9327586db4e0c8f46f064b6a82ec1566fa",
"version" : "4.0.6"
}
}
],
"version" : 2
Expand Down
16 changes: 0 additions & 16 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ let package = Package(
.library(
name: "Replicate",
targets: ["Replicate"]),
.executable(
name: "generate-replicate-model",
targets: ["generate-replicate-model"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Flight-School/AnyCodable", .upToNextMinor(from: "0.6.6")),
.package(url: "https://github.com/mattt/OpenAPIKit", exact: "2.5.1-fork.1"),
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "1.1.4")),
.package(url: "https://github.com/apple/swift-syntax.git", exact: "0.50700.00"),
.package(url: "https://github.com/apple/swift-format.git", exact: "0.50700.00"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -35,15 +28,6 @@ let package = Package(
.product(name: "AnyCodable", package: "AnyCodable"),
]
),
.executableTarget(
name: "generate-replicate-model",
dependencies: [
"Replicate",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftFormat", package: "swift-format"),
.product(name: "OpenAPIKit", package: "OpenAPIKit")
]),
.testTarget(
name: "ReplicateTests",
dependencies: [
Expand Down
147 changes: 0 additions & 147 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,153 +118,6 @@ repeat {
} while predictions.count < limit && cursor != nil
```

### Swift code generation for Replicate models

When working with models that are known ahead of time,
you can use the provided `generate-replicate-model` tool
to generate Swift code with fully-typed inputs and outputs.

For example,
the following command generates code for the latest version of the
[stability-ai/stable-diffusion](https://replicate.com/stability-ai/stable-diffusion) model
([a9758cbfbd5f](https://replicate.com/stability-ai/stable-diffusion/versions/a9758cbfbd5f3c2094457d996681af52552901775aa2d6dd0b17fd15df959bef)):

```console
$ REPLICATE_API_TOKEN=<...> \
swift run generate-replicate-model stability-ai/stable-diffusion
```

```swift
import AnyCodable
import Foundation
import Replicate

/// A latent text-to-image diffusion model capable of generating photo-realistic images given any text input
public enum StableDiffusion: Predictable {

/// The model ID.
public static let modelID = "stability-ai/stable-diffusion"

/// The model version ID.
public static let versionID = "a9758cbfbd5f3c2094457d996681af52552901775aa2d6dd0b17fd15df959bef"

/// The model input.
public struct Input: Codable {
/// Input prompt
public var prompt: String?

/// Width of output image. Maximum size is 1024x768 or 768x1024 because of memory limits
public var width: AnyCodable?

/// Height of output image. Maximum size is 1024x768 or 768x1024 because of memory limits
public var height: AnyCodable?

/// Inital image to generate variations of. Will be resized to the specified width and height
public var initImage: URL?

/// Black and white image to use as mask for inpainting over init_image. Black pixels are inpainted and white pixels are preserved. Experimental feature, tends to work better with prompt strength of 0.5-0.7
public var mask: URL?

/// Prompt strength when using init image. 1.0 corresponds to full destruction of information in init image
public var promptStrength: Double?

/// Number of images to output
public var numOutputs: AnyCodable?

/// Number of denoising steps
public var numInferenceSteps: Int?

/// Scale for classifier-free guidance
public var guidanceScale: Double?

/// Random seed. Leave blank to randomize the seed
public var seed: Int?

/// Creates a new Input.
/// - Parameters:
/// - prompt: Input prompt
/// - width: Width of output image. Maximum size is 1024x768 or 768x1024 because of memory limits
/// - height: Height of output image. Maximum size is 1024x768 or 768x1024 because of memory limits
/// - initImage: Inital image to generate variations of. Will be resized to the specified width and height
/// - mask: Black and white image to use as mask for inpainting over init_image. Black pixels are inpainted and white pixels are preserved. Experimental feature, tends to work better with prompt strength of 0.5-0.7
/// - promptStrength: Prompt strength when using init image. 1.0 corresponds to full destruction of information in init image
/// - numOutputs: Number of images to output
/// - numInferenceSteps: Number of denoising steps
/// - guidanceScale: Scale for classifier-free guidance
/// - seed: Random seed. Leave blank to randomize the seed
public init(
prompt: String? = "",
width: AnyCodable? = 512,
height: AnyCodable? = 512,
initImage: URL? = nil,
mask: URL? = nil,
promptStrength: Double? = 0.8,
numOutputs: AnyCodable? = 1,
numInferenceSteps: Int? = 50,
guidanceScale: Double? = 7.5,
seed: Int? = nil
) {
self.prompt = prompt
self.width = width
self.height = height
self.initImage = initImage
self.mask = mask
self.promptStrength = promptStrength
self.numOutputs = numOutputs
self.numInferenceSteps = numInferenceSteps
self.guidanceScale = guidanceScale
self.seed = seed
}

private enum CodingKeys: String, CodingKey {
case prompt = "prompt"
case width = "width"
case height = "height"
case initImage = "init_image"
case mask = "mask"
case promptStrength = "prompt_strength"
case numOutputs = "num_outputs"
case numInferenceSteps = "num_inference_steps"
case guidanceScale = "guidance_scale"
case seed = "seed"
}
}

/// The model output.
public typealias Output = [URL]
}
```

By adding this code to your project,
you can now create predictions for the model with fully type-checked Swift code:

```swift
// `StableDiffusion.Input` is a struct with several properties
// which can be set through the initializer or property accessors.
var input = StableDiffusion.Input(prompt: "multicolor hyperspace")
input.numOutputs = 4

let prediction = try await StableDiffusion.predict(with: replicate, input: input)

// `StableDiffusion.Output` is a typealias for `[URL]`
for url in prediction.output ?? [] {
print(url.absoluteString)
}
```

[Cog](https://github.com/replicate/cog) models hosted on Replicate
describe their inputs and outputs using [OpenAPI](https://www.openapis.org/).
The `generate-replicate-model` parses this description
with the [`OpenAPIKit` package](https://github.com/mattpolzin/OpenAPIKit)
and generates code using [`SwiftSyntax`](https://github.com/apple/swift-syntax).

> **Note**
>
> The `Replicate` library has only one external dependency on
> the [`AnyCodable` package](https://github.com/Flight-School/AnyCodable),
> so including `Replicate` in your project won't add any other packages
> as runtime dependencies.
## Adding `Replicate` as a Dependency

To use the `Replicate` library in a Swift project,
Expand Down
Loading

0 comments on commit b88b1b2

Please sign in to comment.