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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/informers/pipelines.rb', line 33 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 |