class Bazil::Model

Attributes

config_id[R]
model_id[R]

Public Class Methods

new(client, model_id, config_id) click to toggle source
# File lib/bazil/model.rb, line 9
def initialize(client, model_id, config_id)
  @client = client
  @http_cli = client.http_client
  @model_id = model_id
  @config_id = config_id
end

Public Instance Methods

config() click to toggle source
# File lib/bazil/model.rb, line 33
def config
  res = @http_cli.get(gen_uri("configs/#{@config_id}"))
  raise_error("Failed to get config of the model: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)
end
delete_all_training_data() click to toggle source
# File lib/bazil/model.rb, line 98
def delete_all_training_data
  res = @http_cli.delete(gen_uri("training_data"))
  raise_error("Failed to delete all training_data of the model: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  {}
end
delete_training_data(id) click to toggle source
# File lib/bazil/model.rb, line 119
def delete_training_data(id)
  raise ArgumentError, 'Id must be Integer' unless id.kind_of? Integer
  res = @http_cli.delete(gen_uri("training_data/#{id}"))
  raise_error("Failed to delete a training data: id = #{id}, #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  {}
end
evaluate(method, config = nil) click to toggle source
# File lib/bazil/model.rb, line 67
def evaluate(method, config = nil)
  new_data = {}
  new_data['method'] = method if method
  new_data['config'] = config if config
  body = post(target_path(@config_id, "evaluate"), new_data.to_json, "Failed to execute evaluate")
  JSON.parse(body)
end
labels() click to toggle source
# File lib/bazil/model.rb, line 75
def labels
  res = @http_cli.get(gen_uri(target_path(@config_id, "labels")))
  raise_error("Failed to get labels the model has: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)['labels']
end
list_training_data(condition = {}) click to toggle source
# File lib/bazil/model.rb, line 88
def list_training_data(condition = {})
  condition = condition.dup
  condition[:page] ||= 1
  condition[:per_page] ||= 10

  res = @http_cli.get(gen_uri("training_data?page=#{condition[:page]}&per_page=#{condition[:per_page]}"))
  raise_error("Failed to query training data of the model", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)
end
model_config() click to toggle source
# File lib/bazil/model.rb, line 22
def model_config
  res = @http_cli.get(gen_uri())
  raise_error("Failed to get model config: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)
end
put_training_data(new_data = {}) click to toggle source
# File lib/bazil/model.rb, line 104
def put_training_data(new_data = {})
  new_data = symbolize_keys(new_data)
  raise ArgumentError, 'Data must be not nil' unless new_data.has_key? :data
  body = post('training_data', new_data.to_json, "Failed to post training data")
  JSON.parse(body)
end
query(data) click to toggle source
# File lib/bazil/model.rb, line 126
def query(data)
  data = {'data' => data}.to_json
  res = post(target_path(@config_id, 'query'), data, "Failed to post data for query")
  JSON.parse(res)
end
retrain(option = {}) click to toggle source
# File lib/bazil/model.rb, line 53
def retrain(option = {})
  body = post(target_path(@config_id, 'retrain'), option.to_json, "Failed to retrain the model")
  JSON.parse(body)
end
status() click to toggle source
# File lib/bazil/model.rb, line 16
def status
  res = @http_cli.get(gen_uri(target_path(@config_id, "status")))
  raise_error("Failed to get status of the model: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)
end
trace(method, data, config = nil) click to toggle source
# File lib/bazil/model.rb, line 58
def trace(method, data, config = nil)
  new_data = {}
  new_data['method'] = method if method
  new_data['data'] = data if data
  new_data['config'] = config if config
  body = post(target_path(@config_id, "trace"), new_data.to_json, "Failed to execute trace")
  JSON.parse(body)
end
train(train_data) click to toggle source
# File lib/bazil/model.rb, line 44
def train(train_data)
  train_data = symbolize_keys(train_data)
  raise ArgumentError, 'Annotation must be not nil' unless train_data.has_key? :annotation
  raise ArgumentError, 'Data must be not nil' unless train_data.has_key? :data

  body = post("training_data?train=true", train_data.to_json, "Failed to post training data")
  JSON.parse(body)
end
training_data(id) click to toggle source
# File lib/bazil/model.rb, line 81
def training_data(id)
  raise ArgumentError, 'Id must be Integer' unless id.kind_of? Integer
  res = @http_cli.get(gen_uri("training_data/#{id}"))
  raise_error("Failed to get training data of the model: id = #{id}, #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/
  JSON.parse(res.body)
end
update_config(config) click to toggle source
# File lib/bazil/model.rb, line 39
def update_config(config)
  res = send(:put, "configs/#{@config_id}", config.to_json, "Failed to updated config")
  {}
end
update_model_config(conf) click to toggle source
# File lib/bazil/model.rb, line 28
def update_model_config(conf)
  res = @http_cli.put(gen_uri(), conf.to_json, {'Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => conf.to_json.length.to_s})
  JSON.parse(res.body)
end
update_training_data(id, new_data = {}) click to toggle source
# File lib/bazil/model.rb, line 111
def update_training_data(id, new_data = {})
  new_data = symbolize_keys(new_data)
  raise ArgumentError, 'Id must be Integer' unless id.kind_of? Integer
  raise ArgumentError, 'Data must be not nil' unless new_data.has_key? :data
  send(:put, "training_data/#{id}", new_data.to_json, "Failed to update training data")
  {}
end

Private Instance Methods

error_suffix() click to toggle source
# File lib/bazil/model.rb, line 172
def error_suffix
  "model = #{@model_id}"
end
gen_uri(path = nil) click to toggle source
# File lib/bazil/model.rb, line 164
def gen_uri(path = nil)
  if path
    "/#{@client.api_version}/models/#{@model_id}/#{path}"
  else
    "/#{@client.api_version}/models/#{@model_id}"
  end
end
post(path, data, error_message) click to toggle source
# File lib/bazil/model.rb, line 150
def post(path, data, error_message)
  send(:post, path, data, error_message)
end
raise_error(message, res) click to toggle source
# File lib/bazil/model.rb, line 176
def raise_error(message, res)
  raise APIError.new(message, res.code, JSON.parse(res.body))
end
send(method, path, data, error_message) click to toggle source
# File lib/bazil/model.rb, line 154
def send(method, path, data, error_message)
  res = @http_cli.method(method).call(gen_uri(path), data, {'Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => data.length.to_s})
  raise_error("#{error_message}: #{error_suffix}", res) unless res.code =~ /2[0-9][0-9]/ # TODO: enhance error information
  res.body
end
stringify_keys(data) click to toggle source
# File lib/bazil/model.rb, line 142
def stringify_keys(data)
  {}.tap{|d|
    data.each{|k,v|
      d[k.to_s] = v
    }
  }
end
symbolize_keys(data) click to toggle source
# File lib/bazil/model.rb, line 134
def symbolize_keys(data)
  {}.tap{|d|
    data.each{|k,v|
      d[k.to_sym] = v
    }
  }
end
target_path(id, path) click to toggle source
# File lib/bazil/model.rb, line 160
def target_path(id, path)
  "configs/#{id}/#{path}"
end