Skip to content

Commit

Permalink
Merge pull request #130 from alexrudall/dalle-edit
Browse files Browse the repository at this point in the history
Add DALL·E Image Edit and Variations
  • Loading branch information
alexrudall authored Dec 23, 2022
2 parents 5ca3f55 + c85ef87 commit 5d8c88c
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 2 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ This fine-tuned model name can then be used in classifications:
JSON.parse(response.body)["choices"].map { |c| c["text"] }
```

### Images
### Image Generation

Generate an image using DALL·E!

Expand All @@ -209,7 +209,32 @@ Generate an image using DALL·E!
=> "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
```

![Otter Chef](https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhhQPMiIQ0Es8OwrH/user-jxM65ijkZc1qRfHC0IJ8mOIc/img-UrDvFC4tDnuhTieF7TrTJ2gq.png?st=2022-11-13T15%3A55%3A34Z&se=2022-11-13T17%3A55%3A34Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2022-11-13T01%3A32%3A30Z&ske=2022-11-14T01%3A32%3A30Z&sks=b&skv=2021-08-06&sig=tLdggckHl20CnnpCleoeiAEQjy4zMjuZJiUdovmkoF0%3D)
![Ruby](https://ibb.co/qDRNFpf)

### Image Edit

Fill in the transparent part of an image, or upload a mask with transparent sections to indicate the parts of an image that can be changed according to your prompt...

```ruby
response = client.images.edit(parameters: { prompt: "A solid red Ruby on a blue background", image: "image.png", mask: "mask.png" })
puts response.dig("data", 0, "url")
=> "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
```

![Ruby](https://ibb.co/LNZMpGs)

### Image Variations

Create n variations of an image.

```ruby
response = client.images.variations(parameters: { image: "image.png", n: 2 })
puts response.dig("data", 0, "url")
=> "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
```

![Ruby](https://ibb.co/0s1tY25)
![Ruby](https://ibb.co/QkM27tM)

### Moderations

Expand Down
34 changes: 34 additions & 0 deletions lib/ruby/openai/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,44 @@ def generate(version: default_version, parameters: {})
)
end

def edit(version: default_version, parameters: {})
parameters = open_files(parameters)

self.class.post(
"/#{version}/images/edits",
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}",
"OpenAI-Organization" => @organization_id
},
body: parameters
)
end

def variations(version: default_version, parameters: {})
parameters = open_files(parameters)

self.class.post(
"/#{version}/images/variations",
headers: {
"Content-Type" => "application/json",
"Authorization" => "Bearer #{@access_token}",
"OpenAI-Organization" => @organization_id
},
body: parameters
)
end

private

def default_version
"v1".freeze
end

def open_files(parameters)
parameters = parameters.merge(image: File.open(parameters[:image]))
parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
parameters
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions spec/fixtures/cassettes/images_variations_image_png.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added spec/fixtures/files/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/files/mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions spec/ruby/openai/client/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,55 @@
end
end
end

describe "#edit", :vcr do
let(:response) do
OpenAI::Client.new.images.edit(
parameters: {
image: image,
mask: mask,
prompt: prompt,
size: size
}
)
end
let(:cassette) { "images edit #{image_filename} #{prompt}" }
let(:prompt) { "A solid red Ruby on a blue background" }
let(:image) { File.join(RSPEC_ROOT, "fixtures/files", image_filename) }
let(:image_filename) { "image.png" }
let(:mask) { File.join(RSPEC_ROOT, "fixtures/files", mask_filename) }
let(:mask_filename) { "mask.png" }
let(:size) { "256x256" }

it "succeeds" do
VCR.use_cassette(cassette, preserve_exact_body_bytes: true) do
r = JSON.parse(response.body)
expect(r.dig("data", 0, "url")).to include("dalle")
end
end
end

describe "#variations", :vcr do
let(:response) do
OpenAI::Client.new.images.variations(
parameters: {
image: image,
n: 2,
size: size
}
)
end
let(:cassette) { "images variations #{image_filename}" }
let(:image) { File.join(RSPEC_ROOT, "fixtures/files", image_filename) }
let(:image_filename) { "image.png" }
let(:size) { "256x256" }

it "succeeds" do
VCR.use_cassette(cassette, preserve_exact_body_bytes: true) do
r = JSON.parse(response.body)
expect(r.dig("data", 0, "url")).to include("dalle")
end
end
end
end
end

0 comments on commit 5d8c88c

Please sign in to comment.