Class: Informers::ImageToTextPipeline

Inherits:
Pipeline
  • Object
show all
Defined in:
lib/informers/pipelines.rb

Instance Method Summary collapse

Methods inherited from Pipeline

#initialize

Constructor Details

This class inherits a constructor from Informers::Pipeline

Instance Method Details

#call(images, **generate_kwargs) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/informers/pipelines.rb', line 500

def call(images, **generate_kwargs)
  is_batched = images.is_a?(Array)
  prepared_images = prepare_images(images)

  pixel_values = @processor.(prepared_images)[:pixel_values]

  to_return = []
  pixel_values.each do |batch|
    batch = [batch]
    output = @model.generate(batch, **generate_kwargs)
    decoded = @tokenizer
      .batch_decode(output, skip_special_tokens: true)
      .map { |x| {generated_text: x.strip} }
    to_return << decoded
  end

  is_batched ? to_return : to_return[0]
end