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 +4 -4
- data/README.md +29 -16
- data/lib/generate_image/version.rb +1 -1
- data/lib/generate_image.rb +14 -6
- 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: a729f8fda3aa05792e49f221a8d94d471185f0fba92dbb6d76fc5ea92767a6b5
|
4
|
+
data.tar.gz: c9df8fe206dd5e467085640f253b6bae608e89ec20c5c6ccfdbda2ff60d44684
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92969cfef020fc0c2c3c3c50b9c62489d12206dfaf6f68fd1d565388580866cf9f732751b74b93c0eacb6453e6e35ee1c82b79ca4a3847fc05b28482d079f699
|
7
|
+
data.tar.gz: c0d3971e217207be1043dd081a979b2cd669813e1b217bd83214dc8a1dcec29be8bda115562b05005bc3db915a2622fa7e5c26c3811c9a44859585980e334195
|
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
@@ -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
|
-
|
7
|
-
|
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
|
-
|
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 #{
|
20
|
+
request['Authorization'] = "Bearer #{API_KEY}"
|
13
21
|
request.body = {
|
14
|
-
model:
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2023-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|