Class: Lara::Images

Inherits:
Object
  • Object
show all
Defined in:
lib/lara/images.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Images

Returns a new instance of Images.



5
6
7
# File 'lib/lara/images.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#translate(file_path:, target:, source: nil, adapt_to: nil, glossaries: nil, style: nil, text_removal: nil, no_trace: false) ⇒ String

Translates an image and returns the translated image as binary data.

Parameters:

  • file_path (String)

    Path to the image file.

  • target (String)

    Target language code.

  • source (String, nil) (defaults to: nil)

    Source language code (nil for auto-detection).

  • adapt_to (Array<String>, nil) (defaults to: nil)

    Memory IDs for translation adaptation.

  • glossaries (Array<String>, nil) (defaults to: nil)

    Glossary IDs to apply.

  • style (String, nil) (defaults to: nil)

    Translation style (“faithful”, “fluid”, “creative”).

  • text_removal (String, nil) (defaults to: nil)

    Text removal method (“overlay” or “inpainting”).

  • no_trace (Boolean) (defaults to: false)

    If true, disables request tracing.

Returns:

  • (String)

    Binary image data of the translated image.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lara/images.rb', line 19

def translate(file_path:, target:, source: nil, adapt_to: nil, glossaries: nil,
              style: nil, text_removal: nil, no_trace: false)
  image_upload = Faraday::Multipart::FilePart.new(file_path, mime_type_for(file_path))

  body = {
    source: source,
    target: target,
    adapt_to: adapt_to&.to_json,
    glossaries: glossaries&.to_json,
    style: style,
    text_removal: text_removal
  }.compact

  headers = {}
  headers["X-No-Trace"] = "true" if no_trace

  @client.post("/v2/images/translate", body: body, files: { image: image_upload },
                                       headers: headers, raw_response: true)
end

#translate_text(file_path:, target:, source: nil, adapt_to: nil, glossaries: nil, style: nil, verbose: false, no_trace: false) ⇒ Lara::Models::ImageTextResult

Extracts and translates text from an image.

Parameters:

  • file_path (String)

    Path to the image file.

  • target (String)

    Target language code.

  • source (String, nil) (defaults to: nil)

    Source language code (nil for auto-detection).

  • adapt_to (Array<String>, nil) (defaults to: nil)

    Memory IDs for translation adaptation.

  • glossaries (Array<String>, nil) (defaults to: nil)

    Glossary IDs to apply.

  • style (String, nil) (defaults to: nil)

    Translation style (“faithful”, “fluid”, “creative”).

  • verbose (Boolean) (defaults to: false)

    If true, includes match details in the response.

  • no_trace (Boolean) (defaults to: false)

    If true, disables request tracing.

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lara/images.rb', line 49

def translate_text(file_path:, target:, source: nil, adapt_to: nil, glossaries: nil,
                   style: nil, verbose: false, no_trace: false)
  image_upload = Faraday::Multipart::FilePart.new(file_path, mime_type_for(file_path))

  body = {
    source: source,
    target: target,
    adapt_to: adapt_to&.to_json,
    glossaries: glossaries&.to_json,
    style: style,
    verbose: verbose.to_s
  }.compact

  headers = {}
  headers["X-No-Trace"] = "true" if no_trace

  result = @client.post("/v2/images/translate-text", body: body, files: { image: image_upload },
                                                     headers: headers)
  Lara::Models::ImageTextResult.from_hash(result)
end