Class: StabilitySDK::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/stability_sdk/cli.rb

Class Method Summary collapse

Class Method Details

.save_answer(answer, options, logger) ⇒ Object



3
4
5
6
7
8
9
10
11
12
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
# File 'lib/stability_sdk/cli.rb', line 3

def self.save_answer(answer, options, logger)
  logger.debug "received answer: #{answer}"
  return if options[:no_store]

  allowed_artifact_types = []
  if options.has_key?(:artifact_types)
    allowed_artifact_types = options[:artifact_types].map(&:to_sym)
  end

  answer.artifacts.each_with_index do |artifact, idx|
    filename_base = "#{options[:prefix] || "generation"}-#{answer.request_id}-#{answer.answer_id}-#{idx}"

    filename = ""
    contents = ""

    if allowed_artifact_types.size > 0 && !allowed_artifact_types.include?(artifact.type)
      logger.info "skipping #{artifact.type} due to artifact type filter"
      next
    end

    case artifact.type
    when :ARTIFACT_IMAGE
      ext = MIME::Types[artifact.mime].first.preferred_extension
      filename = "#{filename_base}.#{ext}"
      contents = artifact.binary
    when :ARTIFACT_CLASSIFICATIONS
      ext = "pb.json"
      filename = "#{filename_base}.#{ext}"
      contents = artifact.to_json
    else
      logger.warn "not implemented for ArtifactType #{artifact.type}"
    end

    if artifact.finish_reason == :FILTER
      logger.debug "the generated image is filtered"
    end

    next if filename == "" || contents == ""

    File.open(filename, "wb") do |f|
      f.write(contents)
      logger.debug "wrote #{artifact.type} to #{filename}"
    end
  end
end