Class: GenAI::Image::StabilityAI

Inherits:
Base
  • Object
show all
Defined in:
lib/gen_ai/image/stability_ai.rb

Constant Summary collapse

DEFAULT_SIZE =
'256x256'
API_BASE_URL =
'https://api.stability.ai'
DEFAULT_MODEL =
'stable-diffusion-xl-beta-v2-2-2'
UPSCALE_MODEL =
'stable-diffusion-x4-latent-upscaler'

Instance Method Summary collapse

Methods inherited from Base

#variations

Methods included from Dependency

#depends_on

Constructor Details

#initialize(token:, options: {}) ⇒ StabilityAI

Returns a new instance of StabilityAI.



13
14
15
# File 'lib/gen_ai/image/stability_ai.rb', line 13

def initialize(token:, options: {})
  build_client(token)
end

Instance Method Details

#edit(image, prompt, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gen_ai/image/stability_ai.rb', line 30

def edit(image, prompt, options = {})
  model = options[:model] || DEFAULT_MODEL
  url = "/v1/generation/#{model}/image-to-image"

  response = client.post url, build_edit_body(image, prompt, options), multipart: true

  build_result(
    raw: response,
    model: model,
    parsed: parse_response_data(response['artifacts'])
  )
end

#generate(prompt, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gen_ai/image/stability_ai.rb', line 17

def generate(prompt, options = {})
  model = options[:model] || DEFAULT_MODEL
  url = "/v1/generation/#{model}/text-to-image"

  response = client.post url, build_generation_body(prompt, options)

  build_result(
    raw: response,
    model: model,
    parsed: parse_response_data(response['artifacts'])
  )
end

#upscale(image, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gen_ai/image/stability_ai.rb', line 43

def upscale(image, options = {})
  model = options[:model] || UPSCALE_MODEL
  url = "/v1/generation/#{model}/image-to-image/upscale"

  response = client.post url, build_upscale_body(image, options), multipart: true

  build_result(
    raw: response,
    model: model,
    parsed: parse_response_data(response['artifacts'])
  )
end