ruby-leonardoai 0.1.0 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 267d56d7aa704aabc1811a935781aeb4a76fd33a6190814fa24d0205094e4a0a
4
- data.tar.gz: 257de0b0042ce3f39de55fbb49b6df8a04b011852627cf6c3ae040c5ae76c3c9
3
+ metadata.gz: d108ed0e36199b2e7841810b31b77e951b1173082f97aa75410de1050aaa6080
4
+ data.tar.gz: c31edd35e33bc9c5a0c9ba2df992f8ce9b8f38698cdd9c48d0789a441f0eb8cd
5
5
  SHA512:
6
- metadata.gz: a35af039b1a1a22e5b34d64e9fa7698dca4ecde84205699218bba8bb3a9ac8e769afb87342b97bc0a2374a1496b7ee4f8def929e356422acb476aebda0f3f69c
7
- data.tar.gz: aa680397e3e68cf6e442b5c0932d09ed0423352e94c7ad53d1fa560bf6bfb4e5a50b00d2dcac6573c8d8063e890ab072103ae35e83dbddf2c2dd2273df6b8dc3
6
+ metadata.gz: 9fce8ddb89a37f921531baecab3ee91d95314bc94757acdbab8d140c871b64caa65d961bb1c8929242c1e0d40e075cf64f088a81c73d8e6185969ae3e2ebb159
7
+ data.tar.gz: 2acadd28592ca2cb8131892b853add8193afef93f22f74d00af9060df6c7cbeed8d11cfd761106ab2cb4677a4c2f6e12e552eae51086b97a1df2757d0a878375
data/CHANGELOG.md CHANGED
@@ -3,3 +3,11 @@
3
3
  ## [0.1.0] - 2023-10-01
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.3] - 2023-10-10
8
+
9
+ - Updated file structure and got image generation working.
10
+
11
+ ## [0.1.3] - 2023-10-10
12
+
13
+ - Added README.md and updated installation and how-to-use instructions
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-leonardoai (0.1.3)
5
+ faraday (>= 1)
6
+ faraday-multipart (>= 1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.5)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ dotenv (2.8.1)
19
+ faraday (2.7.10)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ json (2.6.3)
27
+ multipart-post (2.3.0)
28
+ parallel (1.23.0)
29
+ parser (3.2.2.4)
30
+ ast (~> 2.4.1)
31
+ racc
32
+ public_suffix (5.0.3)
33
+ racc (1.7.1)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ regexp_parser (2.8.1)
37
+ rexml (3.2.6)
38
+ rspec (3.12.0)
39
+ rspec-core (~> 3.12.0)
40
+ rspec-expectations (~> 3.12.0)
41
+ rspec-mocks (~> 3.12.0)
42
+ rspec-core (3.12.2)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-expectations (3.12.3)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-mocks (3.12.6)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-support (3.12.1)
51
+ rubocop (1.50.2)
52
+ json (~> 2.3)
53
+ parallel (~> 1.10)
54
+ parser (>= 3.2.0.0)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8, < 3.0)
57
+ rexml (>= 3.2.5, < 4.0)
58
+ rubocop-ast (>= 1.28.0, < 2.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 2.4.0, < 3.0)
61
+ rubocop-ast (1.29.0)
62
+ parser (>= 3.2.1.0)
63
+ ruby-progressbar (1.13.0)
64
+ ruby2_keywords (0.0.5)
65
+ unicode-display_width (2.5.0)
66
+ vcr (6.1.0)
67
+ webmock (3.18.1)
68
+ addressable (>= 2.8.0)
69
+ crack (>= 0.3.2)
70
+ hashdiff (>= 0.4.0, < 2.0.0)
71
+
72
+ PLATFORMS
73
+ x86_64-darwin-19
74
+
75
+ DEPENDENCIES
76
+ byebug (~> 11.1.3)
77
+ dotenv (~> 2.8.1)
78
+ rake (~> 13.0)
79
+ rspec (~> 3.12)
80
+ rubocop (~> 1.50.2)
81
+ ruby-leonardoai!
82
+ vcr (~> 6.1.0)
83
+ webmock (~> 3.18.1)
84
+
85
+ BUNDLED WITH
86
+ 2.4.20
data/README.md CHANGED
@@ -1,32 +1,170 @@
1
- # Ruby::Leonardoai
1
+ # Ruby LeonardoAI
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby/leonardoai`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/ruby-openai.svg)](https://badge.fury.io/rb/ruby-leonardoai)
4
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/royalgiant/ruby-leonardoai/blob/main/LICENSE.txt)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Use the [LeonardoAI API](https://docs.leonardo.ai/reference/getuserself) with Ruby! 🤖❤️
6
7
 
7
- ## Installation
8
+ Generate, Get, and Delete images, models, variations, and datasets with Leonardo AI
8
9
 
9
- Install the gem and add to the application's Gemfile by executing:
10
+ 🚢 Based in the US and want to hire me? Now you can! [Email Me](mailto:donaldlee50@gmail.com)
10
11
 
11
- $ bundle add ruby-leonardoai
12
+ [🐦 Twitter](https://twitter.com/donaldlee50) | [▶️ Youtube](https://youtube.com/c/donaldleecrypto)
12
13
 
13
- If bundler is not being used to manage dependencies, install the gem by executing:
14
+ ### Bundler
14
15
 
15
- $ gem install ruby-leonardoai
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem "ruby-leonardoai"
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ $ bundle install
26
+ ```
27
+
28
+ ### Gem install
29
+
30
+ Or install with:
31
+
32
+ ```bash
33
+ $ gem install ruby-leonardoai
34
+ ```
35
+
36
+ and require with:
37
+
38
+ ```ruby
39
+ require "leonardoai"
40
+ ```
16
41
 
17
42
  ## Usage
18
43
 
19
- TODO: Write usage instructions here
44
+ - Get your API access from [https://app.leonardo.ai/api-access](https://app.leonardo.ai/api-access)
45
+ - After subscribing, you will be able to generate an api key from [https://app.leonardo.ai/settings](https://app.leonardo.ai/settings)
46
+
47
+ ### Quickstart
48
+
49
+ For a quick test you can pass your token directly to a new client:
50
+
51
+ ```ruby
52
+ client = LeonardoAI::Client.new(access_token: "access_token_goes_here")
53
+ ```
54
+
55
+ ### With Config
56
+
57
+ For a more robust setup, you can configure the gem with your API keys, for example in an `leonardoai.rb` initializer file. Never hardcode secrets into your codebase - instead use something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments.
58
+
59
+ ```ruby
60
+ LeonardoAI.configure do |config|
61
+ config.access_token = ENV.fetch("LEONARDOAI_ACCESS_TOKEN")
62
+ end
63
+ ```
64
+ If you use Rails 7, you would probably store your key in credentials.yml, then you can do something like this:
65
+ ```ruby
66
+ LeonardoAI.configure do |config|
67
+ config.access_token = Rails.application.credentials[Rails.env.to_sym].dig(:leonardoai, :api_key)
68
+ end
69
+ ```
70
+
71
+ Then you can create a client like this:
72
+
73
+ ```ruby
74
+ client = LeonardoAI::Client.new
75
+ ```
76
+
77
+ You can still override the config defaults when making new clients; any options not included will fall back to any global config set with LeonardoAI.configure. e.g. in this example the uri_base, request_timeout, etc. will fallback to any set globally using LeonardoAI.configure, with only the access_token overridden:
78
+
79
+ ```ruby
80
+ client = LeonardoAI::Client.new(access_token: "access_token_goes_here")
81
+ ```
82
+
83
+ #### Custom timeout or base URI
84
+
85
+ The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client. You can also change the base URI used for all requests.
86
+
87
+ ```ruby
88
+ client = LeonardoAI::Client.new(
89
+ access_token: "access_token_goes_here",
90
+ uri_base: "https://cloud.leonardo.ai/api/rest/v1/",
91
+ request_timeout: 240,
92
+ extra_headers: {
93
+ "accept" => "application/json",
94
+ "content-type": "application/json",
95
+ }
96
+ )
97
+ ```
98
+
99
+ or when configuring the gem:
100
+
101
+ ```ruby
102
+ LeonardoAI.configure do |config|
103
+ config.access_token = ENV.fetch("LEONARDOAI_ACCESS_TOKEN") # Or Rails.application.credentials[Rails.env.to_sym].dig(:leonardoai, :api_key) for Rails 7
104
+ config.uri_base = "https://cloud.leonardo.ai/api/rest/v1/",
105
+ config.request_timeout = 240 # Optional
106
+ config.extra_headers = {
107
+ "accept" => "application/json",
108
+ "content-type": "application/json",
109
+ } # Optional
110
+ end
111
+ ```
112
+
113
+ ### Generation
114
+
115
+ ChatGPT is a model that can be used to generate text in a conversational style. You can use it to [generate a response](https://platform.openai.com/docs/api-reference/chat/create) to a sequence of [messages](https://platform.openai.com/docs/guides/chat/introduction):
116
+
117
+ ```ruby
118
+ params = {
119
+ :height=>1024,
120
+ :prompt=>"A ad flyer of a ninja cat, heavy texture, moonlit background, circle design, tshirt design, pen and ink style",
121
+ :width=>512, :num_images=>1,
122
+ :photoReal=>false,
123
+ :presetStyle=>"LEONARDO",
124
+ :promptMagic=>true,
125
+ :promptMagicVersion=>"v2",
126
+ :public=>false,
127
+ :init_strength=>0.4,
128
+ :sd_version=>"v2",
129
+ }
130
+
131
+ response = client.generations.generate(parameters: params) # {"sdGenerationJob"=>{"generationId"=>"c747522c-91e7-4830-8d1f-1f1ed37efd35"}}
132
+ puts response.dig("sdGenerationJob", "generationId")
133
+ # => "c747522c-91e7-4830-8d1f-1f1ed37efd35"
134
+ ```
135
+
136
+ ### Model
137
+ UNDER CONSTRUCTION
138
+
139
+ ### Unzoom
140
+ UNDER CONSTRUCTION
141
+
142
+ ### Dataset
143
+ UNDER CONSTRUCTION
20
144
 
21
145
  ## Development
22
146
 
23
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
147
+ After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
24
148
 
25
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
149
+ To install this gem onto your local machine, run `bundle exec rake install`.
150
+
151
+ ### Warning
152
+
153
+ If you have an `LEONARDOAI_ACCESS_TOKEN` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
154
+
155
+ ## Release
156
+
157
+ First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set LEONARDOAI_ACCESS_TOKEN in your environment or pass it in like this:
158
+
159
+ ```
160
+ LEONARDOAI_ACCESS_TOKEN=123abc bundle exec rspec
161
+ ```
162
+
163
+ Then update the version number in `version.rb`, update `CHANGELOG.md`, run `bundle install` to update Gemfile.lock, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
164
 
27
165
  ## Contributing
28
166
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby-leonardoai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruby-leonardoai/blob/main/CODE_OF_CONDUCT.md).
167
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/royalgiant/ruby-leonardoai>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/royalgiant/ruby-leonardoai/blob/main/CODE_OF_CONDUCT.md).
30
168
 
31
169
  ## License
32
170
 
@@ -34,5 +172,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
34
172
 
35
173
  ## Code of Conduct
36
174
 
37
- Everyone interacting in the Ruby::Leonardoai project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby-leonardoai/blob/main/CODE_OF_CONDUCT.md).
38
- # ruby-leonardoai
175
+ Everyone interacting in the Ruby LeonardoAI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/royalgiant/ruby-leonardoai/blob/main/CODE_OF_CONDUCT.md).
176
+
177
+ ## Influences
178
+ Project heavily influenced by [https://github.com/alexrudall/ruby-openai](https://github.com/alexrudall/ruby-openai). Great project, go give them a star!
@@ -0,0 +1,31 @@
1
+ module LeonardoAI
2
+ class Client
3
+ include LeonardoAI::HTTP
4
+
5
+ CONFIG_KEYS = %i[
6
+ api_type
7
+ api_version
8
+ access_token
9
+ uri_base
10
+ request_timeout
11
+ extra_headers
12
+ ].freeze
13
+ attr_reader *CONFIG_KEYS
14
+
15
+ def initialize(config = {})
16
+ CONFIG_KEYS.each do |key|
17
+ # Set instance variables like api_type & access_token. Fall back to global config
18
+ # if not present.
19
+ instance_variable_set("@#{key}", config[key] || LeonardoAI.configuration.send(key))
20
+ end
21
+ end
22
+
23
+ def chat(parameters: {})
24
+ json_post(path: "/chat/completions", parameters: parameters)
25
+ end
26
+
27
+ def generations
28
+ @generations ||= LeonardoAI::Generations.new(client: self)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module LeonardoAI
3
+ VERSION = ::LeonardoAI::VERSION
4
+
5
+ Error = ::LeonardoAI::Error
6
+ ConfigurationError = ::LeonardoAI::ConfigurationError
7
+ Configuration = ::LeonardoAI::Configuration
8
+ end
9
+ end
@@ -5,11 +5,11 @@ module LeonardoAI
5
5
  end
6
6
 
7
7
  def generate(parameters: {})
8
- @client.json_post(path: "/images/generations", parameters: parameters)
8
+ @client.json_post(path: "/generations", parameters: parameters)
9
9
  end
10
10
 
11
11
  def edit(parameters: {})
12
- @client.multipart_post(path: "/images/edits", parameters: open_files(parameters))
12
+ @client.multipart_post(path: "/edits", parameters: open_files(parameters))
13
13
  end
14
14
 
15
15
  private
@@ -19,7 +19,5 @@ module LeonardoAI
19
19
  parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
20
20
  parameters
21
21
  end
22
-
23
- def
24
22
  end
25
23
  end
@@ -0,0 +1,73 @@
1
+ module LeonardoAI
2
+ module HTTP
3
+ def get(path:)
4
+ to_json(conn.get(uri(path: path)) do |req|
5
+ req.headers = headers
6
+ end&.body)
7
+ end
8
+
9
+ def json_post(path:, parameters:)
10
+ to_json(conn.post(uri(path: path)) do |req|
11
+ req.headers = headers
12
+ req.body = parameters.to_json
13
+ end&.body)
14
+ end
15
+
16
+ def multipart_post(path:, parameters: nil)
17
+ to_json(conn(multipart: true).post(uri(path: path)) do |req|
18
+ req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
19
+ req.body = multipart_parameters(parameters)
20
+ end&.body)
21
+ end
22
+
23
+ def delete(path:)
24
+ to_json(conn.delete(uri(path: path)) do |req|
25
+ req.headers = headers
26
+ end&.body)
27
+ end
28
+
29
+ private
30
+
31
+ def to_json(string)
32
+ return unless string
33
+
34
+ JSON.parse(string)
35
+ rescue JSON::ParserError
36
+ # Convert a multiline string of JSON objects to a JSON array.
37
+ JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
38
+ end
39
+
40
+ def conn(multipart: false)
41
+ Faraday.new do |f|
42
+ f.options[:timeout] = @request_timeout
43
+ f.request(:multipart) if multipart
44
+ end
45
+ end
46
+
47
+ def uri(path:)
48
+ File.join(@uri_base, @api_version, path)
49
+ end
50
+
51
+ def headers
52
+ leonardoai_headers.merge(@extra_headers || {})
53
+ end
54
+
55
+ def leonardoai_headers
56
+ {
57
+ "Content-Type" => "application/json",
58
+ "Authorization" => "Bearer #{@access_token}",
59
+ }
60
+ end
61
+
62
+ def multipart_parameters(parameters)
63
+ parameters&.transform_values do |value|
64
+ next value unless value.respond_to?(:close) # File or IO object.
65
+
66
+ # Doesn't seem like OpenAI needs mime_type yet, so not worth
67
+ # the library to figure this out. Hence the empty string
68
+ # as the second argument.
69
+ Faraday::UploadIO.new(value, "", value.path)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module LeonardoAI
2
+ VERSION = "0.1.4"
3
+ end
data/lib/leonardoai.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require "faraday"
2
2
  require "faraday/multipart"
3
+ require_relative "leonardoai/http"
4
+ require_relative "leonardoai/client"
5
+ require_relative "leonardoai/generations"
3
6
  require_relative "leonardoai/version"
4
7
 
5
-
6
- module Leonardoai
8
+ module LeonardoAI
7
9
  class Error < StandardError; end
8
10
  class ConfigurationError < Error; end
9
11
 
@@ -13,7 +15,7 @@ module Leonardoai
13
15
  :extra_headers
14
16
 
15
17
  DEFAULT_API_VERSION = "v1".freeze
16
- DEFAULT_URI_BASE = "https://cloud.leonardo.ai/api/rest/v1/".freeze
18
+ DEFAULT_URI_BASE = "https://cloud.leonardo.ai/api/rest/".freeze
17
19
  DEFAULT_REQUEST_TIMEOUT = 120
18
20
 
19
21
  def initialize
@@ -35,5 +37,17 @@ module Leonardoai
35
37
  raise ConfigurationError, error_text
36
38
  end
37
39
  end
40
+
41
+ class << self
42
+ attr_writer :configuration
43
+ end
44
+
45
+ def self.configuration
46
+ @configuration ||= LeonardoAI::Configuration.new
47
+ end
48
+
49
+ def self.configure
50
+ yield(configuration)
51
+ end
38
52
  end
39
53
 
@@ -0,0 +1,2 @@
1
+ require_relative "../leonardoai"
2
+ require_relative "../leonardoai/compatibility"
@@ -1,10 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/ruby/leonardoai/version"
1
+ require_relative "lib/leonardoai/version"
4
2
 
5
3
  Gem::Specification.new do |spec|
6
4
  spec.name = "ruby-leonardoai"
7
- spec.version = Ruby::Leonardoai::VERSION
5
+ spec.version = LeonardoAI::VERSION
8
6
  spec.authors = ["Donald Lee"]
9
7
  spec.email = ["donaldlee50@gmail.com"]
10
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-leonardoai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donald Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-07 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -49,14 +49,19 @@ files:
49
49
  - CHANGELOG.md
50
50
  - CODE_OF_CONDUCT.md
51
51
  - Gemfile
52
+ - Gemfile.lock
52
53
  - LICENSE.txt
53
54
  - README.md
54
55
  - Rakefile
55
56
  - bin/console
56
57
  - bin/setup
57
58
  - lib/leonardoai.rb
58
- - lib/ruby/leonardoai/generations.rb
59
- - lib/ruby/leonardoai/version.rb
59
+ - lib/leonardoai/client.rb
60
+ - lib/leonardoai/compatibility.rb
61
+ - lib/leonardoai/generations.rb
62
+ - lib/leonardoai/http.rb
63
+ - lib/leonardoai/version.rb
64
+ - lib/ruby/leonardoai.rb
60
65
  - ruby-leonardoai.gemspec
61
66
  - sig/ruby/leonardoai.rbs
62
67
  homepage: https://github.com/royalgiant/ruby-leonardoai
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ruby
4
- module Leonardoai
5
- VERSION = "0.1.0"
6
- end
7
- end