stability_sdk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dac029de10ccef05e11d7fee6a581f14fec50252e1cf7790ce2e849e5061489c
4
- data.tar.gz: 66f8803ff804dae6971cdf3b8473225012255e87d67f00484583d65ecd8afb6a
3
+ metadata.gz: 2b707c869873f4d9ff68de1d281a70b498fb9d0509f83e6e3b3550e8f1d86bff
4
+ data.tar.gz: 04454b501ae8d8d4ed62140812b2f0482e9178fb54a7d4fd42b1e7bf2817f1fb
5
5
  SHA512:
6
- metadata.gz: ad988347184eafe784ab654b0022f1742cf56e51c9e2d4d88b3507f97162c6aaa7a165ad8ee5a6d3c34c1a9427fc860a54679192235c154bfea3008765dd8f89
7
- data.tar.gz: d4522e6b2644f0ef309dbfb862a0667ed80c77e93c30bc575764997aa511d73789b3f270641d44c83cc2fdc017b7a0d5562e7de79ed7bebf86ff894019ad2642
6
+ metadata.gz: efef7f4abc46b1664a4545514598c888472e7828a67d9ca93f5597c6cf3f1709cbc58bd1508936696626985689762b8d9100f773b1175f1a38fde8e507961df0
7
+ data.tar.gz: ca8e9fbc0ab70bb2f05b5171985c8159a4c423fe32c6a1903c42548366c1d1d17c40a172c85921e884291c64ef34e398bfda8f7e8e08af1632894504e292ede8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stability_sdk (0.1.0)
4
+ stability_sdk (0.2.0)
5
5
  grpc
6
6
  mime-types
7
7
 
data/README.md CHANGED
@@ -36,6 +36,12 @@ This command saves an image like this:
36
36
 
37
37
  ![3749380973_A_night_in_winter__oil_on_canvas_landscape_painting__by_Vincent_van_Gogh](https://user-images.githubusercontent.com/25668/188884116-0b03494b-0b34-49de-bbbc-89fbc2f6029d.png)
38
38
 
39
+ img2img example:
40
+
41
+ ```sh
42
+ STABILITY_SDK_API_KEY=YOUR_API_KEY stability-client --init_image=/path/to/image.png --mask_image=/path/to/mask.png 'your prompt'
43
+ ```
44
+
39
45
 
40
46
  ```sh
41
47
  Usage: stability-client [options] YOUR_PROMPT_TEXT
@@ -52,6 +58,10 @@ Options:
52
58
  --no-store do not write out artifacts
53
59
  -n, --num_samples=VAL number of samples to generate
54
60
  -e, --engine=VAL engine to use for inference. default `stable-diffusion-v1`
61
+ -i, --init_image=VAL path to init image
62
+ -m, --mask_image=VAL path to mask image
63
+ --start_schedule=VAL start schedule for init image (must be greater than 0, 1 is full strength text prompt, no trace of image). default 1.0
64
+ --end_schedule=VAL end schedule for init image. default 0.01
55
65
  -v, --verbose
56
66
  ```
57
67
 
@@ -67,6 +77,10 @@ client = StabilitySDK::Client.new(api_key: "YOUR_API_KEY")
67
77
  prompt = "your prompot here"
68
78
  options = {}
69
79
 
80
+ # for the case of img2img:
81
+ # options[:init_image] = "/path/to/image.png"
82
+ # options[:mask_image] = "/path/to/mask.png"
83
+
70
84
  client.generate(prompt, options) do |answer|
71
85
  answer.artifacts.each do |artifact|
72
86
  if artifact.type == :ARTIFACT_IMAGE
data/exe/stability-client CHANGED
@@ -27,14 +27,20 @@ opt.on("-p", "--prefix=VAL", "output prefixes for artifacts. default `generation
27
27
  opt.on("--no-store", "do not write out artifacts") {|v| options[:no_store] = v }
28
28
  opt.on("-n", "--num_samples=VAL", "number of samples to generate") {|v| options[:num_samples] = v }
29
29
  opt.on("-e", "--engine=VAL", "engine to use for inference. default `stable-diffusion-v1`") {|v| options[:engine_id] = v }
30
+ opt.on("-i", "--init_image=VAL", "path to init image") {|v| options[:init_image] = v }
31
+ opt.on("-m", "--mask_image=VAL", "path to mask image") {|v| options[:mask_image] = v }
32
+ opt.on("--start_schedule=VAL", "start schedule for init image (must be greater than 0, 1 is full strength text prompt, no trace of image). default 1.0") {|v| options[:start_schedule] = v }
33
+ opt.on("--end_schedule=VAL", "end schedule for init image. default 0.01") {|v| options[:end_schedule] = v }
30
34
  opt.on("-v", "--verbose") { logger.level = Logger::DEBUG }
31
35
  opt.parse!(ARGV)
32
36
 
33
37
  prompt = ARGV.join(" ")
34
- raise StabilitySDK::InsufficientParameter, "prompt is required" if prompt.nil? || prompt == ""
38
+ raise StabilitySDK::InsufficientParameter, "prompt and/or init_image is required" if (prompt.nil? || prompt == "") && !options.has_key?(:init_image)
35
39
 
36
40
  options[:api_key] = ENV["STABILITY_SDK_API_KEY"] if ENV["STABILITY_SDK_API_KEY"]
37
- raise StabilitySDK::InsufficientParameter, "api key is required" if !options.has_key?(:api_key) || options[:api_key] == ""
41
+ raise StabilitySDK::InsufficientParameter, "api key is required" if !options.has_key?(:api_key)
42
+
43
+ raise StabilitySDK::InsufficientParameter, "init_image is required if mask_image is provided" if options.has_key?(:mask_image) && !options.has_key?(:init_image)
38
44
 
39
45
  client = StabilitySDK::Client.new(api_key: options[:api_key])
40
46
 
data/lib/generation_pb.rb CHANGED
@@ -64,9 +64,14 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
64
64
  add_message "gooseai.ConditionerParameters" do
65
65
  proto3_optional :vector_adjust_prior, :string, 1
66
66
  end
67
+ add_message "gooseai.ScheduleParameters" do
68
+ optional :start, :float, 1
69
+ optional :end, :float, 2
70
+ end
67
71
  add_message "gooseai.StepParameter" do
68
72
  optional :scaled_step, :float, 1
69
73
  proto3_optional :sampler, :message, 2, "gooseai.SamplerParameters"
74
+ proto3_optional :schedule, :message, 3, "gooseai.ScheduleParameters"
70
75
  end
71
76
  add_message "gooseai.TransformType" do
72
77
  oneof :type do
@@ -125,6 +130,7 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
125
130
  value :ARTIFACT_TOKENS, 4
126
131
  value :ARTIFACT_EMBEDDING, 5
127
132
  value :ARTIFACT_CLASSIFICATIONS, 6
133
+ value :ARTIFACT_MASK, 7
128
134
  end
129
135
  add_enum "gooseai.DiffusionSampler" do
130
136
  value :SAMPLER_DDIM, 0
@@ -166,6 +172,7 @@ module Gooseai
166
172
  Answer = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.Answer").msgclass
167
173
  SamplerParameters = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.SamplerParameters").msgclass
168
174
  ConditionerParameters = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.ConditionerParameters").msgclass
175
+ ScheduleParameters = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.ScheduleParameters").msgclass
169
176
  StepParameter = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.StepParameter").msgclass
170
177
  TransformType = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.TransformType").msgclass
171
178
  ImageParameters = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("gooseai.ImageParameters").msgclass
@@ -8,9 +8,11 @@ module StabilitySDK
8
8
  DEFAULT_IMAGE_HEIGHT = 512
9
9
  DEFAULT_SAMPLE_SIZE = 1
10
10
  DEFAULT_STEPS = 50
11
- DEFAULT_ENGINE_ID = "stable-diffusion-v1"
11
+ DEFAULT_ENGINE_ID = "stable-diffusion-v1-5"
12
12
  DEFAULT_CFG_SCALE = 7.0
13
13
  DEFAULT_SAMPLER_ALGORITHM = Gooseai::DiffusionSampler::SAMPLER_K_LMS
14
+ DEFAULT_START_SCHEDULE = 1.0
15
+ DEFAULT_END_SCHEDULE = 0.01
14
16
 
15
17
  sampler_algorithms = {
16
18
  "ddim": Gooseai::DiffusionSampler::SAMPLER_DDIM,
@@ -33,21 +35,8 @@ module StabilitySDK
33
35
  end
34
36
 
35
37
  def generate(prompt, options, &block)
36
- image_param = image_param(options)
37
- req = Gooseai::Request.new(
38
- prompt: [Gooseai::Prompt.new(text: prompt)],
39
- engine_id: options[:engine_id] || DEFAULT_ENGINE_ID,
40
- image: image_param
41
- )
42
-
43
- @stub.generate(req).each do |answer|
44
- block.call(answer)
45
- end
46
- end
47
-
48
- def image_param(options={})
49
38
  width = options.has_key?(:width) ? options[:width].to_i : DEFAULT_IMAGE_WIDTH
50
- height = options.has_key?(:height) ? options[:height] : DEFAULT_IMAGE_HEIGHT
39
+ height = options.has_key?(:height) ? options[:height].to_i : DEFAULT_IMAGE_HEIGHT
51
40
  samples = options.has_key?(:num_samples) ? [:num_samples].to_i : DEFAULT_SAMPLE_SIZE
52
41
  steps = options.has_key?(:steps) ? options[:steps].to_i : DEFAULT_STEPS
53
42
  seed = options.has_key?(:seed) ? [options[:seed]] : [rand(4294967295)]
@@ -61,7 +50,28 @@ module StabilitySDK
61
50
  ),
62
51
  )]
63
52
 
64
- return Gooseai::ImageParameters.new(
53
+ prompt_param = []
54
+ if prompt != ""
55
+ prompt_param << Gooseai::Prompt.new(text: prompt)
56
+ end
57
+ if options.has_key?(:init_image)
58
+ prompt_param << init_image_to_prompt(options[:init_image])
59
+ parameters = [Gooseai::StepParameter.new(
60
+ scaled_step: 0,
61
+ sampler: Gooseai::SamplerParameters.new(
62
+ cfg_scale: options.has_key?(:cfg_scale) ? options[:cfg_scale].to_f : DEFAULT_CFG_SCALE,
63
+ ),
64
+ schedule: Gooseai::ScheduleParameters.new(
65
+ start: options.has_key?(:start_schedule) ? options[:start_schedule].to_f : DEFAULT_START_SCHEDULE,
66
+ end: options.has_key?(:end_schedule) ? options[:end_schedule].to_f : DEFAULT_END_SCHEDULE,
67
+ ),
68
+ )]
69
+ end
70
+ if options.has_key?(:mask_image)
71
+ prompt_param << mask_image_to_prompt(options[:mask_image])
72
+ end
73
+
74
+ image_param = Gooseai::ImageParameters.new(
65
75
  width: width,
66
76
  height: height,
67
77
  samples: samples,
@@ -70,6 +80,39 @@ module StabilitySDK
70
80
  transform: transform,
71
81
  parameters: parameters,
72
82
  )
83
+
84
+ req = Gooseai::Request.new(
85
+ prompt: prompt_param,
86
+ engine_id: options[:engine_id] || DEFAULT_ENGINE_ID,
87
+ image: image_param
88
+ )
89
+
90
+ @stub.generate(req).each do |answer|
91
+ block.call(answer)
92
+ end
93
+ end
94
+
95
+ def init_image_to_prompt(path)
96
+ bin = IO.binread(path)
97
+ return Gooseai::Prompt.new(
98
+ artifact: Gooseai::Artifact.new(
99
+ type: Gooseai::ArtifactType::ARTIFACT_IMAGE,
100
+ binary: bin,
101
+ ),
102
+ parameters: Gooseai::PromptParameters.new(
103
+ init: true
104
+ ),
105
+ )
106
+ end
107
+
108
+ def mask_image_to_prompt(path)
109
+ bin = IO.binread(path)
110
+ return Gooseai::Prompt.new(
111
+ artifact: Gooseai::Artifact.new(
112
+ type: Gooseai::ArtifactType::ARTIFACT_MASK,
113
+ binary: bin,
114
+ ),
115
+ )
73
116
  end
74
117
  end
75
118
  end
@@ -1,3 +1,3 @@
1
1
  module StabilitySDK
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -19,6 +19,7 @@ enum ArtifactType {
19
19
  ARTIFACT_TOKENS = 4;
20
20
  ARTIFACT_EMBEDDING = 5;
21
21
  ARTIFACT_CLASSIFICATIONS = 6;
22
+ ARTIFACT_MASK = 7;
22
23
  }
23
24
 
24
25
  message Token {
@@ -106,9 +107,15 @@ enum Upscaler {
106
107
  UPSCALER_ESRGAN = 2;
107
108
  }
108
109
 
110
+ message ScheduleParameters {
111
+ float start = 1;
112
+ float end = 2;
113
+ }
114
+
109
115
  message StepParameter {
110
116
  float scaled_step = 1;
111
117
  optional SamplerParameters sampler = 2;
118
+ optional ScheduleParameters schedule = 3;
112
119
  }
113
120
 
114
121
  message TransformType {
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.email = ["cou929@gmail.com"]
8
8
 
9
9
  spec.summary = "Ruby client for interacting with stability.ai APIs (e.g. stable diffusion inference)"
10
- spec.description = "Ruby client of https://github.com/Stability-AI/stability-sdk"
10
+ spec.description = "Interacting with stability.ai APIs (e.g. stable diffusion inference). Ruby client of https://github.com/Stability-AI/stability-sdk ."
11
11
  spec.homepage = "https://github.com/cou929/stability-sdk-ruby"
12
12
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
13
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stability_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kosei Moriyama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-07 00:00:00.000000000 Z
11
+ date: 2022-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc
@@ -52,7 +52,8 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Ruby client of https://github.com/Stability-AI/stability-sdk
55
+ description: Interacting with stability.ai APIs (e.g. stable diffusion inference).
56
+ Ruby client of https://github.com/Stability-AI/stability-sdk .
56
57
  email:
57
58
  - cou929@gmail.com
58
59
  executables: