generate_image 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generate_image/version.rb +1 -1
- data/lib/generate_image.rb +25 -44
- 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: 00f69d4b0c88b6e09af4a1cdd8b766041c0d014a4777368b791fb9acc6af82ec
|
4
|
+
data.tar.gz: d78c09e191db7a1be962f4b698c4682f10cc24108cb1309770fbdf5d96918df8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 662d9656c7d83a67633dbea6995502729ff238f393d0924dccecebdd6f89abab3f6f2fadc810327a804c663792f431b7b98839135941b420049936604f1cbcae
|
7
|
+
data.tar.gz: f9b12463cd29f9ca602ef7e0d742aa93ca5a74431ad8e8a4ec7a5d877d7b0f998bca2d0096874aafc144861efa283555d4eef63aa42032b9d6f6ad60e990d782
|
data/lib/generate_image.rb
CHANGED
@@ -1,56 +1,36 @@
|
|
1
1
|
require 'net/http'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module GenerateImage
|
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
|
5
|
+
class RequestFailed < StandardError; end
|
15
6
|
|
16
|
-
class
|
7
|
+
class Client
|
17
8
|
API_ENDPOINT = 'https://api.openai.com/v1/images/generations'
|
18
|
-
|
19
|
-
|
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
|
+
IMAGE_MODEL_NAME = 'image-alpha-001'
|
10
|
+
TEXT_MODEL_NAME = 'text-davinci-002'
|
32
11
|
|
33
|
-
|
34
|
-
|
35
|
-
|
12
|
+
attr_accessor :api_key
|
13
|
+
|
14
|
+
def initialize(api_key = nil)
|
15
|
+
@api_key = api_key || ENV['DALL_E_API_KEY']
|
16
|
+
raise StandardError, "API Key not set" unless @api_key
|
17
|
+
end
|
36
18
|
|
19
|
+
def generate_image(text, options = {})
|
37
20
|
uri = URI(API_ENDPOINT)
|
38
21
|
request = Net::HTTP::Post.new(uri)
|
39
22
|
request['Content-Type'] = 'application/json'
|
40
|
-
request['Authorization'] = "Bearer #{
|
23
|
+
request['Authorization'] = "Bearer #{@api_key}"
|
41
24
|
request.body = {
|
42
|
-
model: options[:model],
|
25
|
+
model: options[:model] || IMAGE_MODEL_NAME,
|
43
26
|
prompt: text,
|
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]
|
27
|
+
num_images: options[:num_images] || 1,
|
28
|
+
size: options[:size] || '512x512',
|
29
|
+
response_format: options[:response_format] || 'url',
|
30
|
+
style: options[:style] || nil,
|
31
|
+
scale: options[:scale] || 1,
|
32
|
+
seed: options[:seed] || nil,
|
33
|
+
quality: options[:quality] || 80
|
54
34
|
}.to_json
|
55
35
|
|
56
36
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
@@ -60,14 +40,15 @@ module GenerateImage
|
|
60
40
|
if response.code == '200'
|
61
41
|
if options[:response_format] == 'base64'
|
62
42
|
image_base64 = JSON.parse(response.body)['data'][0]['base64']
|
63
|
-
{ image_base64: image_base64 }
|
43
|
+
return { image_base64: image_base64 }
|
64
44
|
else
|
65
45
|
image_url = JSON.parse(response.body)['data'][0]['url']
|
66
|
-
{ image_url: image_url }
|
46
|
+
return { image_url: image_url }
|
67
47
|
end
|
68
48
|
else
|
69
|
-
raise RequestFailed
|
49
|
+
raise RequestFailed, "Failed to generate image. Response code: #{response.code}. Response body: #{response.body}"
|
70
50
|
end
|
71
51
|
end
|
52
|
+
|
72
53
|
end
|
73
54
|
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.1.
|
4
|
+
version: 1.1.1
|
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-03-
|
11
|
+
date: 2023-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-http
|