eleben 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/eleben.rb +155 -0
  3. metadata +71 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5d77822e9bc4d7bca50fa017c35c4e422290206fd5df247d86447beb16752b6
4
+ data.tar.gz: 4c5d6fe4af81bb496544576c69d44f9692d15bd1a2edc493faf301ae5e4e6d1c
5
+ SHA512:
6
+ metadata.gz: 5457b8a28bcd0ec0c42de7ab221a51d7b94dbd18febbb1341347cde772b20e02f37e10725b300f954055fcceca1d08856ff798f721448390bbbbd1beea7047fa
7
+ data.tar.gz: 78f91dc711a8facedf68949920dd99c5dd7399b5447e407bc8180a8acb7041ddb289a6facf0a4495e1dde8bf053abc1e0b970a115d8e774610004a4502f7e916
data/lib/eleben.rb ADDED
@@ -0,0 +1,155 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'base64'
4
+ require 'tempfile'
5
+ require 'fileutils'
6
+
7
+ class Eleben
8
+ DEFAULT_TIMEOUT = 300 # Default timeout in seconds
9
+
10
+ # Initializes a new instance of the StableDiffusion class.
11
+ #
12
+ # @param base_url [String] The base URL for the API.
13
+ # @param timeout [Integer] The timeout value in seconds (optional, default is 30).
14
+ def initialize(base_url, timeout: DEFAULT_TIMEOUT)
15
+ @base_url = base_url
16
+ @timeout = timeout
17
+ end
18
+
19
+ # Retrieves the available Stable Diffusion models.
20
+ #
21
+ # @return [Array] An array of Stable Diffusion models.
22
+ def get_sd_models
23
+ uri = URI("#{@base_url}/sdapi/v1/sd-models")
24
+ response = Net::HTTP.get(uri)
25
+ JSON.parse(response)
26
+ end
27
+
28
+ # Retrieves the Stable Diffusion Variational Autoencoder (VAE).
29
+ #something
30
+ # @return [Hash] The Stable Diffusion VAE.
31
+ def get_sd_vae
32
+ uri = URI("#{@base_url}/sdapi/v1/sd-vae")
33
+ response = Net::HTTP.get(uri)
34
+ JSON.parse(response)
35
+ end
36
+
37
+ # Retrieves the available samplers.
38
+ #
39
+ # @return [Array] An array of samplers.
40
+ def get_samplers
41
+ uri = URI("#{@base_url}/sdapi/v1/samplers")
42
+ response = Net::HTTP.get(uri)
43
+ JSON.parse(response)
44
+ end
45
+
46
+ # Retrieves the available embeddings.
47
+ #
48
+ # @return [Array] An array of embeddings.
49
+ def get_embeddings
50
+ uri = URI("#{@base_url}/sdapi/v1/embeddings")
51
+ response = Net::HTTP.get(uri)
52
+ JSON.parse(response)
53
+ end
54
+
55
+ # Retrieves the available options.
56
+ #
57
+ # @return [Hash] The available options.
58
+ def get_options
59
+ uri = URI("#{@base_url}/sdapi/v1/options")
60
+ response = Net::HTTP.get(uri)
61
+ JSON.parse(response)
62
+ end
63
+
64
+ # Updates the options with new values.
65
+ #
66
+ # @param new_options [Hash] The new options to be updated.
67
+ # @return [Hash] The updated options.
68
+ def update_options(new_options)
69
+ uri = URI("#{@base_url}/sdapi/v1/options")
70
+ http = Net::HTTP.new(uri.host, uri.port)
71
+
72
+ http.read_timeout = @timeout
73
+ http.open_timeout = @timeout
74
+
75
+ request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
76
+ request.body = new_options.to_json
77
+
78
+ response = http.request(request)
79
+
80
+ if response.code.to_i == 200
81
+ JSON.parse(response.body)
82
+ else
83
+ raise "Error #{response.code}: #{response.message}"
84
+ end
85
+ end
86
+
87
+ # Changes the model used for stable diffusion.
88
+ #
89
+ # @param model_name [String] The name of the model to be used.
90
+ # @return [void]
91
+ def change_model(model_name)
92
+ options = get_options
93
+ options['sd_model_checkpoint'] = model_name
94
+ update_options(options)
95
+ end
96
+
97
+ # Converts text to image using Stable Diffusion.
98
+ #
99
+ # @param request_body [Hash] The request body containing the text.
100
+ # @return [Hash] The generated image.
101
+ def txt2img(request_body = {})
102
+ uri = URI("#{@base_url}/sdapi/v1/txt2img")
103
+ http = Net::HTTP.new(uri.host, uri.port)
104
+
105
+ http.read_timeout = @timeout
106
+ http.open_timeout = @timeout
107
+
108
+ request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
109
+ request.body = request_body.to_json
110
+
111
+ response = http.request(request)
112
+
113
+ if response.code.to_i == 200
114
+ JSON.parse(response.body)
115
+ else
116
+ raise "Error #{response.code}: #{response.message}"
117
+ end
118
+ end
119
+
120
+ # Saves the image data to disk at the specified output path.
121
+ #
122
+ # @param image_data [String] The base64-encoded image data.
123
+ # @param output_path [String] The path where the image will be saved.
124
+ # @return [void]
125
+ def save_image_to_disk(image_data, output_path)
126
+ image_binary_data = Base64.decode64(image_data)
127
+
128
+ # Use Tempfile to generate a unique temporary file path
129
+ temp_file = Tempfile.new(['sd_image', '.png'])
130
+ temp_file.binmode
131
+ temp_file.write(image_binary_data)
132
+ temp_file.close
133
+
134
+ # Move the temporary file to the specified output path
135
+ FileUtils.mv(temp_file.path, output_path)
136
+
137
+ puts "Image saved to #{output_path}"
138
+ end
139
+
140
+ # Converts text to image using the txt2img API and saves the resulting image to disk.
141
+ #
142
+ # @param params [Hash] The parameters for the txt2img request.
143
+ # @param output_path [String] The path where the image will be saved.
144
+ # @return [void]
145
+ def txt2img_and_save(params, output_path)
146
+ # Make txt2img request
147
+ txt2img_response = txt2img(params)
148
+ puts "txt2img Response:"
149
+ puts txt2img_response
150
+
151
+ # Save the image to disk
152
+ image_data = txt2img_response['images'][0]
153
+ save_image_to_disk(image_data, output_path)
154
+ end
155
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eleben
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lleetllama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem provides a Ruby client for the Automatic1111 API.
42
+ email:
43
+ - lleetllama@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/eleben.rb
49
+ homepage: https://github.com/lleetllama/eleben
50
+ licenses: []
51
+ metadata: {}
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: '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.32
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A gem for interacting with the Automatic1111 API
71
+ test_files: []