Class: ReplicateClient::Training
- Inherits:
-
Object
- Object
- ReplicateClient::Training
- Defined in:
- lib/replicate-client/training.rb
Defined Under Namespace
Modules: Status
Constant Summary collapse
- INDEX_PATH =
"/trainings"
Instance Attribute Summary collapse
-
#client ⇒ ReplicateClient::Client
The client used to make API requests for this training.
-
#completed_at ⇒ Time?
The timestamp when the training was completed.
-
#created_at ⇒ String
The timestamp when the training was created.
-
#error ⇒ String?
The error message, if any, encountered during the training process.
-
#id ⇒ String
The unique identifier of the training.
-
#input ⇒ Hash
The input data provided for the training.
-
#logs ⇒ String
The logs generated during the training process.
-
#metrics ⇒ Hash?
The metrics generated during the training process.
-
#model_full_name ⇒ String
The full model name in the format “owner/name”.
-
#output ⇒ Hash?
The output data generated during the training process.
-
#started_at ⇒ Time?
The timestamp when the training was started.
-
#status ⇒ String
The current status of the training.
-
#urls ⇒ Hash
URLs related to the training, such as those for retrieving or canceling it.
-
#version_id ⇒ String
The version ID of the model being trained.
Class Method Summary collapse
-
.auto_paging_each(client: ReplicateClient.client) {|ReplicateClient::Training| ... } ⇒ void
List all trainings.
-
.build_path(id:) ⇒ String
Build the path for a specific training.
-
.cancel!(id, client: ReplicateClient.client) ⇒ void
Cancel a training.
-
.create!(owner:, name:, version:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Create a new training.
-
.create_for_model!(model:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Create a new training for a specific model.
-
.find(id, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Find a training by id.
Instance Method Summary collapse
-
#cancel! ⇒ void
Cancel the training.
-
#canceled? ⇒ Boolean
Check if the training was canceled.
-
#failed? ⇒ Boolean
Check if the training has failed.
-
#initialize(attributes, client: ReplicateClient.client) ⇒ ReplicateClient::Training
constructor
Initialize a new training instance.
-
#model ⇒ ReplicateClient::Model
The model instance of the training.
-
#processing? ⇒ Boolean
Check if the training is processing.
-
#reload! ⇒ void
Reload the training.
-
#starting? ⇒ Boolean
Check if the training is starting.
-
#succeeded? ⇒ Boolean
Check if the training has succeeded.
-
#version ⇒ ReplicateClient::Model::Version
The version instance of the training.
Constructor Details
#initialize(attributes, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Initialize a new training instance.
211 212 213 214 |
# File 'lib/replicate-client/training.rb', line 211 def initialize(attributes, client: ReplicateClient.client) @client = client reset_attributes(attributes) end |
Instance Attribute Details
#client ⇒ ReplicateClient::Client
The client used to make API requests for this training.
203 204 205 |
# File 'lib/replicate-client/training.rb', line 203 def client @client end |
#completed_at ⇒ Time?
The timestamp when the training was completed.
168 169 170 |
# File 'lib/replicate-client/training.rb', line 168 def completed_at @completed_at end |
#created_at ⇒ String
The timestamp when the training was created.
163 164 165 |
# File 'lib/replicate-client/training.rb', line 163 def created_at @created_at end |
#error ⇒ String?
The error message, if any, encountered during the training process.
183 184 185 |
# File 'lib/replicate-client/training.rb', line 183 def error @error end |
#id ⇒ String
The unique identifier of the training.
137 138 139 |
# File 'lib/replicate-client/training.rb', line 137 def id @id end |
#input ⇒ Hash
The input data provided for the training.
152 153 154 |
# File 'lib/replicate-client/training.rb', line 152 def input @input end |
#logs ⇒ String
The logs generated during the training process.
178 179 180 |
# File 'lib/replicate-client/training.rb', line 178 def logs @logs end |
#metrics ⇒ Hash?
The metrics generated during the training process.
198 199 200 |
# File 'lib/replicate-client/training.rb', line 198 def metrics @metrics end |
#model_full_name ⇒ String
The full model name in the format “owner/name”.
142 143 144 |
# File 'lib/replicate-client/training.rb', line 142 def model_full_name @model_full_name end |
#output ⇒ Hash?
The output data generated during the training process.
193 194 195 |
# File 'lib/replicate-client/training.rb', line 193 def output @output end |
#started_at ⇒ Time?
The timestamp when the training was started.
173 174 175 |
# File 'lib/replicate-client/training.rb', line 173 def started_at @started_at end |
#status ⇒ String
The current status of the training. Possible values: “starting”, “processing”, “succeeded”, “failed”, “canceled”.
158 159 160 |
# File 'lib/replicate-client/training.rb', line 158 def status @status end |
#urls ⇒ Hash
URLs related to the training, such as those for retrieving or canceling it.
188 189 190 |
# File 'lib/replicate-client/training.rb', line 188 def urls @urls end |
#version_id ⇒ String
The version ID of the model being trained.
147 148 149 |
# File 'lib/replicate-client/training.rb', line 147 def version_id @version_id end |
Class Method Details
.auto_paging_each(client: ReplicateClient.client) {|ReplicateClient::Training| ... } ⇒ void
This method returns an undefined value.
List all trainings.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/replicate-client/training.rb', line 23 def auto_paging_each(client: ReplicateClient.client, &block) cursor = nil loop do url_params = cursor ? "?cursor=#{cursor}" : "" attributes = client.get("#{INDEX_PATH}#{url_params}") trainings = attributes["results"].map { |training| new(training, client: client) } trainings.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(id:) ⇒ String
Build the path for a specific training.
129 130 131 |
# File 'lib/replicate-client/training.rb', line 129 def build_path(id:) "#{INDEX_PATH}/#{id}" end |
.cancel!(id, client: ReplicateClient.client) ⇒ void
This method returns an undefined value.
Cancel a training.
119 120 121 122 |
# File 'lib/replicate-client/training.rb', line 119 def cancel!(id, client: ReplicateClient.client) path = "#{build_path(id: id)}/cancel" client.post(path, {}) end |
.create!(owner:, name:, version:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Create a new training.
format.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/replicate-client/training.rb', line 52 def create!(owner:, name:, version:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) destination_str = destination.is_a?(ReplicateClient::Model) ? destination.full_name : destination version_id = version.is_a?(ReplicateClient::Model::Version) ? version.id : version path = "/models/#{owner}/#{name}/versions/#{version_id}/trainings" body = { destination: destination_str, input: input, webhook: webhook_url || client.configuration.webhook_url, webhook_events_filter: webhook_events_filter } attributes = client.post(path, body) new(attributes, client: client) end |
.create_for_model!(model:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Create a new training for a specific model.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/replicate-client/training.rb', line 79 def create_for_model!(model:, destination:, input:, webhook_url: nil, webhook_events_filter: nil, client: ReplicateClient.client) model_instance = if model.is_a?(ReplicateClient::Model) model else ReplicateClient::Model.find(model, client: client) end raise ArgumentError, "Invalid model" unless model_instance create!( owner: model_instance.owner, name: model_instance.name, version: model_instance.version_id, destination: destination, input: input, webhook_url: webhook_url || client.configuration.webhook_url, webhook_events_filter: webhook_events_filter, client: client ) end |
.find(id, client: ReplicateClient.client) ⇒ ReplicateClient::Training
Find a training by id.
107 108 109 110 111 |
# File 'lib/replicate-client/training.rb', line 107 def find(id, client: ReplicateClient.client) path = build_path(id: id) attributes = client.get(path) new(attributes, client: client) end |
Instance Method Details
#cancel! ⇒ void
This method returns an undefined value.
Cancel the training.
254 255 256 |
# File 'lib/replicate-client/training.rb', line 254 def cancel! ReplicateClient::Training.cancel!(id, client: @client) end |
#canceled? ⇒ Boolean
Check if the training was canceled.
247 248 249 |
# File 'lib/replicate-client/training.rb', line 247 def canceled? status == Status::CANCELED end |
#failed? ⇒ Boolean
Check if the training has failed.
240 241 242 |
# File 'lib/replicate-client/training.rb', line 240 def failed? status == Status::FAILED end |
#model ⇒ ReplicateClient::Model
The model instance of the training.
269 270 271 |
# File 'lib/replicate-client/training.rb', line 269 def model @model ||= ReplicateClient::Model.find(model_full_name, version_id: version_id, client: @client) end |
#processing? ⇒ Boolean
Check if the training is processing.
226 227 228 |
# File 'lib/replicate-client/training.rb', line 226 def processing? status == Status::PROCESSING end |
#reload! ⇒ void
This method returns an undefined value.
Reload the training.
261 262 263 264 |
# File 'lib/replicate-client/training.rb', line 261 def reload! attributes = @client.get(Training.build_path(id: id)) reset_attributes(attributes) end |
#starting? ⇒ Boolean
Check if the training is starting.
219 220 221 |
# File 'lib/replicate-client/training.rb', line 219 def starting? status == Status::STARTING end |
#succeeded? ⇒ Boolean
Check if the training has succeeded.
233 234 235 |
# File 'lib/replicate-client/training.rb', line 233 def succeeded? status == Status::SUCCEEDED end |
#version ⇒ ReplicateClient::Model::Version
The version instance of the training.
276 277 278 |
# File 'lib/replicate-client/training.rb', line 276 def version @version ||= model.version end |