Class: Informers::TextClassificationPipeline
- Defined in:
- lib/informers/pipelines.rb
Instance Method Summary collapse
Methods inherited from Pipeline
Constructor Details
This class inherits a constructor from Informers::Pipeline
Instance Method Details
#call(texts, top_k: 1) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/informers/pipelines.rb', line 13 def call(texts, top_k: 1) # Run tokenization model_inputs = @tokenizer.(texts, padding: true, truncation: true ) # Run model outputs = @model.(model_inputs) function_to_apply = if @model.config.problem_type == "multi_label_classification" ->(batch) { Utils.sigmoid(batch) } else ->(batch) { Utils.softmax(batch) } # single_label_classification (default) end id2label = @model.config.id2label to_return = [] outputs.logits.each do |batch| output = function_to_apply.(batch) scores = Utils.get_top_items(output, top_k) vals = scores.map do |x| { label: id2label[x[0].to_s], score: x[1] } end if top_k == 1 to_return.concat(vals) else to_return << vals end end texts.is_a?(Array) ? to_return : to_return[0] end |