Unofficial Leap.ai Python Library

In the past days I used and tested the Leap model to generate images. So I coded a python library to interact with the REST API provided. The whole code is open source, you can download it, and use it. You can find the all links in the references.

IperGiove
3 min readMar 21, 2023

TryLeap Class

The “TryLeap” library is a Python module that provides a set of methods to interact with the Leap API. The library offers several methods as example create, train, and generate images.

Configuration and Installation

To use the “TryLeap” library, you must first install the package:

pip install py_leap_api

Then, you'll need to create an account on the TryLeap website and obtain an API key.

So now you can create an instance of the TryLeap class:

from py_leap_api.leap import TryLeap
leap = TryLeap(api="Your_api")

At the first time you have to create a model, like this:

# Create a model
model = await leap.create_model("My Model")
print(model)

The output should be a dictionary:

{
"id": "string",
"title": "string",
"subjectKeyword": "string",
"subjectIdentifier": "string"
}.

The id string is the id of the model that you can pass to a the method set_model of the class, in order to save it in the library and use for the further functions.

# add the model id
leap.set_model(model=model["id"])

Training and Tuning

Now that you have create a model, you can tune with your images. To do that it is necessary before update the image and after make the training.

# Upload some images
urls = [
"https://example.com/image1.png",
"https://example.com/image2.png",
"https://example.com/image3.png",
]
await leap.upload_images_url(urls)

# Train the model
queue = await leap.training_model()
print(queue)

Ideally if you have a webhook url you can provide it to training_model in order to know when the training finished. Otherwise you can fetch the status that return from the function, indeed the queue dict has the status key:

{
"id": "string",
"createdAt": "2023-03-21T22:42:18.431Z",
"status": "queued",
"model": {
"id": "string",
"title": "string",
"subjectKeyword": "string",
"subjectIdentifier": "string"
},
"weights": {
"uri": "string",
"id": "string"
}
}

Generate Images

When the training finished some images can be generated:

# Generate some images
prompt = "a cat sitting on a couch"
images = await leap.generate_image(prompt, number_images=3)

Also in this case you have to wait that the task is finished, and to check the output image you can use this function:

# Retrieve the output images
output_images = await leap.output_images()
print(output_images)

Example

I want to ask something similar to my logo.

await leap.generate_image("The burning Jupiter planet in purplish coloration")
{'status': 'queued',
'prompt': 'the burning jupiter planet in purplish coloration, concept art, by peter mohrbacher',
'negativePrompt': '',
'seed': 4523184,
'width': 720,
'height': 720,
'numberOfImages': 2,
'steps': 150,
'promptStrength': 20,
'images': [],
}

The results are really interesting.

References

  1. https://github.com/IperGiove/py_leap_api
  2. https://pypi.org/project/py-leap-api/
  3. https://www.tryleap.ai/
  4. https://twitter.com/leap_api

--

--