Class: Informers::TextClassificationPipeline
- Defined in:
- lib/informers/pipelines.rb
Instance Method Summary collapse
- #call(texts, top_k: 1) ⇒ Object
-
#initialize(**options) ⇒ TextClassificationPipeline
constructor
A new instance of TextClassificationPipeline.
Constructor Details
#initialize(**options) ⇒ TextClassificationPipeline
Returns a new instance of TextClassificationPipeline.
13 14 15 |
# File 'lib/informers/pipelines.rb', line 13 def initialize(**) super(**) end |
Instance Method Details
#call(texts, top_k: 1) ⇒ Object
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 52 53 54 55 |
# File 'lib/informers/pipelines.rb', line 17 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 |