Class: ReplicateClient::Model
- Inherits:
-
Object
- Object
- ReplicateClient::Model
- 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
-
#cover_image_url ⇒ String
A URL for the model’s cover image.
-
#default_example ⇒ Hash
The default example of the model.
-
#description ⇒ String
A description of the model.
-
#github_url ⇒ String
A URL for the model’s source code on GitHub.
-
#latest_version_id ⇒ Hash
The id of the latest version of the model.
-
#license_url ⇒ String
A URL for the model’s license.
-
#name ⇒ String
The name of the model.
-
#owner ⇒ String
The name of the user or organization that will own the model.
-
#paper_url ⇒ String
A URL for the model’s paper.
-
#run_count ⇒ Integer
The number of times the model has been run.
-
#url ⇒ String
The URL of the model.
-
#version_id ⇒ String
The current version id of the model.
-
#visibility ⇒ String
Whether the model should be public or private.
Class Method Summary collapse
-
.auto_paging_each {|ReplicateClient::Model| ... } ⇒ void
Paginate through all models.
-
.build_path(owner:, name:) ⇒ String
Build the path for the model.
-
.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.
-
.find(name, version_id: nil) ⇒ ReplicateClient::Model
Find a model by name.
-
.find_by(owner:, name:, version_id: nil) ⇒ ReplicateClient::Model
Find a model.
-
.find_by!(owner:, name:, version_id: nil) ⇒ ReplicateClient::Model
Find a model.
-
.parse_model_name(model_name) ⇒ Hash
Parse the model name.
Instance Method Summary collapse
-
#create_prediction!(input:, webhook_url: nil, webhook_events_filter: nil) ⇒ ReplicateClient::Prediction
Create a new prediction for the model.
-
#destroy! ⇒ void
Delete the model.
-
#full_name ⇒ String
Returns the full name of the model in “owner/name” format.
-
#initialize(attributes, version_id: nil) ⇒ ReplicateClient::Model
constructor
Initialize a new model.
-
#latest_version ⇒ ReplicateClient::Model::Version
The latest version of the model.
-
#path ⇒ String
The path of the model.
-
#private? ⇒ Boolean
Check if the model is private.
-
#public? ⇒ Boolean
Check if the model is public.
-
#reload! ⇒ void
Reload the model.
-
#version ⇒ ReplicateClient::Model::Version
The version of the model.
-
#version_path ⇒ String
The path of the current version.
-
#versions ⇒ Array<ReplicateClient::Model::Version>
The versions of the model.
Constructor Details
#initialize(attributes, version_id: nil) ⇒ ReplicateClient::Model
Initialize a new model.
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_url ⇒ String
A URL for the model’s cover image. This should be an image file.
187 188 189 |
# File 'lib/replicate-client/model.rb', line 187 def cover_image_url @cover_image_url end |
#default_example ⇒ Hash
The default example of the model.
192 193 194 |
# File 'lib/replicate-client/model.rb', line 192 def default_example @default_example end |
#description ⇒ String
A description of the model.
156 157 158 |
# File 'lib/replicate-client/model.rb', line 156 def description @description end |
#github_url ⇒ String
A URL for the model’s source code on GitHub.
167 168 169 |
# File 'lib/replicate-client/model.rb', line 167 def github_url @github_url end |
#latest_version_id ⇒ Hash
The id of the latest version of the model.
202 203 204 |
# File 'lib/replicate-client/model.rb', line 202 def latest_version_id @latest_version_id end |
#license_url ⇒ String
A URL for the model’s license.
177 178 179 |
# File 'lib/replicate-client/model.rb', line 177 def license_url @license_url end |
#name ⇒ String
The name of the model.
151 152 153 |
# File 'lib/replicate-client/model.rb', line 151 def name @name end |
#owner ⇒ String
The name of the user or organization that will own the model.
146 147 148 |
# File 'lib/replicate-client/model.rb', line 146 def owner @owner end |
#paper_url ⇒ String
A URL for the model’s paper.
172 173 174 |
# File 'lib/replicate-client/model.rb', line 172 def paper_url @paper_url end |
#run_count ⇒ Integer
The number of times the model has been run.
182 183 184 |
# File 'lib/replicate-client/model.rb', line 182 def run_count @run_count end |
#url ⇒ String
The URL of the model.
141 142 143 |
# File 'lib/replicate-client/model.rb', line 141 def url @url end |
#version_id ⇒ String
The current version id of the model.
197 198 199 |
# File 'lib/replicate-client/model.rb', line 197 def version_id @version_id end |
#visibility ⇒ String
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.
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.
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.
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.
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”.
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.
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.
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.
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.
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_name ⇒ String
Returns the full name of the model in “owner/name” format.
304 305 306 |
# File 'lib/replicate-client/model.rb', line 304 def full_name "#{owner}/#{name}" end |
#latest_version ⇒ ReplicateClient::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 |
#path ⇒ String
The path of the model.
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.
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.
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 |
#version ⇒ ReplicateClient::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_path ⇒ String
The path of the current version.
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 |
#versions ⇒ Array<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 |