generate_image 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +51 -18
- data/lib/generate_image/version.rb +1 -1
- data/lib/generate_image.rb +22 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2de351fd26b27b162f1cadf99e8bebec19eab834a7326add766aa99494ef72d1
|
4
|
+
data.tar.gz: 7fd43c333077f86d8fef116b80b6868ea7da18b1d22ba82b7c43c77a211ea14a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20050a7726574a61d88718d29b0f1fb3dde18968a07cd4191f0f4bfaef9bc41b294378da6464e8456008d1804a0960061457dac6baf1e83f32e4d2be3dcf0fb7
|
7
|
+
data.tar.gz: 4ccd64ccd20b3393f471a0df141427e302a0b59d50e051739624ba7df16c81485845dc07d9cb8c8ea67a90a9890a830ee4e5961e35ba5848874bd2357af9fedf
|
data/README.md
CHANGED
@@ -1,21 +1,31 @@
|
|
1
1
|
# GenerateImage
|
2
|
-
The GenerateImage gem is a Ruby gem that provides
|
2
|
+
The GenerateImage gem is a Ruby gem that provides an interface for generating images using the OpenAI DALL-E API. This gem can be used in Ruby on Rails projects or any other Ruby projects.
|
3
3
|
|
4
|
-
##
|
5
|
-
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'generate_image'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
bundle install
|
6
12
|
|
7
|
-
|
13
|
+
Or install it directly by running:
|
8
14
|
|
9
|
-
|
15
|
+
gem install generate_image
|
16
|
+
## Usage
|
17
|
+
The gem provides a generate_image method, which takes a text argument and returns the generated image URL or image base64 as a hash. The method makes a request to the DALL-E API to generate an image based on the provided text.
|
10
18
|
|
11
|
-
Before
|
19
|
+
Before using the generate_image method, you must set your OpenAI API key as an environment variable named DALL_E_API_KEY. The gem uses the Net::HTTP library to make API requests and includes error handling to ensure successful image generation. In case of any errors, the method will raise a RequestFailed exception.
|
12
20
|
|
13
|
-
|
21
|
+
### Examples
|
14
22
|
|
15
|
-
|
23
|
+
require 'generate_image'
|
16
24
|
|
17
|
-
|
25
|
+
# Set the DALL-E API key
|
26
|
+
ENV['DALL_E_API_KEY'] = 'your_api_key'
|
18
27
|
|
28
|
+
# Generate a single image with default options
|
19
29
|
result = GenerateImage.generate_image('A three-story castle made of ice cream')
|
20
30
|
if result[:error]
|
21
31
|
puts result[:error]
|
@@ -23,16 +33,38 @@ Here's an example of how you can set the environment variable
|
|
23
33
|
puts result[:image_url]
|
24
34
|
end
|
25
35
|
|
26
|
-
|
27
|
-
|
36
|
+
# Generate a single image with custom options
|
37
|
+
result = GenerateImage.generate_image('A cat playing the piano', model: 'image-alpha-001', num_images: 2, size: '1024x1024', response_format: 'base64', quality: 90)
|
38
|
+
if result[:error]
|
39
|
+
puts result[:error]
|
40
|
+
else
|
41
|
+
puts result[:image_base64]
|
42
|
+
end
|
43
|
+
## Options
|
44
|
+
The generate_image method accepts a hash of options to customize the generated images. Here are the available options:
|
28
45
|
|
29
|
-
|
30
|
-
### And then execute:
|
46
|
+
`model` - The name of the model to use for generating the images. Default is `image-alpha-001`.
|
31
47
|
|
32
|
-
|
33
|
-
|
48
|
+
`num_images` - The number of images to generate. Default is `1`.
|
49
|
+
|
50
|
+
`size` - The dimensions of the generated images in the format widthxheight. Default is `512x512`.
|
51
|
+
|
52
|
+
`response_format` - The format of the response, either `url` or `base64`. Default is `url`.
|
53
|
+
|
54
|
+
`style` - The model or style to use for generating the images. Default is `nil`, which uses the default style of the selected model.
|
55
|
+
|
56
|
+
`scale` - The scaling factor for the generated image. Default is `1`.
|
57
|
+
|
58
|
+
`seed` - The random seed to use for the generation process. Default is `nil`.
|
59
|
+
|
60
|
+
`quality` - The JPEG compression quality of the generated image. Default is `80`.
|
61
|
+
|
62
|
+
`text_model` - The name of the model to use for generating text prompts. Default is `text-davinci-002`.
|
63
|
+
|
64
|
+
`text_prompt` - The text prompt to use for generating the image. Default is `nil`.
|
65
|
+
|
66
|
+
`text_length` - The maximum length of the generated text. Default is `nil`.
|
34
67
|
|
35
|
-
gem install generate_image
|
36
68
|
## Development
|
37
69
|
To contribute to the development of this gem, clone the repository and run the following commands to install dependencies and run tests:
|
38
70
|
|
@@ -40,13 +72,14 @@ To contribute to the development of this gem, clone the repository and run the f
|
|
40
72
|
rake spec
|
41
73
|
You can also run bin/console for an interactive prompt to experiment with the code.
|
42
74
|
|
43
|
-
|
75
|
+
To release a new version, update the version number in version.rb and run:
|
44
76
|
|
45
77
|
bundle exec rake release
|
78
|
+
|
46
79
|
This will create a git tag for the new version, push the git commits and tags, and upload the .gem file to RubyGems.org.
|
47
80
|
|
48
81
|
## Contributing
|
49
82
|
Bug reports and pull requests are welcome on the GitHub repository. This project is intended to be a safe and welcoming space for collaboration, and all contributors are expected to adhere to the code of conduct.
|
50
83
|
|
51
84
|
## License
|
52
|
-
The GenerateImage gem is open source software, released under the terms of the MIT License.
|
85
|
+
The GenerateImage gem is open source software, released under the terms of the MIT License.
|
data/lib/generate_image.rb
CHANGED
@@ -5,23 +5,32 @@ module GenerateImage
|
|
5
5
|
class RequestFailed < StandardError; end
|
6
6
|
|
7
7
|
class << self
|
8
|
-
|
8
|
+
API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
|
9
9
|
IMAGE_MODEL_NAME = 'image-alpha-001'
|
10
|
-
|
10
|
+
TEXT_MODEL_NAME = 'text-davinci-002'
|
11
11
|
API_KEY = ENV['DALL_E_API_KEY']
|
12
12
|
|
13
|
-
def generate_image(text)
|
13
|
+
def generate_image(text, options = {})
|
14
14
|
unless API_KEY
|
15
15
|
raise StandardError, "API Key not set"
|
16
16
|
end
|
17
|
-
uri = URI(
|
17
|
+
uri = URI(API_ENDPOINT)
|
18
18
|
request = Net::HTTP::Post.new(uri)
|
19
19
|
request['Content-Type'] = 'application/json'
|
20
20
|
request['Authorization'] = "Bearer #{API_KEY}"
|
21
21
|
request.body = {
|
22
|
-
model: IMAGE_MODEL_NAME,
|
22
|
+
model: options[:model] || IMAGE_MODEL_NAME,
|
23
23
|
prompt: text,
|
24
|
-
num_images: 1
|
24
|
+
num_images: options[:num_images] || 1,
|
25
|
+
size: options[:size] || '512x512',
|
26
|
+
response_format: options[:response_format] || 'url',
|
27
|
+
style: options[:style] || nil,
|
28
|
+
scale: options[:scale] || 1,
|
29
|
+
seed: options[:seed] || nil,
|
30
|
+
quality: options[:quality] || 80,
|
31
|
+
text_model: options[:text_model] || TEXT_MODEL_NAME,
|
32
|
+
text_prompt: options[:text_prompt] || nil,
|
33
|
+
text_length: options[:text_length] || nil
|
25
34
|
}.to_json
|
26
35
|
|
27
36
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
@@ -29,12 +38,16 @@ module GenerateImage
|
|
29
38
|
end
|
30
39
|
|
31
40
|
if response.code == '200'
|
32
|
-
|
33
|
-
|
41
|
+
if options[:response_format] == 'base64'
|
42
|
+
image_base64 = JSON.parse(response.body)['data'][0]['base64']
|
43
|
+
return { image_base64: image_base64 }
|
44
|
+
else
|
45
|
+
image_url = JSON.parse(response.body)['data'][0]['url']
|
46
|
+
return { image_url: image_url }
|
47
|
+
end
|
34
48
|
else
|
35
49
|
raise RequestFailed, "Failed to generate image. Response code: #{response.code}. Response body: #{response.body}"
|
36
50
|
end
|
37
51
|
end
|
38
52
|
end
|
39
53
|
end
|
40
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: generate_image
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AMQOR Merouane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|