generate_image 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -16
- data/lib/generate_image/version.rb +1 -1
- data/lib/generate_image.rb +11 -3
- 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: 462b48a0798e5e6d50bffb2386d428b69d43b2c547a0e1bcc7ef01e1ada2fdb3
|
4
|
+
data.tar.gz: 9f6bf6aac0c92744f538aee95c82a64dab88200370891918a6406c77cb047e93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3682a51b886ecf2808b6f826bb30243e0b08a2628e93b6011c241fa201ab8abaaecc2195f9f5e46423a347330dbab8286e28fd3795417fb2661c0cf23c939f46
|
7
|
+
data.tar.gz: 5426a9c7093f85c6078f9d6d3deaab45253ca5d538dabe5ffd01c53317e8c452e623f0b071f693367c29702990b313499023ec047c62513bd599996100f5ded7
|
data/README.md
CHANGED
@@ -1,39 +1,52 @@
|
|
1
1
|
# GenerateImage
|
2
|
-
The
|
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
|
-
|
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
|
-
|
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
|
-
|
27
|
+
### Add this line to your application's Gemfile:
|
10
28
|
|
11
29
|
gem 'generate_image'
|
12
|
-
###
|
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
|
-
|
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
|
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
|
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
|
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
|
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.
|
data/lib/generate_image.rb
CHANGED
@@ -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(
|
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:
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2023-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|