Class: OpenAI::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/open_ai/image.rb

Constant Summary collapse

BASE_TOKENS =
85
TILE_TOKENS =
170
TILE_SIZE =
512

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, base64) ⇒ Image

Returns a new instance of Image.



21
22
23
24
25
# File 'lib/open_ai/image.rb', line 21

def initialize(width, height, base64)
  @width = width
  @height = height
  @base64 = base64
end

Instance Attribute Details

#base64Object

Returns the value of attribute base64.



19
20
21
# File 'lib/open_ai/image.rb', line 19

def base64
  @base64
end

#heightObject

Returns the value of attribute height.



19
20
21
# File 'lib/open_ai/image.rb', line 19

def height
  @height
end

#widthObject

Returns the value of attribute width.



19
20
21
# File 'lib/open_ai/image.rb', line 19

def width
  @width
end

Class Method Details

.from_tg_photo(file, model:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/open_ai/image.rb', line 7

def self.from_tg_photo(file, model:)
  return unless file
  return unless model.has_vision?

  base64 = Base64.encode64(file.read)
  size = `identify -format "%w %h" ./#{file.original_filename}`
  width, height = size.split(" ")
  FileUtils.rm_rf("./#{file.original_filename}")

  new(width, height, base64)
end

Instance Method Details

#tiles(pixels) ⇒ Object



34
35
36
# File 'lib/open_ai/image.rb', line 34

def tiles(pixels)
  (pixels.to_f / TILE_SIZE).ceil
end

#tokensObject



27
28
29
30
31
32
# File 'lib/open_ai/image.rb', line 27

def tokens
  @tokens ||= begin
    tiles = tiles(width) * tiles(height)
    (tiles * TILE_TOKENS) + BASE_TOKENS
  end
end