Class: Bazil::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/bazil/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, model_id, config_id) ⇒ Model

Returns a new instance of Model.



9
10
11
12
13
14
# 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

Instance Attribute Details

#config_idObject (readonly)

Returns the value of attribute config_id.



7
8
9
# File 'lib/bazil/model.rb', line 7

def config_id
  @config_id
end

#model_idObject (readonly)

Returns the value of attribute model_id.



7
8
9
# File 'lib/bazil/model.rb', line 7

def model_id
  @model_id
end

Instance Method Details

#configObject



33
34
35
36
37
# 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_dataObject



98
99
100
101
102
# 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) ⇒ Object

Raises:

  • (ArgumentError)


119
120
121
122
123
124
# 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) ⇒ Object



67
68
69
70
71
72
73
# 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

#labelsObject



75
76
77
78
79
# 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 = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
# 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_configObject



22
23
24
25
26
# 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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


104
105
106
107
108
109
# 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) ⇒ Object



126
127
128
129
130
# 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 = {}) ⇒ Object



53
54
55
56
# 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

#statusObject



16
17
18
19
20
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
# 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) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# 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) ⇒ Object



39
40
41
42
# 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) ⇒ Object



28
29
30
31
# 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 = {}) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
# 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