Class: Stability::Client
- Inherits:
-
Object
- Object
- Stability::Client
- Includes:
- HTTP
- Defined in:
- lib/stability/client.rb
Instance Method Summary collapse
-
#generate_core(prompt, options: {}, json: false) ⇒ Object
Performs a text-to-image generation request to the Stability API using Stable Image Core.
-
#generate_sd3(prompt, options: {}, json: false) ⇒ Object
Performs a text-to-image or image-to-image generation request to the Stability API using Stable Diffusion 3.
-
#initialize(api_key: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) {|Stability.configuration| ... } ⇒ Client
constructor
Initializes the client with optional configurations.
Methods included from HTTP
#delete, #get, #multipart_post, #post
Constructor Details
#initialize(api_key: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) {|Stability.configuration| ... } ⇒ Client
Initializes the client with optional configurations.
15 16 17 18 19 20 21 |
# File 'lib/stability/client.rb', line 15 def initialize(api_key: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) Stability.configuration.api_key = api_key if api_key Stability.configuration.request_timeout = request_timeout if request_timeout Stability.configuration.uri_base = uri_base if uri_base Stability.configuration.extra_headers = extra_headers if extra_headers.any? yield(Stability.configuration) if block_given? end |
Instance Method Details
#generate_core(prompt, options: {}, json: false) ⇒ Object
Performs a text-to-image generation request to the Stability API using Stable Image Core.
48 49 50 51 52 53 54 55 |
# File 'lib/stability/client.rb', line 48 def generate_core(prompt, options: {}, json: false) headers = { "Accept" => json ? "application/json" : "image/*" } parameters = { prompt: }.merge() multipart_post(path: "/stable-image/generate/core", headers:, parameters:).tap do |response| raise ServerError, "Empty response from Stability. Might be worth retrying once or twice." if response.blank? raise ServerError, response.dig("error", "message") if response.dig("error", "message").present? end.with_indifferent_access end |
#generate_sd3(prompt, options: {}, json: false) ⇒ Object
Performs a text-to-image or image-to-image generation request to the Stability API using Stable Diffusion 3.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/stability/client.rb', line 87 def generate_sd3(prompt, options: {}, json: false) headers = { "Accept" => json ? "application/json" : "image/*" } parameters = { prompt: }.merge() # Ensure required parameters for image-to-image mode if parameters[:mode] == "image-to-image" raise ArgumentError, "image is required for image-to-image mode" unless parameters[:image] raise ArgumentError, "strength is required for image-to-image mode" unless parameters[:strength] end multipart_post(path: "/stable-image/generate/sd3", headers:, parameters:).tap do |response| raise ServerError, "Empty response from Stability. Might be worth retrying once or twice." if response.blank? raise ServerError, response.dig("error", "message") if response.dig("error", "message").present? end.with_indifferent_access end |