Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/mindee/parsing/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
14 changes: 8 additions & 6 deletions lib/mindee/parsing/v2/inference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
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)
raise ArgumentError, 'server_response must be a Hash' unless server_response.is_a?(Hash)

@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']
Expand All @@ -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")
Expand Down
42 changes: 42 additions & 0 deletions lib/mindee/parsing/v2/inference_active_options.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/mindee/parsing/v2/inference_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 4 additions & 15 deletions lib/mindee/parsing/v2/inference_result.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,18 +10,16 @@ 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)
raise ArgumentError, 'server_response must be a Hash' unless server_response.is_a?(Hash)

@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.
Expand All @@ -32,15 +30,6 @@ def to_s
'======',
@fields.to_s,
]

if @options
parts += [
'Options',
'=======',
@options.to_s,
]
end

parts.join("\n")
end
end
Expand Down
21 changes: 0 additions & 21 deletions lib/mindee/parsing/v2/inference_result_options.rb

This file was deleted.

14 changes: 7 additions & 7 deletions lib/mindee/parsing/v2/raw_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/mindee/parsing/v2/raw_text_page.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion sig/mindee/parsing/v2/inference.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions sig/mindee/parsing/v2/inference_active_options.rbs
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion sig/mindee/parsing/v2/inference_result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sig/mindee/parsing/v2/raw_text.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
42 changes: 25 additions & 17 deletions spec/client_v2_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
Loading