Class: ReplicateClient::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/replicate-client/model.rb,
lib/replicate-client/model/version.rb

Defined Under Namespace

Modules: Visibility Classes: Version

Constant Summary collapse

INDEX_PATH =
"/models"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, version_id: nil) ⇒ ReplicateClient::Model

Initialize a new model.

Parameters:

  • attributes (Hash)

    The attributes of the model.

  • version_id (String) (defaults to: nil)

    The version of the model to use.



210
211
212
# File 'lib/replicate-client/model.rb', line 210

def initialize(attributes, version_id: nil)
  reset_attributes(attributes, version_id: version_id)
end

Instance Attribute Details

#cover_image_urlString

A URL for the model’s cover image. This should be an image file.

Returns:

  • (String)


187
188
189
# File 'lib/replicate-client/model.rb', line 187

def cover_image_url
  @cover_image_url
end

#default_exampleHash

The default example of the model.

Returns:

  • (Hash)


192
193
194
# File 'lib/replicate-client/model.rb', line 192

def default_example
  @default_example
end

#descriptionString

A description of the model.

Returns:

  • (String)


156
157
158
# File 'lib/replicate-client/model.rb', line 156

def description
  @description
end

#github_urlString

A URL for the model’s source code on GitHub.

Returns:

  • (String)


167
168
169
# File 'lib/replicate-client/model.rb', line 167

def github_url
  @github_url
end

#latest_version_idHash

The id of the latest version of the model.

Returns:

  • (Hash)


202
203
204
# File 'lib/replicate-client/model.rb', line 202

def latest_version_id
  @latest_version_id
end

#license_urlString

A URL for the model’s license.

Returns:

  • (String)


177
178
179
# File 'lib/replicate-client/model.rb', line 177

def license_url
  @license_url
end

#nameString

The name of the model.

Returns:

  • (String)


151
152
153
# File 'lib/replicate-client/model.rb', line 151

def name
  @name
end

#ownerString

The name of the user or organization that will own the model.

Returns:

  • (String)


146
147
148
# File 'lib/replicate-client/model.rb', line 146

def owner
  @owner
end

#paper_urlString

A URL for the model’s paper.

Returns:

  • (String)


172
173
174
# File 'lib/replicate-client/model.rb', line 172

def paper_url
  @paper_url
end

#run_countInteger

The number of times the model has been run.

Returns:

  • (Integer)


182
183
184
# File 'lib/replicate-client/model.rb', line 182

def run_count
  @run_count
end

#urlString

The URL of the model.

Returns:

  • (String)


141
142
143
# File 'lib/replicate-client/model.rb', line 141

def url
  @url
end

#version_idString

The current version id of the model.

Returns:

  • (String)


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

def version_id
  @version_id
end

#visibilityString

Whether the model should be public or private. A public model can be viewed and run by anyone, whereas a private model can be viewed and run only by the user or organization members that own the model.

Returns:

  • (String)

    “public” or “private”



162
163
164
# File 'lib/replicate-client/model.rb', line 162

def visibility
  @visibility
end

Class Method Details

.auto_paging_each {|ReplicateClient::Model| ... } ⇒ void

This method returns an undefined value.

Paginate through all models.

Yields:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/replicate-client/model.rb', line 66

def auto_paging_each(&block)
  cursor = nil

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

    models = attributes["results"].map { |model| new(model) }

    models.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 the model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

Returns:

  • (String)


57
58
59
# File 'lib/replicate-client/model.rb', line 57

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

.create!(owner:, name:, description:, visibility:, hardware:, github_url: nil, paper_url: nil, license_url: nil, cover_image_url: nil) ⇒ ReplicateClient::Model

Create a new model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • description (String)

    A description of the model.

  • visibility (String)

    “public” or “private”.

  • hardware (String)

    The SKU for the hardware used to run the model.

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

    A URL for the model’s source code on GitHub.

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

    A URL for the model’s paper.

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

    A URL for the model’s license.

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

    A URL for the model’s cover image.

Returns:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/replicate-client/model.rb', line 95

def create!(
  owner:,
  name:,
  description:,
  visibility:,
  hardware:,
  github_url: nil,
  paper_url: nil,
  license_url: nil,
  cover_image_url: nil
)
  new_attributes = {
    owner: owner,
    name: name,
    description: description,
    visibility: visibility,
    hardware: hardware,
    github_url: github_url,
    paper_url: paper_url,
    license_url: license_url,
    cover_image_url: cover_image_url
  }

  attributes = ReplicateClient.client.post("/models", new_attributes)

  new(attributes)
end

.find(name, version_id: nil) ⇒ ReplicateClient::Model

Find a model by name. The name should be in the format “owner/name”.

Parameters:

  • name (String)

    The name of the model.

  • version_id (String) (defaults to: nil)

    The version id of the model to use.

Returns:



47
48
49
# File 'lib/replicate-client/model.rb', line 47

def find(name, version_id: nil)
  find_by!(**parse_model_name(name), version_id: version_id)
end

.find_by(owner:, name:, version_id: nil) ⇒ ReplicateClient::Model

Find a model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

  • version_id (String) (defaults to: nil)

    The version id of the model to use.

Returns:



34
35
36
37
38
# File 'lib/replicate-client/model.rb', line 34

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

.find_by!(owner:, name:, version_id: nil) ⇒ ReplicateClient::Model

Find a model.

Parameters:

  • owner (String)

    The owner of the model.

  • name (String)

    The name of the model.

Returns:



21
22
23
24
25
# File 'lib/replicate-client/model.rb', line 21

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

.parse_model_name(model_name) ⇒ Hash

Parse the model name.

Parameters:

  • model_name (String)

    The name of the model.

Returns:

  • (Hash)


128
129
130
131
132
133
134
135
# File 'lib/replicate-client/model.rb', line 128

def parse_model_name(model_name)
  parts = model_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 a new prediction for the model.

Parameters:

  • input (Hash)

    The input data for the prediction.

Returns:



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/replicate-client/model.rb', line 261

def create_prediction!(input:, webhook_url: nil, webhook_events_filter: nil)
  if version_id.nil?
    Prediction.create_for_official_model!(
      model: self,
      input: input,
      webhook_url: webhook_url,
      webhook_events_filter: webhook_events_filter
    )
  else
    Prediction.create!(
      version: version_id,
      input: input,
      webhook_url: webhook_url,
      webhook_events_filter: webhook_events_filter
    )
  end
end

#destroy!void

This method returns an undefined value.

Delete the model.



224
225
226
# File 'lib/replicate-client/model.rb', line 224

def destroy!
  ReplicateClient.client.delete(path)
end

#full_nameString

Returns the full name of the model in “owner/name” format.

Returns:

  • (String)


304
305
306
# File 'lib/replicate-client/model.rb', line 304

def full_name
  "#{owner}/#{name}"
end

#latest_versionReplicateClient::Model::Version

The latest version of the model.



245
246
247
# File 'lib/replicate-client/model.rb', line 245

def latest_version
  @latest_version ||= Version.find_by!(owner: owner, name: name, version_id: latest_version_id)
end

#pathString

The path of the model.

Returns:

  • (String)


217
218
219
# File 'lib/replicate-client/model.rb', line 217

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

#private?Boolean

Check if the model is private.

Returns:

  • (Boolean)


297
298
299
# File 'lib/replicate-client/model.rb', line 297

def private?
  visibility == Visibility::PRIVATE
end

#public?Boolean

Check if the model is public.

Returns:

  • (Boolean)


290
291
292
# File 'lib/replicate-client/model.rb', line 290

def public?
  visibility == Visibility::PUBLIC
end

#reload!void

This method returns an undefined value.

Reload the model.



282
283
284
285
# File 'lib/replicate-client/model.rb', line 282

def reload!
  attributes = ReplicateClient.client.get(path)
  reset_attributes(attributes, version_id: version_id)
end

#versionReplicateClient::Model::Version

The version of the model.



238
239
240
# File 'lib/replicate-client/model.rb', line 238

def version
  @version ||= Version.find_by!(owner: owner, name: name, version_id: version_id)
end

#version_pathString

The path of the current version.

Returns:

  • (String)


231
232
233
# File 'lib/replicate-client/model.rb', line 231

def version_path
  Version.build_path(owner: owner, name: name, version_id: version_id)
end

#versionsArray<ReplicateClient::Model::Version>

The versions of the model.



252
253
254
# File 'lib/replicate-client/model.rb', line 252

def versions
  @versions ||= Version.where(owner: owner, name: name)
end