Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.0.1 #1

Merged
merged 3 commits into from
Aug 29, 2023
Merged
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
58 changes: 56 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
# leonardo_api
Leonardo.ai API
# Leonardo_api

pypi package can be found [here](https://pypi.org/project/leonardo_api/).

## This is Leonardo.ai API.

This package contains Python API for [Leonardo.ai](https://leonardo.ai/) based on official [API documentation](https://docs.leonardo.ai/reference).

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](https://app.leonardo.ai/settings)->User API. Then, init manager class with using your access_token:

```python
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:

```python
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:

```json
{'sdGenerationJob': {'generationId': '123456-0987-aaaa-bbbb-01010101010'}}
```

To obtain your image you need to use additional method:

```python

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

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

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

Finally, you'll get your array of images:

```python
[{'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](https://raw.githubusercontent.com/wwakabobik/leonardo_api/master/src/assets/fox.jpeg)

You'll find descriptions for rest of the methods in official [API reference](https://docs.leonardo.ai/reference).
11 changes: 11 additions & 0 deletions pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Code linters/formatters
- [ ] PyCharm inspection passed on changed files (or any local IDE)
- [ ] pylint inspection executed and passed on changed files
- [ ] mypy inspection executed and passed on changed files
- [ ] Black code formatter executed and passed on changed files

### Documentation
- [ ] Documentation / README.md has been updated

### Other code changes
- [ ] The soul is pure
14 changes: 11 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ authors = [
description = "Leonardo.ai Python API"
readme = "README.md"
license = { file="LICENSE" }
requires-python = ">=3.7"
requires-python = ">=3.8"
dependencies = [
'requests',
'aiohttp',
'aiofiles',
'asyncio',
'requests',
'logging'
'urllib3',
'async-timeout',
'certifi',
'charset-normalizer',
'frozenlist'
]
classifiers = [
"Programming Language :: Python :: 3",
Expand All @@ -32,7 +36,11 @@ install_requires = [
'aiofiles',
'asyncio',
'requests',
'logging'
'urllib3',
'async-timeout',
'certifi',
'charset-normalizer',
'frozenlist'
]

[project.urls]
Expand Down
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
aiohttp==3.8.5
aiofiles==23.2.1
asyncio==3.4.3
requests==2.31.0
urllib3==2.0.4
async-timeout==4.0.3
certifi==2023.7.22
charset-normalizer==3.2.0
frozenlist==1.4.0
8 changes: 6 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ url = https://github.com/wwakabobik/leonardo_api
description = Leonardo.ai Python API
long_description = file: README.md
long_description_content_type = text/markdown
keywords = testrail, api, testrail_api, confluence, chart, reports
keywords = leonardo, leonardo.ai, image generation, stablediffusion, api
license = MIT License
classifiers =
License :: OSI Approved :: MIT License
Expand All @@ -20,7 +20,11 @@ install_requires =
aiofiles
asyncio
requests
logging
urllib3
async-timeout
certifi
charset-normalizer
frozenlist

[options.package_data]
* = README.md
56 changes: 56 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Leonardo_api

pypi package can be found [here](https://pypi.org/project/leonardo_api/).

## This is Leonardo.ai API.

This package contains Python API for [Leonardo.ai](https://leonardo.ai/) based on official [API documentation](https://docs.leonardo.ai/reference).

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](https://app.leonardo.ai/settings)->User API. Then, init manager class with using your access_token:

```python
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:

```python
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:

```json
{'sdGenerationJob': {'generationId': '123456-0987-aaaa-bbbb-01010101010'}}
```

To obtain your image you need to use additional method:

```python

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

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

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

Finally, you'll get your array of images:

```python
[{'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](https://raw.githubusercontent.com/wwakabobik/leonardo_api/master/src/assets/fox.jpeg)

You'll find descriptions for rest of the methods in official [API reference](https://docs.leonardo.ai/reference).
Binary file added src/assets/fox.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ aiohttp==3.8.5
aiofiles==23.2.1
asyncio==3.4.3
requests==2.31.0
logging==0.4.9.6
urllib3==2.0.4
async-timeout==4.0.3
certifi==2023.7.22
charset-normalizer==3.2.0
frozenlist==1.4.0