replicate-api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gem/credentials +1 -0
- data/.rspec +3 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +46 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/lib/replicate/client.rb +57 -0
- data/lib/replicate/model.rb +32 -0
- data/lib/replicate/version.rb +5 -0
- data/lib/replicate.rb +10 -0
- data/sig/replicate/ruby.rbs +6 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6a977c31bb41a70a5734b042c1a3147d10b5d53976a0c6ad5f8425da6fabe74b
|
4
|
+
data.tar.gz: ea9f526e0b3afd6f221edb0a937aca33a8ad21fd7cb2be1c640659764789ee10
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5389b2593291fd89e28a5f6e6beb9fbf877c03f2cdc3fd2e28972615c03c5222e778b97d51c81a63ce8628aefaab966b95df86460dd88251316483a1ff93899c
|
7
|
+
data.tar.gz: cf6c9e95bcc21b96f2676f9a8707a9823240fbafefab1f01ba9b5602833246882c2b1b8c5488b76a13adc81fbe188b7a1d0ef12a919829876d0328170ebfbec5
|
data/.gem/credentials
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
:github: Bearer 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
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
replicate-api (0.1.0)
|
5
|
+
faraday (~> 2.7.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
faraday (2.7.1)
|
13
|
+
faraday-net_http (>= 2.0, < 3.1)
|
14
|
+
ruby2_keywords (>= 0.0.4)
|
15
|
+
faraday-net_http (3.0.2)
|
16
|
+
method_source (1.0.0)
|
17
|
+
pry (0.13.1)
|
18
|
+
coderay (~> 1.1)
|
19
|
+
method_source (~> 1.0)
|
20
|
+
rake (13.0.6)
|
21
|
+
rspec (3.12.0)
|
22
|
+
rspec-core (~> 3.12.0)
|
23
|
+
rspec-expectations (~> 3.12.0)
|
24
|
+
rspec-mocks (~> 3.12.0)
|
25
|
+
rspec-core (3.12.0)
|
26
|
+
rspec-support (~> 3.12.0)
|
27
|
+
rspec-expectations (3.12.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.12.0)
|
30
|
+
rspec-mocks (3.12.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.12.0)
|
33
|
+
rspec-support (3.12.0)
|
34
|
+
ruby2_keywords (0.0.5)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
arm64-darwin-21
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
pry (~> 0.13.1)
|
41
|
+
rake (~> 13.0)
|
42
|
+
replicate-api!
|
43
|
+
rspec (~> 3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
2.3.7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Olivia Li
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Replicate Ruby client
|
2
|
+
|
3
|
+
This is a Ruby client for Replicate. It lets you run models with your Ruby code.
|
4
|
+
|
5
|
+
Grab your token from replicate.com/account to use this library.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'replicate-api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install replicate-api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
You can run a model and get its output:
|
26
|
+
|
27
|
+
```
|
28
|
+
replicate = Replicate::Client.new(api_key: "#{YOUR API KEY HERE}")
|
29
|
+
model = replicate.models("stability-ai/stable-diffusion")
|
30
|
+
model.predict(prompt="a 19th century portrait of a wombat gentleman")
|
31
|
+
```
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/olivia-li/replicate-ruby.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
|
5
|
+
module Replicate
|
6
|
+
REPLICATE_API_BASE_URL = "https://api.replicate.com/v1"
|
7
|
+
|
8
|
+
# Client for the Replicate API
|
9
|
+
class Client
|
10
|
+
attr_reader :token, :conn
|
11
|
+
|
12
|
+
def initialize(token:, &block)
|
13
|
+
@token = token
|
14
|
+
if block_given?
|
15
|
+
conn &block
|
16
|
+
else
|
17
|
+
conn
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def conn(&block)
|
23
|
+
@conn ||= Faraday.new(url: REPLICATE_API_BASE_URL) do |f|
|
24
|
+
yield f if block_given?
|
25
|
+
f.request :json
|
26
|
+
f.response :json, content_type: /\bjson$/
|
27
|
+
f.headers = headers()
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def models(path)
|
32
|
+
return Replicate::Model.new(client: self, path: path)
|
33
|
+
end
|
34
|
+
|
35
|
+
def headers
|
36
|
+
return {
|
37
|
+
"Authorization": "Token #{@token}",
|
38
|
+
"User-Agent": "replicate-ruby@#{Replicate::VERSION}",
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def requests(method:, path:, **kwargs)
|
43
|
+
if ["GET", "OPTIONS"].include? method
|
44
|
+
kwargs = kwargs.merge({allow_redirects: true})
|
45
|
+
end
|
46
|
+
if ["HEAD"].include? method
|
47
|
+
kwargs = kwargs.merge({allow_redirects: false})
|
48
|
+
end
|
49
|
+
response = @conn.send(method.downcase, path, kwargs)
|
50
|
+
if 400 <= response.status && response.status < 600
|
51
|
+
raise ("HTTP error: #{response.status}, #{response.reason_phrase}")
|
52
|
+
end
|
53
|
+
return response
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Replicate
|
2
|
+
class Model
|
3
|
+
attr_reader :version_id, :path
|
4
|
+
|
5
|
+
def initialize(client:, path:)
|
6
|
+
@client = client
|
7
|
+
@path = path
|
8
|
+
response = client.requests(method: "GET", path: "models/#{path}")
|
9
|
+
@version_id = response.body["latest_version"]["id"]
|
10
|
+
end
|
11
|
+
|
12
|
+
def predict(prompt:)
|
13
|
+
body = {version: @version_id, input: {prompt: prompt}}
|
14
|
+
response = @client.requests(method: "POST", path: "predictions", **body)
|
15
|
+
poll(prediction_id: response.body["id"])
|
16
|
+
end
|
17
|
+
|
18
|
+
def poll(prediction_id:)
|
19
|
+
response = @client.requests(method: "GET", path: "predictions/#{prediction_id}")
|
20
|
+
puts(response.body["status"])
|
21
|
+
if !["succeeded", "failed", "canceled"].include?(response.body["status"])
|
22
|
+
sleep(2)
|
23
|
+
poll(prediction_id: prediction_id)
|
24
|
+
elsif response.body["status"] == "succeeded"
|
25
|
+
return response.body["output"][0]
|
26
|
+
else
|
27
|
+
raise response.body["error"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/replicate.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: replicate-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Olivia Li
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.7.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.7.1
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- oliviali.ouyang@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gem/credentials"
|
35
|
+
- ".rspec"
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- lib/replicate.rb
|
42
|
+
- lib/replicate/client.rb
|
43
|
+
- lib/replicate/model.rb
|
44
|
+
- lib/replicate/version.rb
|
45
|
+
- sig/replicate/ruby.rbs
|
46
|
+
homepage: https://github.com/Olivia-li/replicate-ruby.git
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata:
|
50
|
+
homepage_uri: https://github.com/Olivia-li/replicate-ruby.git
|
51
|
+
source_code_uri: https://github.com/Olivia-li/replicate-ruby.git
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.6.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.2.3
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Ruby API wrapper for Replicate
|
71
|
+
test_files: []
|