generate_image 1.0.2 → 1.1.0
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 +4 -4
- data/lib/generate_image/version.rb +1 -1
- data/lib/generate_image.rb +43 -20
- 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: e0cf6f92bb8a3dc9f268736bc22cae4cfec8cd0af5a4e193e383feb156a1d2ac
|
4
|
+
data.tar.gz: 78735582f957597a69428939cc39f9a857583443c6f903927b4b449a3e7427b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25a102011cffbda0cec595234e51bccfc9593022362ae0d9d1cd44f0744ac1aed1c0d591921abe8270f7e655899e289fc2c7dae2cb0c03a548db35223fcc9ec2
|
7
|
+
data.tar.gz: 2bab3bbcd46182d4f2487a6a13f7fe5d74537b512b036ab958ac1d648a2190219c35129f1bc3df005ed9e56506bf345083c3f7925a6781116174a9a1d3977ae1
|
data/lib/generate_image.rb
CHANGED
@@ -1,33 +1,56 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
1
3
|
module GenerateImage
|
2
|
-
class RequestFailed < StandardError
|
4
|
+
class RequestFailed < StandardError
|
5
|
+
def initialize(message = "Request failed")
|
6
|
+
super(message)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class MissingApiKey < RequestFailed
|
11
|
+
def initialize(message = "API key not set")
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
end
|
3
15
|
|
4
16
|
class << self
|
5
17
|
API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
|
6
|
-
IMAGE_MODEL_NAME = 'image-alpha-001'
|
7
|
-
TEXT_MODEL_NAME = 'text-davinci-002'
|
8
18
|
API_KEY = ENV['DALL_E_API_KEY']
|
19
|
+
DEFAULT_OPTIONS = {
|
20
|
+
model: 'image-alpha-001',
|
21
|
+
num_images: 1,
|
22
|
+
size: '1024x1024',
|
23
|
+
response_format: 'url',
|
24
|
+
style: nil,
|
25
|
+
scale: 1,
|
26
|
+
seed: nil,
|
27
|
+
quality: 100,
|
28
|
+
text_model: 'text-davinci-002',
|
29
|
+
text_prompt: nil,
|
30
|
+
text_length: nil
|
31
|
+
}
|
9
32
|
|
10
33
|
def generate_image(text, options = {})
|
11
|
-
|
12
|
-
|
13
|
-
|
34
|
+
options = DEFAULT_OPTIONS.merge(options)
|
35
|
+
raise MissingApiKey if API_KEY.nil?
|
36
|
+
|
14
37
|
uri = URI(API_ENDPOINT)
|
15
38
|
request = Net::HTTP::Post.new(uri)
|
16
39
|
request['Content-Type'] = 'application/json'
|
17
40
|
request['Authorization'] = "Bearer #{API_KEY}"
|
18
41
|
request.body = {
|
19
|
-
model: options[:model]
|
42
|
+
model: options[:model],
|
20
43
|
prompt: text,
|
21
|
-
num_images: options[:num_images]
|
22
|
-
size: options[:size]
|
23
|
-
response_format: options[:response_format]
|
24
|
-
style: options[:style]
|
25
|
-
scale: options[:scale]
|
26
|
-
seed: options[:seed]
|
27
|
-
quality: options[:quality]
|
28
|
-
text_model: options[:text_model]
|
29
|
-
text_prompt: options[:text_prompt]
|
30
|
-
text_length: options[:text_length]
|
44
|
+
num_images: options[:num_images],
|
45
|
+
size: options[:size],
|
46
|
+
response_format: options[:response_format],
|
47
|
+
style: options[:style],
|
48
|
+
scale: options[:scale],
|
49
|
+
seed: options[:seed],
|
50
|
+
quality: options[:quality],
|
51
|
+
text_model: options[:text_model],
|
52
|
+
text_prompt: options[:text_prompt],
|
53
|
+
text_length: options[:text_length]
|
31
54
|
}.to_json
|
32
55
|
|
33
56
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
@@ -37,13 +60,13 @@ module GenerateImage
|
|
37
60
|
if response.code == '200'
|
38
61
|
if options[:response_format] == 'base64'
|
39
62
|
image_base64 = JSON.parse(response.body)['data'][0]['base64']
|
40
|
-
|
63
|
+
{ image_base64: image_base64 }
|
41
64
|
else
|
42
65
|
image_url = JSON.parse(response.body)['data'][0]['url']
|
43
|
-
|
66
|
+
{ image_url: image_url }
|
44
67
|
end
|
45
68
|
else
|
46
|
-
raise RequestFailed
|
69
|
+
raise RequestFailed.new("Failed to generate image. Response code: #{response.code}. Response body: #{response.body}")
|
47
70
|
end
|
48
71
|
end
|
49
72
|
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: 1.0
|
4
|
+
version: 1.1.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-
|
11
|
+
date: 2023-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|