generate_image 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c82e0f5d7f1a2831ddf9cbac4eb7072476f7c9c258c3e3f68b79bcea1e17087
4
- data.tar.gz: 591e2f4ee36d58271652eebe77a3bf42111fbfd39de61617fa5c3d65a9465b47
3
+ metadata.gz: 462b48a0798e5e6d50bffb2386d428b69d43b2c547a0e1bcc7ef01e1ada2fdb3
4
+ data.tar.gz: 9f6bf6aac0c92744f538aee95c82a64dab88200370891918a6406c77cb047e93
5
5
  SHA512:
6
- metadata.gz: fa20658a7577013d65fc74cb67a706d34a8294cdec6b8d9566930b1411a4b41a9b6d94ee70b93a4c5d7b17e76fb0f5ed851bc47f2647d3c742400e1db11dba0a
7
- data.tar.gz: 46a369f42d94f244f37de29acb10ac3495209f5a0593eee3cfc3a68c97207f4c217c5838f3c27d565df814a075c9c7a833170e7968276b6a18a833ceb9398ccb
6
+ metadata.gz: 3682a51b886ecf2808b6f826bb30243e0b08a2628e93b6011c241fa201ab8abaaecc2195f9f5e46423a347330dbab8286e28fd3795417fb2661c0cf23c939f46
7
+ data.tar.gz: 5426a9c7093f85c6078f9d6d3deaab45253ca5d538dabe5ffd01c53317e8c452e623f0b071f693367c29702990b313499023ec047c62513bd599996100f5ded7
data/README.md CHANGED
@@ -1,39 +1,52 @@
1
1
  # GenerateImage
2
- The generate_image gem is a Ruby gem that provides a simple and easy-to-use interface for generating images using the DALL-E API. The gem is designed to be used in Ruby on Rails projects, but it can also be used in other Ruby projects.
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.
3
3
 
4
- The gem provides a generate_image method, which takes a text argument and returns the generated image binary data. The method makes a request to the DALL-E API to generate an image based on the text, and returns the image binary data.
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.
5
6
 
6
- The gem uses the Net::HTTP library to make the API request and it requires the API key to be set as an environment variable named DALL_E_API_KEY. The gem also includes error handling to ensure that the image generation is successful and raises an error if there's any issue.
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.
8
+
9
+ ## Setting the API Key
10
+
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.
12
+
13
+ Here's an example of how you can set the environment variable
14
+
15
+ DALL_E_API_KEY=your_api_key
16
+
17
+ ## Example Usage
18
+
19
+ result = GenerateImage.generate_image('A three-story castle made of ice cream')
20
+ if result[:error]
21
+ puts result[:error]
22
+ else
23
+ puts result[:image_url]
24
+ end
7
25
 
8
26
  ## Installation
9
- To install the gem, add this line to your application's Gemfile:
27
+ ### Add this line to your application's Gemfile:
10
28
 
11
29
  gem 'generate_image'
12
- ### Then run:
13
-
30
+ ### And then execute:
31
+
14
32
  bundle install
15
33
  ### Or install it directly by running:
16
34
 
17
-
18
35
  gem install generate_image
19
-
20
36
  ## Development
21
- ### To contribute to the development of this gem, check out the repo and run the following commands to install dependencies and run the tests:
37
+ To contribute to the development of this gem, clone the repository and run the following commands to install dependencies and run tests:
22
38
 
23
39
  bin/setup
24
40
  rake spec
25
41
  You can also run bin/console for an interactive prompt to experiment with the code.
26
42
 
27
- ### To release a new version, update the version number in version.rb, then run:
43
+ ### To release a new version, update the version number in version.rb and run:
28
44
 
29
45
  bundle exec rake release
30
- This will create a git tag for the new version, push the git commits and tags, and upload the .gem file to rubygems.org.
46
+ This will create a git tag for the new version, push the git commits and tags, and upload the .gem file to RubyGems.org.
31
47
 
32
48
  ## Contributing
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/merouaneamqor/generate_image. This project is intended to be a safe, welcoming space for collaboration, and all contributors are expected to adhere to the code of conduct.
49
+ 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.
34
50
 
35
51
  ## License
36
- The gem is available as open source under the terms of the MIT License.
37
-
38
- ## Code of Conduct
39
- Everyone interacting with the project is expected to follow the code of conduct.
52
+ 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.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -3,15 +3,21 @@ require 'json'
3
3
 
4
4
  module GenerateImage
5
5
  class << self
6
+ API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
7
+ MODEL_NAME = 'image-alpha-001'
8
+
6
9
  def generate_image(text)
7
10
  api_key = ENV['DALL_E_API_KEY']
11
+ unless api_key
12
+ raise StandardError, "API Key not set"
13
+ end
8
14
 
9
- uri = URI('https://api.openai.com/v1/images/generations')
15
+ uri = URI(API_ENDPOINT)
10
16
  request = Net::HTTP::Post.new(uri)
11
17
  request['Content-Type'] = 'application/json'
12
18
  request['Authorization'] = "Bearer #{api_key}"
13
19
  request.body = {
14
- model: 'image-alpha-001',
20
+ model: MODEL_NAME,
15
21
  prompt: text,
16
22
  num_images: 1
17
23
  }.to_json
@@ -24,7 +30,9 @@ module GenerateImage
24
30
  image_url = JSON.parse(response.body)['data'][0]['url']
25
31
  return { image_url: image_url }
26
32
  else
27
- return { error: "Failed to generate image" }, 500
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
28
36
  end
29
37
  end
30
38
  end
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.1
4
+ version: 0.1.2
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-12 00:00:00.000000000 Z
11
+ date: 2023-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http