Class: StabilitySDK::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stability_sdk/client.rb

Constant Summary collapse

DEFAULT_API_HOST =
"grpc.stability.ai:443"
DEFAULT_IMAGE_WIDTH =
512
DEFAULT_IMAGE_HEIGHT =
512
DEFAULT_SAMPLE_SIZE =
1
DEFAULT_ENGINE_ID =
"stable-diffusion-xl-1024-v1-0"
DEFAULT_CFG_SCALE =
7.0
DEFAULT_START_SCHEDULE =
1.0
DEFAULT_END_SCHEDULE =
0.01
SAMPLER_ALGORITHMS =
{
  "ddim": Gooseai::DiffusionSampler::SAMPLER_DDIM,
  "plms": Gooseai::DiffusionSampler::SAMPLER_DDPM,
  "k_euler": Gooseai::DiffusionSampler::SAMPLER_K_EULER,
  "k_euler_ancestral": Gooseai::DiffusionSampler::SAMPLER_K_EULER_ANCESTRAL,
  "k_heun": Gooseai::DiffusionSampler::SAMPLER_K_HEUN,
  "k_dpm_2": Gooseai::DiffusionSampler::SAMPLER_K_DPM_2,
  "k_dpm_2_ancestral": Gooseai::DiffusionSampler::SAMPLER_K_DPM_2_ANCESTRAL,
  "k_lms": Gooseai::DiffusionSampler::SAMPLER_K_LMS,
  "k_dpmpp_2s_ancestral": Gooseai::DiffusionSampler::SAMPLER_K_DPMPP_2S_ANCESTRAL,
  "k_dpmpp_2m": Gooseai::DiffusionSampler::SAMPLER_K_DPMPP_2M,
  "k_dpmpp_sde": Gooseai::DiffusionSampler::SAMPLER_K_DPMPP_SDE,
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stability_sdk/client.rb', line 30

def initialize(options={})
  host = options[:api_host] || DEFAULT_API_HOST
  channel_creds = options.has_key?(:ca_cert) ? GRPC::Core::ChannelCredentials.new(options[:ca_cert]) : GRPC::Core::ChannelCredentials.new
  call_creds = GRPC::Core::CallCredentials.new(proc { { "authorization" => "Bearer #{options[:api_key]}" } })
  creds = channel_creds.compose(call_creds)

  stub_params = {}
  [:channel_override, :timeout, :propagate_mask, :channel_args, :interceptors].each do |kw|
    stub_params[kw] = options[kw] if options.has_key?(kw)
  end

  if options.has_key?(:logger)
    @logger = options[:logger]
  else
    logger = Logger.new(STDOUT)
    logger.level = Logger::WARN
    @logger = logger
  end

  @stub = Gooseai::GenerationService::Stub.new(host, creds, **stub_params)
end

Instance Method Details

#generate(prompt, options, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/stability_sdk/client.rb', line 52

def generate(prompt, options, &block)
  width = options.has_key?(:width) ? options[:width] : DEFAULT_IMAGE_WIDTH
  height = options.has_key?(:height) ? options[:height] : DEFAULT_IMAGE_HEIGHT

  if width % 64 != 0 || height % 64 != 0
    raise InvalidParameter, "width and height must be a multiple of 64"
  end

  samples = options.has_key?(:num_samples) ? options[:num_samples] : DEFAULT_SAMPLE_SIZE
  steps = options.has_key?(:steps) ? options[:steps] : nil
  seed = options.has_key?(:seed) ? [options[:seed]] : [rand(4294967295)]
  transform = Gooseai::TransformType.new(
    diffusion: options.has_key?(:sampler) ? SAMPLER_ALGORITHMS[options[:sampler].to_sym] : nil,
  )
  step_parameter = Gooseai::StepParameter.new(
    scaled_step: 0,
    sampler: Gooseai::SamplerParameters.new(
      cfg_scale: options.has_key?(:cfg_scale) ? options[:cfg_scale] : DEFAULT_CFG_SCALE,
    ),
  )

  prompt_param = []
  if prompt != ""
    param = Gooseai::Prompt.new(text: prompt)
    param.parameters = Gooseai::PromptParameters.new(weight: options[:prompt_weight]) if options.has_key?(:prompt_weight)
    prompt_param << param
  end
  if options.has_key?(:init_image)
    prompt_param << init_image_to_prompt(options[:init_image], options[:prompt_weight])
    step_parameter.scaled_step = 0
    step_parameter.sampler = Gooseai::SamplerParameters.new(
      cfg_scale: options.has_key?(:cfg_scale) ? options[:cfg_scale] : DEFAULT_CFG_SCALE,
    )
    step_parameter.schedule = Gooseai::ScheduleParameters.new(
      start: options.has_key?(:start_schedule) ? options[:start_schedule] : DEFAULT_START_SCHEDULE,
      end: options.has_key?(:end_schedule) ? options[:end_schedule] : DEFAULT_END_SCHEDULE,
    )
  end
  if options.has_key?(:mask_image)
    prompt_param << mask_image_to_prompt(options[:mask_image])
  end

  # CLIP guidance
  if options.has_key?(:guidance_preset) && options[:guidance_preset] != "GUIDANCE_PRESET_NONE"
    step_parameter.sampler = nil

    guidance_prompt =
      if options.has_key?(:guidance_prompt)
        Gooseai::Prompt.new(text: options[:guidance_prompt])
      else
        Gooseai::Prompt.new(text: prompt)
      end

    guidance_strength = nil
    if options.has_key?(:guidance_strength) && options[:guidance_strength] != 0
      guidance_strength = options[:guidance_strength]
    end

    models = nil
    if options.has_key?(:guidance_models)
      models = options[:guidance_models].map { |m| Gooseai::Model.new(alias: m) }
    end

    cutouts = nil
    if options.has_key?(:guidance_cuts)
      cutouts = Gooseai::CutoutParameters.new(count: options[:guidance_cuts])
    end

    step_parameter.guidance = Gooseai::GuidanceParameters.new(
      guidance_preset: Gooseai::GuidancePreset.const_get(options[:guidance_preset].to_sym),
      instances: [
        Gooseai::GuidanceInstanceParameters.new(
          guidance_strength: guidance_strength,
          models: models,
          cutouts: cutouts,
          prompt: guidance_prompt,
        ),
      ],
    )

    if transform.diffusion != Gooseai::DiffusionSampler::SAMPLER_K_DPM_2_ANCESTRAL && transform.diffusion != Gooseai::DiffusionSampler::SAMPLER_K_EULER_ANCESTRAL
      transform = Gooseai::TransformType.new(
        diffusion: Gooseai::DiffusionSampler::SAMPLER_K_DPM_2_ANCESTRAL
      )
      @logger.warn "CLIP guidance is only supported with ancestral samplers. So override it with SAMPLER_K_DPM_2_ANCESTRAL."
    end
  end

  image_param = Gooseai::ImageParameters.new(
    width: width,
    height: height,
    samples: samples,
    steps: steps,
    seed: seed,
    transform: transform,
    parameters: [step_parameter],
  )

  req = Gooseai::Request.new(
    prompt: prompt_param,
    engine_id: options[:engine_id] || DEFAULT_ENGINE_ID,
    image: image_param
  )

  @logger.debug "sending request."
  start = Time.now
  @stub.generate(req).each do |answer|
    duration = Time.now - start
    if answer.artifacts.size > 0
      artifact_types = answer.artifacts.map { |a| a.type }
      @logger.debug "got #{answer.answer_id} with #{artifact_types} in #{duration.round(2)}s"
    else
      @logger.debug "got keepalive #{answer.answer_id} in #{duration.round(2)}s"
    end

    block.call(answer)

    start = Time.now
  end
end

#init_image_to_prompt(path, weight) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/stability_sdk/client.rb', line 173

def init_image_to_prompt(path, weight)
  bin = IO.binread(path)
  prompt = Gooseai::Prompt.new(
    artifact: Gooseai::Artifact.new(
      type: Gooseai::ArtifactType::ARTIFACT_IMAGE,
      binary: bin,
    )
  )
  unless weight.nil?
    prompt.parameters = Gooseai::PromptParameters.new(weight: weight)
  end
  return prompt
end

#mask_image_to_prompt(path) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/stability_sdk/client.rb', line 187

def mask_image_to_prompt(path)
  bin = IO.binread(path)
  return Gooseai::Prompt.new(
    artifact: Gooseai::Artifact.new(
      type: Gooseai::ArtifactType::ARTIFACT_MASK,
      binary: bin,
    ),
  )
end