Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

F1zzTao/StableHordeAPI.py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StableHordeAPI.py

Simple wrapper around Stable Horde API

⚠️ StableHordeAPI.py is obsolete. Consider using horde-sdk by Haidra-Org instead ⚠️

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

About

Simple Stable Horde API python wrapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages