diff --git a/lib/mindee/parsing/v2.rb b/lib/mindee/parsing/v2.rb index 746d0a4b..d5ae4ebb 100644 --- a/lib/mindee/parsing/v2.rb +++ b/lib/mindee/parsing/v2.rb @@ -8,8 +8,9 @@ require_relative 'v2/inference_model' require_relative 'v2/inference_response' require_relative 'v2/inference_result' -require_relative 'v2/inference_result_options' +require_relative 'v2/inference_active_options' require_relative 'v2/job' require_relative 'v2/job_response' require_relative 'v2/job_webhook' require_relative 'v2/raw_text' +require_relative 'v2/raw_text_page' diff --git a/lib/mindee/parsing/v2/inference.rb b/lib/mindee/parsing/v2/inference.rb index c843196c..ca5f815d 100644 --- a/lib/mindee/parsing/v2/inference.rb +++ b/lib/mindee/parsing/v2/inference.rb @@ -3,20 +3,23 @@ require_relative 'inference_model' require_relative 'inference_file' require_relative 'inference_result' +require_relative 'inference_active_options' module Mindee module Parsing module V2 # Complete data returned by an inference request. class Inference + # @return [String] Identifier of the inference (when provided by API). + attr_reader :id # @return [InferenceModel] Information about the model used. attr_reader :model # @return [InferenceFile] Information about the processed file. attr_reader :file + # @return [InferenceActiveOptions] Options which were activated during the inference. + attr_reader :active_options # @return [InferenceResult] Result contents. attr_reader :result - # @return [String] Identifier of the inference (when provided by API). - attr_reader :id # @param server_response [Hash] Hash representation of the JSON returned by the service. def initialize(server_response) @@ -24,6 +27,7 @@ def initialize(server_response) @model = InferenceModel.new(server_response['model']) @file = InferenceFile.new(server_response['file']) + @active_options = InferenceActiveOptions.new(server_response['active_options']) @result = InferenceResult.new(server_response['result']) @id = server_response['id'] @@ -35,11 +39,9 @@ def to_s [ 'Inference', '#########', - 'Model', - '=====', - ":ID: #{@model.id}", - '', + @model.to_s, @file.to_s, + @active_options.to_s, @result.to_s, '', ].join("\n") diff --git a/lib/mindee/parsing/v2/inference_active_options.rb b/lib/mindee/parsing/v2/inference_active_options.rb new file mode 100644 index 00000000..3a198c7b --- /dev/null +++ b/lib/mindee/parsing/v2/inference_active_options.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Mindee + module Parsing + module V2 + # Options which were activated during the inference. + class InferenceActiveOptions + # @return [Boolean] Whether the Raw Text feature was activated. + attr_reader :raw_text + # @return [Boolean] Whether the polygon feature was activated. + attr_reader :polygon + # @return [Boolean] Whether the confidence feature was activated. + attr_reader :confidence + # @return [Boolean] Whether the Retrieval-Augmented Generation feature was activated. + attr_reader :rag + + # @param server_response [Hash] Raw JSON parsed into a Hash. + def initialize(server_response) + @raw_text = server_response['raw_text'] + @polygon = server_response['polygon'] + @confidence = server_response['confidence'] + @rag = server_response['rag'] + end + + # String representation. + # @return [String] + def to_s + parts = [ + 'Active Options', + '==============', + ":Raw Text: #{@raw_text ? 'True' : 'False'}", + ":Polygon: #{@polygon ? 'True' : 'False'}", + ":Confidence: #{@confidence ? 'True' : 'False'}", + ":RAG: #{@rag ? 'True' : 'False'}", + '', + ] + parts.join("\n") + end + end + end + end +end diff --git a/lib/mindee/parsing/v2/inference_model.rb b/lib/mindee/parsing/v2/inference_model.rb index 1ee1af2b..85cb4eb8 100644 --- a/lib/mindee/parsing/v2/inference_model.rb +++ b/lib/mindee/parsing/v2/inference_model.rb @@ -12,6 +12,18 @@ class InferenceModel def initialize(server_response) @id = server_response['id'] end + + # String representation. + # @return [String] + def to_s + parts = [ + 'Model', + '=====', + ":ID: #{@id}", + '', + ] + parts.join("\n") + end end end end diff --git a/lib/mindee/parsing/v2/inference_result.rb b/lib/mindee/parsing/v2/inference_result.rb index 857dd107..f2c3c78d 100644 --- a/lib/mindee/parsing/v2/inference_result.rb +++ b/lib/mindee/parsing/v2/inference_result.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative 'field/inference_fields' -require_relative 'inference_result_options' +require_relative 'raw_text' module Mindee module Parsing @@ -10,8 +10,8 @@ module V2 class InferenceResult # @return [Mindee::Parsing::V2::Field::InferenceFields] Fields produced by the model. attr_reader :fields - # @return [Mindee::Parsing::V2::InferenceResultOptions, nil] Optional extra data. - attr_reader :options + # @return [Mindee::Parsing::V2::RawText, nil] Optional extra data. + attr_reader :raw_text # @param server_response [Hash] Hash version of the JSON returned by the API. def initialize(server_response) @@ -19,9 +19,7 @@ def initialize(server_response) @fields = Field::InferenceFields.new(server_response['fields']) - return unless server_response.key?('options') && server_response['options'] - - @options = InferenceResultOptions.new(server_response['options']) + @raw_text = server_response['raw_text'] ? RawText.new(server_response['raw_text']) : nil end # String representation. @@ -32,15 +30,6 @@ def to_s '======', @fields.to_s, ] - - if @options - parts += [ - 'Options', - '=======', - @options.to_s, - ] - end - parts.join("\n") end end diff --git a/lib/mindee/parsing/v2/inference_result_options.rb b/lib/mindee/parsing/v2/inference_result_options.rb deleted file mode 100644 index ba903e7c..00000000 --- a/lib/mindee/parsing/v2/inference_result_options.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require_relative 'raw_text' - -module Mindee - module Parsing - module V2 - # Optional data returned alongside an inference. - class InferenceResultOptions - # @return [Array] Collection of raw texts per page. - attr_reader :raw_texts - - # @param server_response [Hash] Raw JSON parsed into a Hash. - def initialize(server_response) - raw = server_response['raw_texts'] - @raw_texts = raw.is_a?(Array) ? raw.map { |rt| RawText.new(rt) } : [] - end - end - end - end -end diff --git a/lib/mindee/parsing/v2/raw_text.rb b/lib/mindee/parsing/v2/raw_text.rb index 0aac358f..08cfec56 100644 --- a/lib/mindee/parsing/v2/raw_text.rb +++ b/lib/mindee/parsing/v2/raw_text.rb @@ -3,17 +3,17 @@ module Mindee module Parsing module V2 - # Raw text extracted from a given page. + # Raw text extracted from all pages in the document. class RawText - # @return [Integer] Page number where the text was found. - attr_reader :page - # @return [String] Text content. - attr_reader :content + # @return [Array[Mindee::Parsing::V2::RawTextPage]] List of pages with their extracted text content. + attr_reader :pages # @param server_response [Hash] Raw JSON parsed into a Hash. def initialize(server_response) - @page = server_response['page'] - @content = server_response['content'] + @pages = [] + server_response.fetch('pages', []).each do |page| + @pages.push RawTextPage.new(page) + end end end end diff --git a/lib/mindee/parsing/v2/raw_text_page.rb b/lib/mindee/parsing/v2/raw_text_page.rb new file mode 100644 index 00000000..ad004b81 --- /dev/null +++ b/lib/mindee/parsing/v2/raw_text_page.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Mindee + module Parsing + module V2 + # Raw text extracted from a single page. + class RawTextPage + # @return [Boolean] Text content of the page as a single string. '\n' is used to separate lines. + attr_reader :content + + # @param server_response [Hash] Raw JSON parsed into a Hash. + def initialize(server_response) + @content = server_response['content'] + end + end + end + end +end diff --git a/sig/mindee/parsing/v2/inference.rbs b/sig/mindee/parsing/v2/inference.rbs index 0d4387f8..bc11c212 100644 --- a/sig/mindee/parsing/v2/inference.rbs +++ b/sig/mindee/parsing/v2/inference.rbs @@ -3,9 +3,10 @@ module Mindee module Parsing module V2 class Inference - attr_reader file: InferenceFile attr_reader id: String attr_reader model: InferenceModel + attr_reader file: InferenceFile + attr_reader active_options: InferenceActiveOptions attr_reader result: InferenceResult def initialize: (Hash[String | Symbol, untyped]) -> void diff --git a/sig/mindee/parsing/v2/inference_active_options.rbs b/sig/mindee/parsing/v2/inference_active_options.rbs new file mode 100644 index 00000000..6ef779bf --- /dev/null +++ b/sig/mindee/parsing/v2/inference_active_options.rbs @@ -0,0 +1,14 @@ +module Mindee + module Parsing + module V2 + class InferenceActiveOptions + attr_reader confidence: bool + attr_reader polygon: bool + attr_reader rag: bool + attr_reader raw_text: bool + + def initialize: (Hash[String | Symbol, untyped]) -> void + end + end + end +end diff --git a/sig/mindee/parsing/v2/inference_result.rbs b/sig/mindee/parsing/v2/inference_result.rbs index b1b6bedc..5bc3f083 100644 --- a/sig/mindee/parsing/v2/inference_result.rbs +++ b/sig/mindee/parsing/v2/inference_result.rbs @@ -4,7 +4,8 @@ module Mindee module V2 class InferenceResult attr_reader fields: Field::InferenceFields - attr_reader options: InferenceResultOptions? + attr_reader raw_text: RawText? + def initialize: (Hash[String | Symbol, untyped]) -> void def to_s: -> String end diff --git a/sig/mindee/parsing/v2/raw_text.rbs b/sig/mindee/parsing/v2/raw_text.rbs index 0f1ce219..b5959005 100644 --- a/sig/mindee/parsing/v2/raw_text.rbs +++ b/sig/mindee/parsing/v2/raw_text.rbs @@ -3,8 +3,8 @@ module Mindee module Parsing module V2 class RawText - attr_reader page: Integer? - attr_reader content: String? + attr_reader pages: Array[RawTextPage] + def initialize: (Hash[String | Symbol, untyped]) -> void end end diff --git a/sig/mindee/parsing/v2/inference_result_options.rbs b/sig/mindee/parsing/v2/raw_text_page.rbs similarity index 50% rename from sig/mindee/parsing/v2/inference_result_options.rbs rename to sig/mindee/parsing/v2/raw_text_page.rbs index fd289c16..a3579370 100644 --- a/sig/mindee/parsing/v2/inference_result_options.rbs +++ b/sig/mindee/parsing/v2/raw_text_page.rbs @@ -1,9 +1,9 @@ -# lib/mindee/parsing/v2/inference_result_options.rb module Mindee module Parsing module V2 - class InferenceResultOptions - attr_reader raw_texts: Array[RawText] + class RawTextPage + attr_reader content: string + def initialize: (Hash[String | Symbol, untyped]) -> void end end diff --git a/spec/client_v2_integration.rb b/spec/client_v2_integration.rb index 33846bcf..547c2b74 100644 --- a/spec/client_v2_integration.rb +++ b/spec/client_v2_integration.rb @@ -22,19 +22,22 @@ file_alias: 'ruby-integration-test', polling_options: polling) - resp = client.enqueue_and_get_inference(input, params) + response = client.enqueue_and_get_inference(input, params) - expect(resp).not_to be_nil - expect(resp.inference).not_to be_nil + expect(response).not_to be_nil + expect(response.inference).not_to be_nil - expect(resp.inference.file).not_to be_nil - expect(resp.inference.file.name).to eq('multipage_cut-2.pdf') + expect(response.inference.file).not_to be_nil + expect(response.inference.file.name).to eq('multipage_cut-2.pdf') - expect(resp.inference.model).not_to be_nil - expect(resp.inference.model.id).to eq(model_id) + expect(response.inference.model).not_to be_nil + expect(response.inference.model.id).to eq(model_id) - expect(resp.inference.result).not_to be_nil - expect(resp.inference.result.options).to be_nil + expect(response.inference.active_options).not_to be_nil + + expect(response.inference.result).not_to be_nil + expect(response.inference.result.raw_text).to be_nil + expect(response.inference.result.fields).not_to be_nil end it 'parses a filled single-page image successfully' do @@ -45,13 +48,18 @@ rag: false, file_alias: 'ruby-integration-test') - resp = client.enqueue_and_get_inference(input, params) + response = client.enqueue_and_get_inference(input, params) + expect(response).not_to be_nil + + expect(response.inference).not_to be_nil + expect(response.inference.file.name).to eq('default_sample.jpg') + + expect(response.inference.model).not_to be_nil + expect(response.inference.model.id).to eq(model_id) - expect(resp).not_to be_nil - expect(resp.inference.file.name).to eq('default_sample.jpg') - expect(resp.inference.model.id).to eq(model_id) + expect(response.inference.active_options).not_to be_nil - fields = resp.inference.result.fields + fields = response.inference.result.fields expect(fields).not_to be_nil expect(fields['supplier_name']).not_to be_nil expect(fields['supplier_name'].value).to eq('John Smith') @@ -91,9 +99,9 @@ params = Mindee::Input::InferenceParameters.new(model_id) - resp = client.enqueue_and_get_inference(url_input, params) + response = client.enqueue_and_get_inference(url_input, params) - expect(resp).not_to be_nil - expect(resp.inference).not_to be_nil + expect(response).not_to be_nil + expect(response.inference).not_to be_nil end end diff --git a/spec/data b/spec/data index f0175f0e..bc8356c1 160000 --- a/spec/data +++ b/spec/data @@ -1 +1 @@ -Subproject commit f0175f0ee644b57b409e6ad7e1c030f28fbe57ef +Subproject commit bc8356c1ce52d60351ed3430d336f33366025012 diff --git a/spec/parsing/v2/inference_spec.rb b/spec/parsing/v2/inference_spec.rb index 5c1e9108..196253a5 100644 --- a/spec/parsing/v2/inference_spec.rb +++ b/spec/parsing/v2/inference_spec.rb @@ -56,23 +56,30 @@ def load_v2_inference(resource_path) it 'loads a complete inference with valid properties' do response = load_v2_inference(complete_path) - inf = response.inference + inference = response.inference - expect(inf).not_to be_nil - expect(inf.id).to eq('12345678-1234-1234-1234-123456789abc') + expect(inference).not_to be_nil + expect(inference.id).to eq('12345678-1234-1234-1234-123456789abc') - model = inf.model + model = inference.model expect(model).not_to be_nil expect(model.id).to eq('12345678-1234-1234-1234-123456789abc') - file = inf.file + file = inference.file expect(file).not_to be_nil expect(file.name).to eq('complete.jpg') expect(file.file_alias).to be_nil expect(file.page_count).to eq(1) expect(file.mime_type).to eq('image/jpeg') - fields = inf.result.fields + active_options = inference.active_options + expect(active_options).not_to be_nil + expect(active_options.raw_text).to eq(false) + expect(active_options.polygon).to eq(false) + expect(active_options.confidence).to eq(false) + expect(active_options.rag).to eq(false) + + fields = inference.result.fields expect(fields).not_to be_empty expect(fields.size).to eq(21) @@ -129,7 +136,7 @@ def load_v2_inference(resource_path) expect(city_field).to be_a(simple_field) expect(city_field.value).to eq('New York') - expect(inf.result.options).to be_nil + expect(inference.result.raw_text).to be_nil end end @@ -167,6 +174,11 @@ def load_v2_inference(resource_path) describe 'standard field types' do it 'recognizes all field variants' do response = load_v2_inference(standard_field_path) + + active_options = response.inference.active_options + expect(active_options).not_to be_nil + expect(active_options.raw_text).to eq(true) + fields = response.inference.result.fields expect(fields['field_simple_string']).to be_a(simple_field) @@ -193,30 +205,22 @@ def load_v2_inference(resource_path) end end - describe 'options' do + describe 'raw_text' do it 'exposes raw texts' do response = load_v2_inference(raw_text_path) - opts = response.inference.result.options - - expect(opts).not_to be_nil - raw_texts = - if opts.respond_to?(:raw_texts) - opts.raw_texts - elsif opts.respond_to?(:get_raw_texts) - opts.get_raw_texts - else - [] - end - expect(raw_texts).to be_a(Array) - expect(raw_texts.length).to eq(2) + active_options = response.inference.active_options + expect(active_options).not_to be_nil + expect(active_options.raw_text).to eq(true) - first = raw_texts.first - page = first.respond_to?(:page) ? first.page : first[:page] - content = first.respond_to?(:content) ? first.content : first[:content] + raw_text = response.inference.result.raw_text + expect(raw_text).not_to be_nil + expect(raw_text).to be_a(Mindee::Parsing::V2::RawText) - expect(page).to eq(0) - expect(content).to eq('This is the raw text of the first page...') + expect(raw_text.pages.length).to eq(2) + first = raw_text.pages.first + expect(first).to be_a(Mindee::Parsing::V2::RawTextPage) + expect(first.content).to eq('This is the raw text of the first page...') end end