LeoAndRuby is a Ruby gem for generating images using the Leonardo.ai API. With this gem, you can easily integrate Leonardo.ai's powerful image generation capabilities into your Ruby and Rails applications.
- Generate images using Leonardo.ai's models.
- Retrieve the status and result of a generated image.
- List available models on the Leonardo.ai platform.
- Webhook support to handle asynchronous image generation results.
- Rails generator for setting up webhook integration effortlessly.
- Simple and intuitive Ruby interface for interacting with the Leonardo.ai API.
Add this line to your application's Gemfile:
gem 'leoandruby'
Then, run:
bundle install
Or install it yourself with:
gem install leoandruby
To use LeoAndRuby, you need an API key from Leonardo.ai. You can obtain it by signing up for an account and navigating to the API key section in your dashboard.
For Rails users, you can generate a webhook controller and route using the built-in generator.
Start by creating a client instance with your Leonardo.ai API key:
require 'leoandruby'
api_key = 'YOUR_API_KEY'
client = LeoAndRuby::Client.new(api_key)
You can generate an image by providing the prompt, model ID, width, height, and optionally the number of images:
generation_response = client.generate_image(
prompt: 'An oil painting of a cat',
model_id: '6bef9f1b-29cb-40c7-b9df-32b51c1f67d3',
width: 512,
height: 512,
num_images: 1 # Optional, defaults to 1 if not specified
)
generation_id = generation_response['sdGenerationJob']['generationId']
You can fetch a list of all available platform models using the list_models
method:
models_response = client.list_models
puts models_response
Wait a few seconds for the image to be generated, then retrieve it using the generation ID:
sleep(5)
image_response = client.get_generation(generation_id)
puts image_response
LeoAndRuby supports asynchronous image generation through Leonardo.ai’s webhook feature. This allows your application to automatically process results when the image generation is complete.
Run the following command to generate a controller, route, and initializer for webhook integration:
rails generate leoandruby:webhook
This will:
- Create
app/controllers/leonardo_controller.rb
. - Add a route to
config/routes.rb
:post '/leonardo_webhook', to: 'leonardo#webhook'
- Add a configuration file to
config/initializers/leoandruby.rb
for managing the webhook token:LeoAndRuby.config = { webhook_token: ENV.fetch('LEONARDO_WEBHOOK_TOKEN', 'your_default_token_here') }
The webhook controller verifies requests using an API token provided in the Authorization
header. Configure your webhook token in an environment variable or directly in the initializer.
Webhook requests without a valid token will be rejected with a 401 Unauthorized
status.
Here's a full example script for generating an image and retrieving it:
require 'leoandruby'
api_key = 'YOUR_API_KEY'
client = LeoAndRuby::Client.new(api_key)
# Generate an image
generation_response = client.generate_image(
prompt: 'A futuristic cityscape at sunset',
model_id: '6bef9f1b-29cb-40c7-b9df-32b51c1f67d3',
width: 1024,
height: 768,
num_images: 1 # Optional, defaults to 1 if not specified
)
generation_id = generation_response['sdGenerationJob']['generationId']
# Wait for a few seconds
sleep(5)
# Retrieve the generated image
image_response = client.get_generation(generation_id)
puts image_response
You can store your API key in an environment variable for security:
export LEOANDRUBY_API_KEY=your_api_key
Then, retrieve it in your code:
api_key = ENV['LEOANDRUBY_API_KEY']
client = LeoAndRuby::Client.new(api_key)
To secure webhook requests, configure the LEONARDO_WEBHOOK_TOKEN
environment variable:
export LEONARDO_WEBHOOK_TOKEN=your_webhook_token
LeoAndRuby is tested with the latest Ruby versions. Ensure your environment is up to date to avoid compatibility issues.
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration.
The gem is available as open source under the terms of the MIT License.
Special thanks to Leonardo.ai for providing such an amazing image generation API.