Skip to content

Instantly share code, notes, and snippets.

@makevoid
Last active November 5, 2022 07:48
Show Gist options
  • Save makevoid/1d1529a15f51aa27be70f20874b37054 to your computer and use it in GitHub Desktop.
Save makevoid/1d1529a15f51aa27be70f20874b37054 to your computer and use it in GitHub Desktop.
Use OpenAI Image API to "draw" a ruby in Ruby
require "json"
require "bundler"
Bundler.require :default
# Gemfile:
# ---
# source "https://rubygems.org"
# gem "excon"
OPENAI_API_KEY = ENV["OPENAI_API_KEY"]
API_ROOT = "https://api.openai.com/v1"
def generate_image(prompt:)
args = {
"prompt" => prompt,
"n" => 1,
"size" => "1024x1024",
}
headers = {
"Authorization" => "Bearer #{OPENAI_API_KEY}",
"Content-Type" => "application/json",
}
response = Excon.post(
"#{API_ROOT}/images/generations",
body: args.to_json,
headers: headers,
)
resp = JSON.parse(response.body)
resp.fetch("data").first.fetch("url")
end
prompt = "one stone, shining stone, multifaceted gem, pyramidal ruby, ruby stone gem top view, light on multifaceted gem faces, gem edges, multicolor, synthwave, red, dark translucent red, color, 4k, hdr, digital art, artstation super trending, incredible detail, hq"
image_url = generate_image prompt: prompt
puts image_url
`open "#{image_url}"` # opens the image on mac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment