Skip to main content

Leonardo.ai Python API

Project description

This is Leonardo.ai API.

PyPI version Linters PyPI - License PyPI - Python Version Downloads Downloads

This package contains Python API for Leonardo.ai based on official API documentation.

Leonardo.ai interface

To install the package, please use package from pypi:

pip install leonardo-api

This Python API provides access to Leonardo API using synchronous methods (based on requests library) as well as asynchronous (aiohttp). You can choose one of them - Leonardo or LeonardoAsync.

To start, you must have paid subscription and create an API access token from you settings page->User API. Then, init manager class with using your access_token:

from leonardo_api import Leonardo

leonardo = Leonardo(auth_token='abcd-1234-5678-90ef-deadbeef00000')

Now you can use all API methods, provided by Leonardo.ai API, i.e. starting getting user info and generating your first image:

response = leonardo.get_user_info()  # get your user info
response = leonardo.post_generations(prompt="The quick brown fox jumps over the lazy dog", num_images=1,
                                           negative_prompt='schrodinger cat paradox',
                                           model_id='e316348f-7773-490e-adcd-46757c738eb7', width=1024, height=768,
                                           guidance_scale=7)

In according to API reference, you will get the json answer with content about pending job like following:

{"sdGenerationJob": {"generationId": "123456-0987-aaaa-bbbb-01010101010"}}

To obtain your image you need to use additional method:

response = leonardo.get_single_generation(generation_id)  # get it using response['sdGenerationJob']['generationId']

Or, optionally, you may wait for job completion using following method:

response = leonardo.wait_for_image_generation(generation_id=response['sdGenerationJob']['generationId'])

Finally, you'll get your array of images:

[{'url': 'https://cdn.leonardo.ai/users/abcd-1234-5678-90ef-deadbeef00000/generations/123456-0987-aaaa-bbbb-01010101010/Absolute_Reality_v16_The_quick_brown_fox_jumps_0.jpg', 'nsfw': False, 'id': 'aaaaaa-bbbb-cccc-dddd-ffffffffff', 'likeCount': 0, 'generated_image_variation_generics': []}]

The quick brown fox jumps over the lazy dog

You'll find descriptions for rest of the methods in official API reference.


As option, you may want to use preloaded dicts with models (nsfw/community/platform):

from leonardo_api import platform_models, custom_models, nsfw_models

Which contains details of models like following:

{
    "data": {
        "custom_models": [
            {
                "id": "f1929ea3-b169-4c18-a16c-5d58b4292c69",
                "name": "RPG v5",
                # rest of the model data
            }
            # rest models
        ]
    }
}

Have fun and enjoy!

Donations

If you like this project, you can support it by donating via DonationAlerts.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leonardo_api-0.0.11.tar.gz (818.2 kB view details)

Uploaded Source

Built Distribution

leonardo_api-0.0.11-py3-none-any.whl (48.6 kB view details)

Uploaded Python 3

File details

Details for the file leonardo_api-0.0.11.tar.gz.

File metadata

  • Download URL: leonardo_api-0.0.11.tar.gz
  • Upload date:
  • Size: 818.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for leonardo_api-0.0.11.tar.gz
Algorithm Hash digest
SHA256 1e4dc83a1b42fbed0baccfbfa11f4e2daadf0f81f92ce4eadfeec2efe45332ec
MD5 817440d92f70471944a1fb92d7d0c366
BLAKE2b-256 bae4a7a3011f395fc2d6bb3f7e8269639ba7bc56bf921e5734a5360b75478921

See more details on using hashes here.

File details

Details for the file leonardo_api-0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for leonardo_api-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd9c4db8e97ac15ef242cce63b70e6db01ecd6c521bc00a36496c14f371779d
MD5 191d81e0cf63f0ced8c1023a84b0afed
BLAKE2b-256 6f8e92d401b8fb703a78d7c31e9553422e15784b7362f5a3862a14af16c83948

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page