Class: LeoAndRuby::Client

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

Constant Summary collapse

API_BASE_URL =
"https://cloud.leonardo.ai/api/rest/v1"
DEFAULT_PHOTO_REAL_MODEL_ID =
"aa77f04e-3eec-4034-9c07-d0f619684628"

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



12
13
14
# File 'lib/leoandruby/client.rb', line 12

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#generate_image(prompt:, height:, width:, model_id: nil, negative_prompt: nil, alchemy: true, preset_style: "DYNAMIC", photo_real: true, photo_real_version: "v2", num_images: 1) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/leoandruby/client.rb', line 16

def generate_image(
  prompt:, height:, width:,
  model_id: nil,
  negative_prompt: nil,
  alchemy: true,
  preset_style: "DYNAMIC",
  photo_real: true,
  photo_real_version: "v2",
  num_images: 1
)
  raise ArgumentError, "Prompt must be provided" if prompt.nil? || prompt.strip.empty?
  raise ArgumentError, "Height must be provided" if height.nil?
  raise ArgumentError, "Width must be provided" if width.nil?

  model_id ||= DEFAULT_PHOTO_REAL_MODEL_ID

  uri = URI("#{API_BASE_URL}/generations")
  request = Net::HTTP::Post.new(uri)
  request["Accept"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}"
  request["Content-Type"] = "application/json"

  body = {
    prompt: prompt,
    modelId: model_id,
    height: height,
    width: width,
    num_images: num_images,
    alchemy: alchemy,
    presetStyle: preset_style,
    photoReal: photo_real,
    photoRealVersion: photo_real_version
  }

  body[:negative_prompt] = negative_prompt unless negative_prompt.nil?

  request.body = body.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  handle_response(response)
end

#get_generation(generation_id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/leoandruby/client.rb', line 61

def get_generation(generation_id)
  uri = URI("#{API_BASE_URL}/generations/#{generation_id}")
  request = Net::HTTP::Get.new(uri)
  request["Accept"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}"

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  handle_response(response)
end

#list_modelsObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/leoandruby/client.rb', line 74

def list_models
  uri = URI("#{API_BASE_URL}/platformModels")
  request = Net::HTTP::Get.new(uri)
  request["Accept"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}"

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  handle_response(response)
end