Module: RubyLLM::Pollinations::Provider::Images

Included in:
Pollinations
Defined in:
lib/ruby_llm/pollinations/provider/images.rb

Constant Summary collapse

VIDEO_MODELS =
%w[veo seedance seedance-pro grok-video ltx-2].freeze
DEFAULT_IMAGE_MODEL =
'flux'
BASIC_PARAMS =
%i[model width height seed quality].freeze
BOOLEAN_PARAMS =
%i[safe enhance].freeze

Class Method Summary collapse

Class Method Details

.add_video_params(params, options) ⇒ Object



79
80
81
82
83
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 79

def add_video_params(params, options)
  params[:duration] = options[:duration] if options[:duration]
  params[:aspectRatio] = options[:aspect_ratio] if options[:aspect_ratio]
  params[:audio] = options[:audio] if options.key?(:audio)
end

.build_image_params(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 56

def build_image_params(options)
  params = extract_basic_params(options)
  extract_boolean_params(params, options)
  params[:negative_prompt] = options[:negative_prompt] if options[:negative_prompt]
  params[:image] = options[:image] if options[:image]
  params[:nologo] = true
  params[:private] = true
  add_video_params(params, options) if options[:model] && video_model?(options[:model])
  params
end

.extract_basic_params(options) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 67

def extract_basic_params(options)
  BASIC_PARAMS.each_with_object({}) do |key, params|
    params[key] = options[key] if options[key]
  end
end

.extract_boolean_params(params, options) ⇒ Object



73
74
75
76
77
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 73

def extract_boolean_params(params, options)
  BOOLEAN_PARAMS.each do |key|
    params[key] = options[key] if options.key?(key)
  end
end

.extract_mime_type(content_type, is_video) ⇒ Object



94
95
96
97
98
99
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 94

def extract_mime_type(content_type, is_video)
  return 'video/mp4' if is_video && !content_type.include?('video/')
  return content_type.split(';').first.strip unless content_type.empty?

  is_video ? 'video/mp4' : 'image/jpeg'
end

.images_url(prompt, **options) ⇒ Object



14
15
16
17
18
19
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 14

def images_url(prompt, **options)
  encoded_prompt = ERB::Util.url_encode(prompt)
  params = build_image_params(options)
  query_string = params.empty? ? '' : "?#{URI.encode_www_form(params)}"
  "image/#{encoded_prompt}#{query_string}"
end

.parse_image_response(response, model:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 32

def parse_image_response(response, model:)
  content_type = response.headers['content-type'] || ''
  is_video = video_response?(content_type, model)
  mime_type = extract_mime_type(content_type, is_video)
  data = Base64.strict_encode64(response.body)

  RubyLLM::Image.new(
    data: data,
    mime_type: mime_type,
    model_id: model
  )
end

.parse_size(size) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 85

def parse_size(size)
  return [1024, 1024] unless size

  parts = size.to_s.split('x')
  return [1024, 1024] unless parts.length == 2

  [parts[0].to_i, parts[1].to_i]
end

.render_image_payload(prompt, model:, size:, **options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 21

def render_image_payload(prompt, model:, size:, **options)
  width, height = parse_size(size)
  {
    prompt: prompt,
    model: model || DEFAULT_IMAGE_MODEL,
    width: width,
    height: height
  }.merge(options.slice(:image, :seed, :quality, :safe, :enhance, :negative_prompt,
                        :duration, :aspect_ratio, :audio))
end

.video_model?(model) ⇒ Boolean



45
46
47
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 45

def video_model?(model)
  VIDEO_MODELS.include?(model.to_s.downcase)
end

.video_response?(content_type, model) ⇒ Boolean



49
50
51
# File 'lib/ruby_llm/pollinations/provider/images.rb', line 49

def video_response?(content_type, model)
  content_type.include?('video/') || video_model?(model)
end