deepratna-awale / AutoExpress

Automatically creates character expressions from given image using A1111 Stable Diffusion API (with ADetailer).
MIT License
10 stars 2 forks source link

sd-parser integration #9

Closed d3x-at closed 3 months ago

d3x-at commented 4 months ago

Hello there!

Just wanted to drop a quick heads-up that 0.4 will be coming soon (-ish), which might simplify a few things on the user side!

Looking at your code, with the current "pre-0.4" state (pip3 install git+https://github.com/d3x-at/sd-parsers), it might look like below.

If you find anything unclear in how to use this library or have ideas on how to structure the data in a better way, please drop me a Note!

from sd_parsers import ParserManager
from sd_parsers.data import Generators

parser_manager = ParserManager()

def get_lora_from_prompt(prompt):
    return [[""]]

def generate_parameters(image_path):
    parsed_data = parser_manager.parse(image_path)
    if not parsed_data:  # return if no metadata found in image
        return None

    # get first sampler (there may be more than one (i.e., in upscaled comfy images)
    sampler = next(iter(parsed_data.samplers), None)
    if sampler is None:  # return if no samplers found in image
        return

    prompt = parsed_data.full_prompt
    if prompt:
        lora = get_lora_from_prompt(prompt)[0]

    width, height = "", ""
    if parsed_data.generator == Generators.AUTOMATIC1111:
        try:
            # almost every SD image generator uses a different way to store image sizes
            width, height = parsed_data.metadata["Size"].split("x")
        except Exception:
            pass

    params = {
        "seed": sampler.parameters.get("seed", None),
        "lora": parsed_data.metadata.get("lora", lora[0]),
        "ad_prompt": prompt,
        "ad_negative_prompt": parsed_data.full_negative_prompt,
        "ad_checkpoint": sampler.model.name if sampler.model else "unknown",
        "ad_sampler": sampler.name,
        "ad_clip_skip": "2",
        "ad_inpaint_width": width,
        "ad_inpaint_height": height,
        "ad_cfg_scale": sampler.parameters.get("cfg_scale", None),
        "ad_denoising_strength": "0.5",
    }
    return params

def main():
    image_path = r"D:\Workspace\AI\stable-diffusion-webui\outputs\txt2img-images\2023-11-30\00005-856227872.png"
    params = generate_parameters(image_path)
    print(params)

if __name__ == "__main__":
    main()
deepratna-awale commented 4 months ago

@d3x-at Thank you for the heads up, I'll edit my code base when you publish the new release. Maybe with a few UI changes, too. One thing I was excited about was parsing the text blob of metadata. I had trouble parsing through it last time. If you could suggest how I can go about or add a feature to return the metadata as a dict, that'd be very beneficial, as due to that I cannot extend the options populated in my UI.

d3x-at commented 4 months ago

If i understand your code right, you went into PromptInfo.parameters, which is raw data of whatever the original image generating software put in there, be it a string, json or whatever. You should almost never need to go into that property.

If you want to access the data as parsed by sd-parsers , look at PromptInfo.prompts, .models, .samplers, .metadata or similar properties as is done in the example scripts. From 0.3.1 you can also simply print() the ParserManager.parse() result to see what's in there.

I think that should answer your concerns about accessing the information as dict or similar. If not, feel free to follow up!

deepratna-awale commented 4 months ago

I meant how the parsed_data.metadata does not have appropriate keys and values when compared with the raw parameters.

PS: Thank you for the code above, it reduced my code length by a mile...

d3x-at commented 4 months ago

I meant how the parsed_data.metadata does not have appropriate keys and values when compared with the raw parameters.

Yes, this is how it is intended. As stated in the README.md PromptInfo.metadata only contains information that cannot be attributed to either prompts, samplers or models.