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
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
|