Class: Zuno::Providers::Replicate

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

Constant Summary collapse

API_BASE_URL =
"https://api.replicate.com/v1".freeze
DEFAULT_TIMEOUT =
120_000

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, timeout: DEFAULT_TIMEOUT) ⇒ Replicate

Returns a new instance of Replicate.

Raises:



1743
1744
1745
1746
1747
1748
# File 'lib/zuno.rb', line 1743

def initialize(api_key: nil, timeout: DEFAULT_TIMEOUT)
  @api_key = api_key
  raise ProviderError, "Replicate API key not configured" if @api_key.nil? || @api_key.to_s.empty?

  @timeout = timeout
end

Instance Method Details

#create_prediction(reference:, input:) ⇒ Object



1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
# File 'lib/zuno.rb', line 1762

def create_prediction(reference:, input:)
  path, payload = build_create_request(reference: reference, input: input)

  response = Typhoeus.post(
    "#{API_BASE_URL}#{path}",
    headers: headers.merge("Prefer" => "wait=#{REPLICATE_PREFER_WAIT_SECONDS}"),
    body: JSON.generate(payload),
    timeout: @timeout
  )
  parse_response(response)
end

#deployment(deployment_id) ⇒ Object



1758
1759
1760
# File 'lib/zuno.rb', line 1758

def deployment(deployment_id)
  model_descriptor(model_id: deployment_id, target: :deployment)
end

#get_prediction(prediction:) ⇒ Object



1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
# File 'lib/zuno.rb', line 1774

def get_prediction(prediction:)
  url = prediction.dig("urls", "get")

  if url.nil? || url.to_s.strip.empty?
    prediction_id = prediction["id"].to_s
    raise ProviderError, "Replicate prediction id is missing" if prediction_id.empty?

    url = "#{API_BASE_URL}/predictions/#{CGI.escape(prediction_id)}"
  end

  response = Typhoeus.get(
    url,
    headers: headers,
    timeout: @timeout
  )
  parse_response(response)
end

#model(model_id) ⇒ Object



1750
1751
1752
# File 'lib/zuno.rb', line 1750

def model(model_id)
  model_descriptor(model_id: model_id, target: :model)
end

#version(version_id) ⇒ Object



1754
1755
1756
# File 'lib/zuno.rb', line 1754

def version(version_id)
  model_descriptor(model_id: version_id, target: :version)
end