Class: Informers::QuestionAnsweringPipeline
- Defined in:
- lib/informers/pipelines.rb
Instance Method Summary collapse
- #call(question, context, top_k: 1) ⇒ Object
-
#initialize(**options) ⇒ QuestionAnsweringPipeline
constructor
A new instance of QuestionAnsweringPipeline.
Constructor Details
#initialize(**options) ⇒ QuestionAnsweringPipeline
Returns a new instance of QuestionAnsweringPipeline.
203 204 205 |
# File 'lib/informers/pipelines.rb', line 203 def initialize(**) super(**) end |
Instance Method Details
#call(question, context, top_k: 1) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/informers/pipelines.rb', line 207 def call(question, context, top_k: 1) # Run tokenization inputs = @tokenizer.(question, text_pair: context, padding: true, truncation: true, return_offsets: true ) output = @model.(inputs) to_return = [] output.start_logits.length.times do |j| ids = inputs[:input_ids][j] sep_index = ids.index(@tokenizer.sep_token_id) offsets = inputs[:offsets][j] s1 = Utils.softmax(output.start_logits[j]) .map.with_index .select { |x| x[1] > sep_index } e1 = Utils.softmax(output.end_logits[j]) .map.with_index .select { |x| x[1] > sep_index } = s1.product(e1) .select { |x| x[0][1] <= x[1][1] } .map { |x| [x[0][1], x[1][1], x[0][0] * x[1][0]] } .sort_by { |v| -v[2] } [.length, top_k].min.times do |k| start, end_, score = [k] answer_tokens = ids.slice(start, end_ + 1) answer = @tokenizer.decode(answer_tokens, skip_special_tokens: true ) to_return << { answer:, score:, start: offsets[start][0], end: offsets[end_][1] } end end question.is_a?(Array) ? to_return : to_return[0] end |