generate_image 0.1.1 → 0.1.3

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: 9c82e0f5d7f1a2831ddf9cbac4eb7072476f7c9c258c3e3f68b79bcea1e17087
4
- data.tar.gz: 591e2f4ee36d58271652eebe77a3bf42111fbfd39de61617fa5c3d65a9465b47
3
+ metadata.gz: a729f8fda3aa05792e49f221a8d94d471185f0fba92dbb6d76fc5ea92767a6b5
4
+ data.tar.gz: c9df8fe206dd5e467085640f253b6bae608e89ec20c5c6ccfdbda2ff60d44684
5
5
  SHA512:
6
- metadata.gz: fa20658a7577013d65fc74cb67a706d34a8294cdec6b8d9566930b1411a4b41a9b6d94ee70b93a4c5d7b17e76fb0f5ed851bc47f2647d3c742400e1db11dba0a
7
- data.tar.gz: 46a369f42d94f244f37de29acb10ac3495209f5a0593eee3cfc3a68c97207f4c217c5838f3c27d565df814a075c9c7a833170e7968276b6a18a833ceb9398ccb
6
+ metadata.gz: 92969cfef020fc0c2c3c3c50b9c62489d12206dfaf6f68fd1d565388580866cf9f732751b74b93c0eacb6453e6e35ee1c82b79ca4a3847fc05b28482d079f699
7
+ data.tar.gz: c0d3971e217207be1043dd081a979b2cd669813e1b217bd83214dc8a1dcec29be8bda115562b05005bc3db915a2622fa7e5c26c3811c9a44859585980e334195
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.3"
3
3
  end
@@ -2,16 +2,24 @@ require 'net/http'
2
2
  require 'json'
3
3
 
4
4
  module GenerateImage
5
+ class RequestFailed < StandardError; end
6
+
5
7
  class << self
6
- def generate_image(text)
7
- api_key = ENV['DALL_E_API_KEY']
8
+ IMAGE_API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
9
+ IMAGE_MODEL_NAME = 'image-alpha-001'
10
+ REWRITE_MODEL_NAME = 'text-davinci-002'
11
+ API_KEY = ENV['DALL_E_API_KEY']
8
12
 
9
- uri = URI('https://api.openai.com/v1/images/generations')
13
+ def generate_image(text)
14
+ unless API_KEY
15
+ raise StandardError, "API Key not set"
16
+ end
17
+ uri = URI(IMAGE_API_ENDPOINT)
10
18
  request = Net::HTTP::Post.new(uri)
11
19
  request['Content-Type'] = 'application/json'
12
- request['Authorization'] = "Bearer #{api_key}"
20
+ request['Authorization'] = "Bearer #{API_KEY}"
13
21
  request.body = {
14
- model: 'image-alpha-001',
22
+ model: IMAGE_MODEL_NAME,
15
23
  prompt: text,
16
24
  num_images: 1
17
25
  }.to_json
@@ -24,7 +32,7 @@ module GenerateImage
24
32
  image_url = JSON.parse(response.body)['data'][0]['url']
25
33
  return { image_url: image_url }
26
34
  else
27
- return { error: "Failed to generate image" }, 500
35
+ raise RequestFailed, "Failed to generate image. Response code: #{response.code}. Response body: #{response.body}"
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.3
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