generate_image 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 462b48a0798e5e6d50bffb2386d428b69d43b2c547a0e1bcc7ef01e1ada2fdb3
4
- data.tar.gz: 9f6bf6aac0c92744f538aee95c82a64dab88200370891918a6406c77cb047e93
3
+ metadata.gz: 2de351fd26b27b162f1cadf99e8bebec19eab834a7326add766aa99494ef72d1
4
+ data.tar.gz: 7fd43c333077f86d8fef116b80b6868ea7da18b1d22ba82b7c43c77a211ea14a
5
5
  SHA512:
6
- metadata.gz: 3682a51b886ecf2808b6f826bb30243e0b08a2628e93b6011c241fa201ab8abaaecc2195f9f5e46423a347330dbab8286e28fd3795417fb2661c0cf23c939f46
7
- data.tar.gz: 5426a9c7093f85c6078f9d6d3deaab45253ca5d538dabe5ffd01c53317e8c452e623f0b071f693367c29702990b313499023ec047c62513bd599996100f5ded7
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 a simple and easy-to-use 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.
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
- ## Usage
5
- The gem provides a generate_image method, which takes a text argument and returns the generated image URL as a hash. The method makes a request to the DALL-E API to generate an image based on the provided text.
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
- 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 return a hash with an error key and a message.
13
+ Or install it directly by running:
8
14
 
9
- ## Setting the API Key
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 you can use the generate_image method, you must set your OpenAI API key as an environment variable named DALL_E_API_KEY.
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
- Here's an example of how you can set the environment variable
21
+ ### Examples
14
22
 
15
- DALL_E_API_KEY=your_api_key
23
+ require 'generate_image'
16
24
 
17
- ## Example Usage
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
- ## Installation
27
- ### Add this line to your application's Gemfile:
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
- gem 'generate_image'
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
- bundle install
33
- ### Or install it directly by running:
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
- ### To release a new version, update the version number in version.rb and run:
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.
@@ -1,3 +1,3 @@
1
1
  module GenerateImage
2
- VERSION = "0.1.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,24 +2,35 @@ require 'net/http'
2
2
  require 'json'
3
3
 
4
4
  module GenerateImage
5
+ class RequestFailed < StandardError; end
6
+
5
7
  class << self
6
8
  API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
7
- MODEL_NAME = 'image-alpha-001'
9
+ IMAGE_MODEL_NAME = 'image-alpha-001'
10
+ TEXT_MODEL_NAME = 'text-davinci-002'
11
+ API_KEY = ENV['DALL_E_API_KEY']
8
12
 
9
- def generate_image(text)
10
- api_key = ENV['DALL_E_API_KEY']
11
- unless api_key
13
+ def generate_image(text, options = {})
14
+ unless API_KEY
12
15
  raise StandardError, "API Key not set"
13
16
  end
14
-
15
17
  uri = URI(API_ENDPOINT)
16
18
  request = Net::HTTP::Post.new(uri)
17
19
  request['Content-Type'] = 'application/json'
18
- request['Authorization'] = "Bearer #{api_key}"
20
+ request['Authorization'] = "Bearer #{API_KEY}"
19
21
  request.body = {
20
- model: MODEL_NAME,
22
+ model: options[:model] || IMAGE_MODEL_NAME,
21
23
  prompt: text,
22
- 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
23
34
  }.to_json
24
35
 
25
36
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
@@ -27,14 +38,16 @@ module GenerateImage
27
38
  end
28
39
 
29
40
  if response.code == '200'
30
- image_url = JSON.parse(response.body)['data'][0]['url']
31
- return { image_url: image_url }
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
32
48
  else
33
- message = "Failed to generate image. Response code: #{response.code}. Response body: #{response.body}"
34
- Rails.logger.error(message) if defined?(Rails)
35
- return { error: message }, 500
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.1.2
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-13 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http