Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
## Features

- Generate images using Leonardo.ai's models.
- Generate images using user-customized elements.
- Retrieve the status and result of a generated image.
- List available models on the Leonardo.ai platform.
- Fetch user information and custom elements.
- 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.
Expand Down Expand Up @@ -96,6 +98,55 @@ image_response = client.get_generation(generation_id)
puts image_response
```

### 5. Generate an Image with User Elements

You can generate an image using custom user elements by passing a list of objects containing id and weight:

Example Usage:

```ruby
user_elements = [
{ id: 26060, weight: 0.8 },
{ id: 27868, weight: 1.2 }
]

generation_response = client.generate_image_with_user_elements(
prompt: "A birthday celebration scene",
model_id: "SDXL_0_9",
width: 1024,
height: 1024,
presetStyle: "ILLUSTRATION",
num_images: 1,
promptMagic: true,
enhancePrompt: true,
user_elements: user_elements
)

puts generation_response

```

### 6. Retrieve User Information

To fetch details about the currently authenticated user:

```ruby
user_info = client.me
puts user_info

```

### 7. Retrieve Custom Elements by User ID

To get a list of custom elements associated with a specific user ID:

```ruby
user_id = "12345"
custom_elements = client.get_custom_elements_by_user_id(user_id)
puts custom_elements

```

---

## Webhook Integration (Rails)
Expand Down
60 changes: 60 additions & 0 deletions lib/leoandruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,66 @@ def generate_image(prompt:, model_id:, width:, height:, num_images: 1)
handle_response(response)
end

def generate_image_with_user_elements(prompt:, model_id:, width:, height:, presetStyle: ,num_images: 1, promptMagic:, enhancePrompt:, user_elements:)
uri = URI("#{API_BASE_URL}/generations")
request = Net::HTTP::Post.new(uri)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer #{@api_key}"
request["Content-Type"] = "application/json"

user_elements_data = user_elements.map do |element|
{
userLoraId: element[:id],
weight: element[:weight] || 1.0
}
end

request.body = {
prompt: prompt,
modelId: model_id,
width: width,
height: height,
presetStyle: presetStyle,
num_images: num_images,
promptMagic: promptMagic,
enhancePrompt: enhancePrompt,
userElements: user_elements_data
}.to_json

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end

handle_response(response)
end


def me
uri = URI("#{API_BASE_URL}/me")
request = Net::HTTP::Get.new(uri)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer #{@api_key}"

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end

handle_response(response)
end

def get_custom_elements_by_user_id(user_id)
uri = URI("#{API_BASE_URL}/elements/user/#{user_id}")
request = Net::HTTP::Get.new(uri)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer #{@api_key}"

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end

handle_response(response)
end

def get_generation(generation_id)
uri = URI("#{API_BASE_URL}/generations/#{generation_id}")
request = Net::HTTP::Get.new(uri)
Expand Down