Skip to main content

Library for using Stable Horde API in Python

Project description

StableHordeAPI.py

Simple wrapper around Stable Horde API

Content

Installation

pip install stablehordeapi.py

Usage

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:

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:

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.

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:

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

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

stablehordeapi_py-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stablehordeapi_py-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file stablehordeapi_py-0.1.0.tar.gz.

File metadata

  • Download URL: stablehordeapi_py-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.7 Windows/10

File hashes

Hashes for stablehordeapi_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0cd696cc9ddf795e7f5e5571284e620c0d9671dd32eb7872e3f6a6384a103b06
MD5 f6e3b02d93b983fe82e5f3dbffa8c789
BLAKE2b-256 53432c1c9cea39fee70a7afd4cb3a49f7686de79e89f0f85f8d7aeca6e70973b

See more details on using hashes here.

File details

Details for the file stablehordeapi_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: stablehordeapi_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.7 Windows/10

File hashes

Hashes for stablehordeapi_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b0f1d735c65db6051c2013c0d74571c0aa74a6b0ffee6016b99236c092fd93a
MD5 e99f59b1f7231efefb898f78a678d88d
BLAKE2b-256 9dfc15a565651cd7047223240e84e638f11ae96233e4d90d17b56da3786c02cc

See more details on using hashes here.

Supported by

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