stablehordeapi-py


Namestablehordeapi-py JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/timius100/StableHordeAPI.py
SummaryLibrary for using Stable Horde API in Python
upload_time2022-12-14 00:07:01
maintainer
docs_urlNone
authorTimur Bogdanov
requires_python>=3.8,<4.0
licenseMIT
keywords stablehorde async asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h1 align="center">
StableHordeAPI.py
</h1>
<h2 align="center">Simple wrapper around Stable Horde API</h2>

# Content
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [License](#license)

# Installation
```
pip install stablehordeapi.py
```

# Usage
```python
import asyncio

from stablehorde_api import StableHordeAPI

async def main():
    client = StableHordeAPI("Your Stable Horde token here")
    await client.generate_from_txt(
        "Futuristic cyberpunk landscape, 8k, hyper realistic, cinematic"
    )

asyncio.run(main())
```
This code will generate an image based on your prompt and save it as "{unix timestamp}\_0.webp" in your current directory.

Additionally, you can specify file name:
```python
await client.generate_from_txt(
    "Your prompt...",
    filename="my_image"
)
```
In that case, your file will be saved as "my\_image.webp"

However, you'll probably want more control over how image is generated. So, for example, you can do this:
```python
import asyncio
from stablehorde_api import GenerationInput, ModelGenerationInputStable

async def main():
    client = StableHordeAPI("Your Stable Horde token here")
    payload = GenerationInput(
        "masterpiece, best quality, ((Hu Tao)), brown hair, long hair, flower-shaped pupils",
	params=ModelGenerationInputStable(
	    height=512,
	    width=768,
	    steps=50,
	    post_processing=['RealESRGAN_x4plus']
	),
	nsfw=True,
	censor_nsfw=False,
	models=['Anything Diffusion'],
	n=5
    )
    # payload can also be a dict, which is useful, if something new added
    txt2img_rsp = await client.txt2img_request(payload)
    img_uuid = txt2img_rsp.id

    done = False
    while not done:
        # Checking every second if image is generated
        await asyncio.sleep(1)
        generate_check = await client.generate_check(img_uuid)
	if generate_check.done == 1:
	    done = True

    # Generating a status which has all generations (in our case,
    # there should be 5 generations, because n is set to 5)
    generate_status = await client.generate_status(img_uuid)
    generations = generate_status.generations
```
After that, all generations will be in `generations` variable. To access first image, use `generations[0].img`

# Examples
This example will generate 3 Hu Tao images using Anything Diffusion model.
```python
import asyncio
import base64

import aiofiles
from stablehorde_api import GenerationInput, ModelGenerationInputStable

async def main():
    client = StableHordeAPI("Your Stable Horde token here")
    payload = GenerationInput(
        "masterpiece, best quality, ((Hu Tao)), brown hair, long hair, flower-shaped pupils",
	models=['Anything Diffusion'],
	n=3
    )
    txt2img_rsp = await client.txt2img_request(payload)
    img_uuid = txt2img_rsp.id

    done = False
    while not done:
        await asyncio.sleep(1)
        generate_check = await client.generate_check(img_uuid)
	if generate_check.done == 1:
	    done = True

    generate_status = await client.generate_status(img_uuid)
    generations = generate_status.generations
    for num, generation in enumerate(generations):
        new_filename = f'{filename}_{num}.webp'
        async with aiofiles.open(new_filename, 'wb') as file:
            b64_bytes = generation.img.encode('utf-8')
            img_bytes = base64.b64decode(b64_bytes)
            awat file.write(img_bytes)
```
If you set `r2` to true, then you will need to request content from the link in generations. You can do that by using aiohttp:
```python
import aiohttp
...

aiohttp_client = aiohttp.ClientSession()
...

img_rsp = (await aiohttp_client.request(url=generation.img)).content
img_bytes = await img_rsp.read()
```

# License
[MIT License](./LICENSE)


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/timius100/StableHordeAPI.py",
    "name": "stablehordeapi-py",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "stablehorde,async,asyncio",
    "author": "Timur Bogdanov",
    "author_email": "timurbogdanov2008@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/53/43/2c1c9cea39fee70a7afd4cb3a49f7686de79e89f0f85f8d7aeca6e70973b/stablehordeapi_py-0.1.0.tar.gz",
    "platform": null,
    "description": "<h1 align=\"center\">\nStableHordeAPI.py\n</h1>\n<h2 align=\"center\">Simple wrapper around Stable Horde API</h2>\n\n# Content\n- [Installation](#installation)\n- [Usage](#usage)\n- [Examples](#examples)\n- [License](#license)\n\n# Installation\n```\npip install stablehordeapi.py\n```\n\n# Usage\n```python\nimport asyncio\n\nfrom stablehorde_api import StableHordeAPI\n\nasync def main():\n    client = StableHordeAPI(\"Your Stable Horde token here\")\n    await client.generate_from_txt(\n        \"Futuristic cyberpunk landscape, 8k, hyper realistic, cinematic\"\n    )\n\nasyncio.run(main())\n```\nThis code will generate an image based on your prompt and save it as \"{unix timestamp}\\_0.webp\" in your current directory.\n\nAdditionally, you can specify file name:\n```python\nawait client.generate_from_txt(\n    \"Your prompt...\",\n    filename=\"my_image\"\n)\n```\nIn that case, your file will be saved as \"my\\_image.webp\"\n\nHowever, you'll probably want more control over how image is generated. So, for example, you can do this:\n```python\nimport asyncio\nfrom stablehorde_api import GenerationInput, ModelGenerationInputStable\n\nasync def main():\n    client = StableHordeAPI(\"Your Stable Horde token here\")\n    payload = GenerationInput(\n        \"masterpiece, best quality, ((Hu Tao)), brown hair, long hair, flower-shaped pupils\",\n\tparams=ModelGenerationInputStable(\n\t    height=512,\n\t    width=768,\n\t    steps=50,\n\t    post_processing=['RealESRGAN_x4plus']\n\t),\n\tnsfw=True,\n\tcensor_nsfw=False,\n\tmodels=['Anything Diffusion'],\n\tn=5\n    )\n    # payload can also be a dict, which is useful, if something new added\n    txt2img_rsp = await client.txt2img_request(payload)\n    img_uuid = txt2img_rsp.id\n\n    done = False\n    while not done:\n        # Checking every second if image is generated\n        await asyncio.sleep(1)\n        generate_check = await client.generate_check(img_uuid)\n\tif generate_check.done == 1:\n\t    done = True\n\n    # Generating a status which has all generations (in our case,\n    # there should be 5 generations, because n is set to 5)\n    generate_status = await client.generate_status(img_uuid)\n    generations = generate_status.generations\n```\nAfter that, all generations will be in `generations` variable. To access first image, use `generations[0].img`\n\n# Examples\nThis example will generate 3 Hu Tao images using Anything Diffusion model.\n```python\nimport asyncio\nimport base64\n\nimport aiofiles\nfrom stablehorde_api import GenerationInput, ModelGenerationInputStable\n\nasync def main():\n    client = StableHordeAPI(\"Your Stable Horde token here\")\n    payload = GenerationInput(\n        \"masterpiece, best quality, ((Hu Tao)), brown hair, long hair, flower-shaped pupils\",\n\tmodels=['Anything Diffusion'],\n\tn=3\n    )\n    txt2img_rsp = await client.txt2img_request(payload)\n    img_uuid = txt2img_rsp.id\n\n    done = False\n    while not done:\n        await asyncio.sleep(1)\n        generate_check = await client.generate_check(img_uuid)\n\tif generate_check.done == 1:\n\t    done = True\n\n    generate_status = await client.generate_status(img_uuid)\n    generations = generate_status.generations\n    for num, generation in enumerate(generations):\n        new_filename = f'{filename}_{num}.webp'\n        async with aiofiles.open(new_filename, 'wb') as file:\n            b64_bytes = generation.img.encode('utf-8')\n            img_bytes = base64.b64decode(b64_bytes)\n            awat file.write(img_bytes)\n```\nIf you set `r2` to true, then you will need to request content from the link in generations. You can do that by using aiohttp:\n```python\nimport aiohttp\n...\n\naiohttp_client = aiohttp.ClientSession()\n...\n\nimg_rsp = (await aiohttp_client.request(url=generation.img)).content\nimg_bytes = await img_rsp.read()\n```\n\n# License\n[MIT License](./LICENSE)\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Library for using Stable Horde API in Python",
    "version": "0.1.0",
    "split_keywords": [
        "stablehorde",
        "async",
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "e99f59b1f7231efefb898f78a678d88d",
                "sha256": "7b0f1d735c65db6051c2013c0d74571c0aa74a6b0ffee6016b99236c092fd93a"
            },
            "downloads": -1,
            "filename": "stablehordeapi_py-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e99f59b1f7231efefb898f78a678d88d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 6165,
            "upload_time": "2022-12-14T00:06:59",
            "upload_time_iso_8601": "2022-12-14T00:06:59.400167Z",
            "url": "https://files.pythonhosted.org/packages/9d/fc/15a565651cd7047223240e84e638f11ae96233e4d90d17b56da3786c02cc/stablehordeapi_py-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "f6e3b02d93b983fe82e5f3dbffa8c789",
                "sha256": "0cd696cc9ddf795e7f5e5571284e620c0d9671dd32eb7872e3f6a6384a103b06"
            },
            "downloads": -1,
            "filename": "stablehordeapi_py-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "f6e3b02d93b983fe82e5f3dbffa8c789",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 5903,
            "upload_time": "2022-12-14T00:07:01",
            "upload_time_iso_8601": "2022-12-14T00:07:01.061507Z",
            "url": "https://files.pythonhosted.org/packages/53/43/2c1c9cea39fee70a7afd4cb3a49f7686de79e89f0f85f8d7aeca6e70973b/stablehordeapi_py-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-14 00:07:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "timius100",
    "github_project": "StableHordeAPI.py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "stablehordeapi-py"
}
        
Elapsed time: 0.02863s