Class: ReplicateClient::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/replicate-client/deployment.rb

Constant Summary collapse

INDEX_PATH =
"/deployments"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: ReplicateClient.client) ⇒ ReplicateClient::Deployment

Initialize a new deployment instance.

Parameters:

  • attributes (Hash)

    The attributes of the deployment.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.



152
153
154
155
# File 'lib/replicate-client/deployment.rb', line 152

def initialize(attributes, client: ReplicateClient.client)
  @client = client
  reset_attributes(attributes)
end

Instance Attribute Details

#clientReplicateClient::Client

The client used to make API requests for this deployment.



144
145
146
# File 'lib/replicate-client/deployment.rb', line 144

def client
  @client
end

#current_releaseObject

Attributes for deployment.



139
140
141
# File 'lib/replicate-client/deployment.rb', line 139

def current_release
  @current_release
end

#nameObject

Attributes for deployment.



139
140
141
# File 'lib/replicate-client/deployment.rb', line 139

def name
  @name
end

#ownerObject

Attributes for deployment.



139
140
141
# File 'lib/replicate-client/deployment.rb', line 139

def owner
  @owner
end

Class Method Details

.auto_paging_each(client: ReplicateClient.client) {|ReplicateClient::Deployment| ... } ⇒ void

This method returns an undefined value.

List all deployments.

Parameters:

Yields:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/replicate-client/deployment.rb', line 15

def auto_paging_each(client: ReplicateClient.client, &block)
  cursor = nil

  loop do
    url_params = cursor ? "?cursor=#{cursor}" : ""
    attributes = client.get("#{INDEX_PATH}#{url_params}")

    deployments = attributes["results"].map { |deployment| new(deployment, client: client) }

    deployments.each(&block)

    cursor = attributes["next"] ? URI.decode_www_form(URI.parse(attributes["next"]).query).to_h["cursor"] : nil
    break if cursor.nil?
  end
end

.build_path(owner:, name:) ⇒ String

Build the path for a specific deployment.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

Returns:

  • (String)


123
124
125
# File 'lib/replicate-client/deployment.rb', line 123

def build_path(owner:, name:)
  "#{INDEX_PATH}/#{owner}/#{name}"
end

.create!(name:, model:, hardware:, min_instances:, max_instances:, version_id: nil, client: ReplicateClient.client) ⇒ ReplicateClient::Deployment

Create a new deployment.

Parameters:

  • name (String)

    The name of the deployment.

  • model (ReplicateClient::Model, String)

    The model identifier in “owner/name” format.

  • version_id (String, nil) (defaults to: nil)

    The version ID of the model.

  • hardware (ReplicateClient::Hardware, String)

    The hardware SKU.

  • min_instances (Integer)

    The minimum number of instances.

  • max_instances (Integer)

    The maximum number of instances.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/replicate-client/deployment.rb', line 42

def create!(name:, model:, hardware:, min_instances:, max_instances:, version_id: nil,
            client: ReplicateClient.client)
  model_full_name = model.is_a?(Model) ? model.full_name : model
  hardware_sku = hardware.is_a?(Hardware) ? hardware.sku : hardware
  version = if version_id
              version_id
            elsif model.is_a?(Model)
              model.version_id
            else
              Model.find(model, client: client).latest_version.id
            end

  body = {
    name: name,
    model: model_full_name,
    version: version,
    hardware: hardware_sku,
    min_instances: min_instances,
    max_instances: max_instances
  }

  attributes = client.post(INDEX_PATH, body)
  new(attributes, client: client)
end

.destroy!(owner:, name:, client: ReplicateClient.client) ⇒ void

This method returns an undefined value.

Delete a deployment.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.



112
113
114
115
# File 'lib/replicate-client/deployment.rb', line 112

def destroy!(owner:, name:, client: ReplicateClient.client)
  path = build_path(owner: owner, name: name)
  client.delete(path)
end

.find(full_name, client: ReplicateClient.client) ⇒ ReplicateClient::Deployment

Find a deployment by owner and name.

Parameters:

  • full_name (String)

    The full name of the deployment in “owner/name” format.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



73
74
75
76
77
# File 'lib/replicate-client/deployment.rb', line 73

def find(full_name, client: ReplicateClient.client)
  path = build_path(**parse_full_name(full_name))
  attributes = client.get(path)
  new(attributes, client: client)
end

.find_by(owner:, name:, client: ReplicateClient.client) ⇒ ReplicateClient::Deployment?

Find a deployment by owner and name.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



99
100
101
102
103
# File 'lib/replicate-client/deployment.rb', line 99

def find_by(owner:, name:, client: ReplicateClient.client)
  find_by!(owner: owner, name: name, client: client)
rescue ReplicateClient::NotFoundError
  nil
end

.find_by!(owner:, name:, client: ReplicateClient.client) ⇒ ReplicateClient::Deployment

Find a deployment by owner and name.

Parameters:

  • owner (String)

    The owner of the deployment.

  • name (String)

    The name of the deployment.

  • client (ReplicateClient::Client) (defaults to: ReplicateClient.client)

    The client to use for requests.

Returns:



86
87
88
89
90
# File 'lib/replicate-client/deployment.rb', line 86

def find_by!(owner:, name:, client: ReplicateClient.client)
  path = build_path(owner: owner, name: name)
  attributes = client.get(path)
  new(attributes, client: client)
end

.parse_full_name(full_name) ⇒ Hash

Parse the full name for a deployment.

Parameters:

  • full_name (String)

    The full name of the deployment.

Returns:

  • (Hash)


132
133
134
135
# File 'lib/replicate-client/deployment.rb', line 132

def parse_full_name(full_name)
  parts = full_name.split("/")
  { owner: parts[0], name: parts[1] }
end

Instance Method Details

#create_prediction!(input, webhook_url: nil, webhook_events_filter: nil) ⇒ ReplicateClient::Prediction

Create prediction for the deployment.

Parameters:

  • input (Hash)

    The input for the prediction.

  • webhook_url (String, nil) (defaults to: nil)

    The URL to send webhook events to.

  • webhook_events_filter (Array<String>, nil) (defaults to: nil)

    The events to send to the webhook.

Returns:



208
209
210
211
212
213
214
215
216
# File 'lib/replicate-client/deployment.rb', line 208

def create_prediction!(input, webhook_url: nil, webhook_events_filter: nil)
  Prediction.create_for_deployment!(
    deployment: self,
    input: input,
    webhook_url: webhook_url,
    webhook_events_filter: webhook_events_filter,
    client: @client
  )
end

#destroy!void

This method returns an undefined value.

Destroy the deployment.



160
161
162
# File 'lib/replicate-client/deployment.rb', line 160

def destroy!
  self.class.destroy!(owner: owner, name: name, client: @client)
end

#pathString

Build the path for the deployment.

Returns:

  • (String)


197
198
199
# File 'lib/replicate-client/deployment.rb', line 197

def path
  self.class.build_path(owner: owner, name: name)
end

#reload!void

This method returns an undefined value.

Reload the deployment.



189
190
191
192
# File 'lib/replicate-client/deployment.rb', line 189

def reload!
  attributes = @client.get(path)
  reset_attributes(attributes)
end

#update!(hardware: nil, min_instances: nil, max_instances: nil, version: nil) ⇒ void

This method returns an undefined value.

Update the deployment.

Parameters:

  • hardware (String, nil) (defaults to: nil)

    The hardware SKU.

  • min_instances (Integer, nil) (defaults to: nil)

    The minimum number of instances.

  • max_instances (Integer, nil) (defaults to: nil)

    The maximum number of instances.

  • version (ReplicateClient::Version, String, nil) (defaults to: nil)

    The version ID of the model.



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/replicate-client/deployment.rb', line 172

def update!(hardware: nil, min_instances: nil, max_instances: nil, version: nil)
  version_id = version.is_a?(ReplicateClient::Model::Version) ? version.id : version
  path = self.class.build_path(owner: owner, name: name)
  body = {
    hardware: hardware,
    min_instances: min_instances,
    max_instances: max_instances,
    version: version_id
  }.compact

  attributes = @client.patch(path, body)
  reset_attributes(attributes)
end