Class: OxAiWorkers::Tool::Pixels

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper, LoadI18n, OxAiWorkers::ToolDefinition
Defined in:
lib/oxaiworkers/tool/pixels.rb

Instance Attribute Summary collapse

Attributes included from LoadI18n

#locale

Attributes included from OxAiWorkers::ToolDefinition

#white_list

Instance Method Summary collapse

Methods included from LoadI18n

#store_locale, #with_locale

Methods included from DependencyHelper

#depends_on

Methods included from OxAiWorkers::ToolDefinition

#define_function, #full_function_name, #function_schemas, #init_white_list_with, #tool_name

Constructor Details

#initialize(worker:, current_dir: nil, only: nil) ⇒ Pixels

Returns a new instance of Pixels.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oxaiworkers/tool/pixels.rb', line 13

def initialize(worker:, current_dir: nil, only: nil)
  store_locale

  init_white_list_with only

  define_function :generate_image, description: I18n.t('oxaiworkers.tool.pixels.generate_image.description') do
    property :prompt, type: 'string', description: I18n.t('oxaiworkers.tool.pixels.generate_image.prompt'),
                      required: true
    if worker.sizes.length > 1
      property :size, type: 'string', description: I18n.t('oxaiworkers.tool.pixels.generate_image.size'),
                      enum: worker.sizes, required: false
    end
    if current_dir.present?
      property :file_name, type: 'string',
                           description: I18n.t('oxaiworkers.tool.pixels.generate_image.file_name'),
                           required: true
    end
    if worker.qualities.length > 1
      property :quality, type: 'string', description: I18n.t('oxaiworkers.tool.pixels.generate_image.quality'),
                         enum: worker.qualities, required: false
    end
  end

  # define_function :edit_image, description: I18n.t('oxaiworkers.tool.pixels.edit_image.description') do
  #   property :input_image, type: 'string', description: I18n.t('oxaiworkers.tool.pixels.edit_image.input_image'),
  #                          required: true
  #   property :prompt, type: 'string', description: I18n.t('oxaiworkers.tool.pixels.edit_image.prompt'),
  #                     required: true
  #   if current_dir.present?
  #     property :output_file_name, type: 'string',
  #                                 description: I18n.t('oxaiworkers.tool.pixels.generate_image.file_name'),
  #                                 required: true
  #   end
  # end

  @worker = worker
  @current_dir = current_dir
end

Instance Attribute Details

#current_dirObject

Returns the value of attribute current_dir.



11
12
13
# File 'lib/oxaiworkers/tool/pixels.rb', line 11

def current_dir
  @current_dir
end

#workerObject

Returns the value of attribute worker.



11
12
13
# File 'lib/oxaiworkers/tool/pixels.rb', line 11

def worker
  @worker
end

Instance Method Details

#generate_image(prompt:, file_name: nil, size: nil, quality: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oxaiworkers/tool/pixels.rb', line 52

def generate_image(prompt:, file_name: nil, size: nil, quality: nil)
  binary = @worker.generate(prompt:, size:, quality:)

  if file_name.present?
    path = save_generated_image(file_name:, binary:)
    "Successfully generated image. file_name: #{path}\n\n#{@worker.result}"
  elsif @worker.result.present?
    @worker.result
  else
    'file_name not set for OxAiWorkers::Tool::Pixels. Please set file name first.'
  end
end

#save_generated_image(file_name:, binary:) ⇒ Object

def edit_image(input_image:, prompt:, output_file_name: nil, size: nil, mask: nil)

TODO: Implement edit_image

end



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oxaiworkers/tool/pixels.rb', line 69

def save_generated_image(file_name:, binary:)
  unless @current_dir.present?
    return 'Current directory not set for OxAiWorkers::Tool::Pixels. Please set current directory first.'
  end

  return 'File name not set for OxAiWorkers::Tool::Pixels. Please set file name first.' unless file_name.present?

  # Ensure current_dir exists
  FileUtils.mkdir_p(@current_dir) unless Dir.exist?(@current_dir)

  path = File.join(@current_dir, file_name)

  File.open(path, 'wb') do |file|
    file.write(binary)
  end

  puts "Successfully saved image. file_name: #{path}"

  file_name
end