Class: Buble::GenerationsResource

Inherits:
Object
  • Object
show all
Defined in:
lib/buble/generations.rb

Constant Summary collapse

FORBIDDEN_FIELDS =
%w[
  input
  options
  scene
  sub_mode_id
  subModeId
  provider
  mediaType
  media_type
  images
  image_input
  video_input
  audio_input
].freeze
TERMINAL_STATUSES =
%w[success failed canceled].freeze

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ GenerationsResource

Returns a new instance of GenerationsResource.



24
25
26
# File 'lib/buble/generations.rb', line 24

def initialize(http)
  @http = http
end

Instance Method Details

#create(model:, mode: nil, prompt: nil, image_urls: nil, start_frame: nil, end_frame: nil, video_urls: nil, audio_urls: nil, is_public: nil, copy_protected: nil, **params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/buble/generations.rb', line 28

def create(model:, mode: nil, prompt: nil, image_urls: nil, start_frame: nil, end_frame: nil,
           video_urls: nil, audio_urls: nil, is_public: nil, copy_protected: nil, **params)
  body = compact_body({
    model: model,
    mode: mode,
    prompt: prompt,
    image_urls: image_urls,
    start_frame: start_frame,
    end_frame: end_frame,
    video_urls: video_urls,
    audio_urls: audio_urls,
    is_public: is_public,
    copy_protected: copy_protected
  }.merge(params))
  assert_public_body!(body)
  @http.request('POST', '/api/v1/generations', body: body)
end

#retrieve(id) ⇒ Object



46
47
48
# File 'lib/buble/generations.rb', line 46

def retrieve(id)
  @http.request('GET', "/api/v1/generations/#{HTTP.encode_segment(id)}")
end

#wait(id, interval: 2, timeout: 600, throw_on_failed: true, throw_on_canceled: true) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/buble/generations.rb', line 50

def wait(id, interval: 2, timeout: 600, throw_on_failed: true, throw_on_canceled: true)
  deadline = Time.now + timeout

  loop do
    envelope = retrieve(id)
    task = envelope['data'] || {}
    status = task['status']
    if TERMINAL_STATUSES.include?(status)
      raise_if_terminal_error!(id, task, status, throw_on_failed, throw_on_canceled)
      return envelope
    end

    if Time.now >= deadline
      raise TimeoutError.new(
        "Generation #{id} did not finish within #{timeout} seconds.",
        timeout: timeout
      )
    end

    sleep interval
  end
end