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

Is there a way to use additional networks in the webui api? #105

Closed
MuQQQQQ opened this issue Feb 13, 2023 · 17 comments
Closed

Is there a way to use additional networks in the webui api? #105

MuQQQQQ opened this issue Feb 13, 2023 · 17 comments

Comments

@MuQQQQQ
Copy link

MuQQQQQ commented Feb 13, 2023

I want to use additional networks in the webui api

My payload looks like this:

    payload = {
        "enable_hr": "true",
        "denoising_strength": 0.6,
        "hr_scale": 2,
        "hr_upscaler": "Latent",
        "hr_second_pass_steps": 20,
        "hr_resize_x": 0,
        "hr_resize_y": 0,
        "prompt": "prompt",
        "seed": -1,
        "subseed": -1,
        "subseed_strength": 0,
        "seed_resize_from_h": -1,
        "seed_resize_from_w": -1,
        "sampler_name": "DPM++ 2M Karras",
        "batch_size": 4,
        "n_iter": 1,
        "steps": 20,
        "cfg_scale": 7,
        "width": 320,
        "height": 480,
        "restore_faces": "false",
        "tiling": "false",
        "negative_prompt": "(EasyNegative), (bad_prompt)",
        "eta": 0,
        "s_churn": 0,
        "s_tmax": 0,
        "s_tmin": 0,
        "s_noise": 1,
        "override_settings": {},
        "override_settings_restore_afterwards": "true",
        "script_args": [],
    }

I want to use a lora model now, what changes do I need to make in the payload, maybe in script_args?

@FireFlieStudio
Copy link

did you find the solution for this? XD

@arch-fan
Copy link

You can charge lora if it's inside models/Lora folder with lora:nameofthemodel:quantity in the prompt. If you have inside your Lora's folder: potatoe.safetensors you must use: lora:potatoe:1.0

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Feb 20, 2023

You can charge lora if it's inside models/Lora folder with lora:nameofthemodel:quantity in the prompt. If you have inside your Lora's folder: potatoe.safetensors you must use: lora:potatoe:1.0

Thanks for your reply. It only works for some Lora models, but it error for others:

activating extra network lora with arguments [<modules.extra_networks.ExtraNetworkParams object at 0x7f8a140a2140>]: IndexError
Traceback (most recent call last):
  File "stable-diffusion-webui/modules/extra_networks.py", line 75, in activate
    extra_network.activate(p, extra_network_args)
  File "stable-diffusion-webui/extensions-builtin/Lora/extra_networks_lora.py", line 17, in activate
    lora.load_loras(names, multipliers)
  File "stable-diffusion-webui/extensions-builtin/Lora/lora.py", line 149, in load_loras
    lora = load_lora(name, lora_on_disk.filename)
  File "stable-diffusion-webui/extensions-builtin/Lora/lora.py", line 98, in load_lora
    module = torch.nn.Linear(weight.shape[1], weight.shape[0], bias=False)
IndexError: tuple index out of range

All the models would work fine if I used Addtional Networks. Maybe these Lora models and my webui are different versions?

@arch-fan
Copy link

I forgot the quotes hhashha, it should be lora:model:1.0

@arch-fan
Copy link

Sorry:

<lora:model:1.0>

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Feb 20, 2023

Sorry:

<lora:model:1.0>

Yes, I used this form.

In the UI, after clicking Generate, IndexError will appear to the console.

In the api, after add lora:model:1.0 in my prompt, webui told me Skipping unknown extra network: lora

For convenience's sake, I just assigned <lora:kobeni_v10:1> to prompt: prompt='<lora:kobeni_v10:1>'

And then there's this:

20230220194804

@arch-fan
Copy link

In which folder are your Lora models stored?

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Feb 20, 2023

In which folder are your Lora models stored?

stable-diffusion-webui/models/Lora

@arch-fan
Copy link

Hmm It doesn't seem to be a problem with the models. Check the webui settings but idk if you can change Lora's folder from there

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Feb 20, 2023

Hmm It doesn't seem to be a problem with the models. Check the webui settings but idk if you can change Lora's folder from there

Forget it, I'd rather use Addtional Networks in the UI.

Thank you for your patience:>

@space-nuko
Copy link
Contributor

space-nuko commented Feb 22, 2023

One possibility is to use this extension of mine and put the model name into the prompt, and let the inner extension logic take care of swapping out the networks:

https://github.com/space-nuko/a1111-stable-diffusion-webui-randomizer-keywords

So you'd just be able to put <addnet_model_1:some_lora> in the prompt via the API and it would work, this uses the additional_networks extension directly instead of webui's built-in support

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Feb 22, 2023

One possibility is to use this extension of mine and put the model name into the prompt, and let the inner extension logic take care of swapping out the networks:

https://github.com/space-nuko/a1111-stable-diffusion-webui-randomizer-keywords

So you'd just be able to put <addnet_model_1:some_lora> in the prompt via the API and it would work, this uses the additional_networks extension directly instead of webui's built-in support

That's a wonderful extension!

It works well in the UI interface, but there was an AssertionError when I use it in the API.

activating extra network addnet_model_1 with arguments [<modules.extra_networks.ExtraNetworkParams object at 0x7fe46c160970>]: AssertionError
Traceback (most recent call last):
  File "stable-diffusion-webui/modules/extra_networks.py", line 75, in activate
    extra_network.activate(p, extra_network_args)
  File "stable-diffusion-webui/extensions/randomizer-keywords/scripts/keywords.py", line 288, in activate
    update_extension_args("additional_networks", p, True, 0)
  File "stable-diffusion-webui/extensions/randomizer-keywords/scripts/keywords.py", line 258, in update_extension_args
    assert script, f"Could not find script for {script_class}!"
AssertionError: Could not find script for <class 'additional_networks.py.Script'>!

I printed all_scripts of Line 254 in keywords.py, and found that it was a empty list. Is this normal?

Should I add something into script_args in the payload when send POST to url?

@Vespinian
Copy link

Vespinian commented Feb 28, 2023

If you still have your issue and are up for it. You can try out my webui PR. If you have the keyword extension and add some for the first net, you might be able to use the txt2img example request almost as is since it's going to override the args passed to the script anyways.

I did have to do 2 small fixes to this extension to make it play well with this. They are to make sure it restores the network after a request and so it doesn't throw an error if it gets an empty tuple as an arg. Not exactly sure if these fixes end up having unwanted effect though.

@MuQQQQQ
Copy link
Author

MuQQQQQ commented Mar 5, 2023

If you still have your issue and are up for it. You can try out my webui PR. If you have the keyword extension and add some for the first net, you might be able to use the txt2img example request almost as is since it's going to override the args passed to the script anyways.

I did have to do 2 small fixes to this extension to make it play well with this. They are to make sure it restores the network after a request and so it doesn't throw an error if it gets an empty tuple as an arg. Not exactly sure if these fixes end up having unwanted effect though.

It works! Thanks for your contributions!

@MuQQQQQ MuQQQQQ closed this as completed Mar 5, 2023
Manto added a commit to talesinc/sd-webui-additional-networks that referenced this issue Mar 20, 2023
@unoriginalscreenname
Copy link

unoriginalscreenname commented Apr 16, 2023

I'm attempting to call the additional networks lora implementation through the api, but i'm really struggling to figure out how to do this. Here is the api call i'm trying to make to the sdapi/v1/txt2img:

{
"prompt": "a puppy",
"steps": 20,
"extra_generation_params": {
"AddNet Enabled": true,
"AddNet Module 1": "lora",
"AddNet Model 1": "cjmstyle__baseline__img-80__itr-60__lr-0005__tun-05__nr-5__v2-1",
"AddNet Weight A 1": 1,
"AddNet Weight B 1": 1
},
"save_images":true
}

I am unable to get the lora to load properly using the autmoatic1111 prompt injection. The UI for additional networks is the only thing that consistently works properly.

Does anybody know how to generate an image over the api and activate the additional networks features?

What I'm really trying to do is trigger an xyz plot of a bunch of different Lora models programmatically via the api. The UI functionality works perfectly with additional networks. if anybody has any idea how i could do this via the API I would really appreciate it.

@w7374520
Copy link

w7374520 commented Jul 3, 2023

I also want to load Lora through the img2img API. Have you resolved this issue?

@paranoidd
Copy link

Hello guys! I am attempting to use the lora models with API as well.
Trying the <lora:name:1> works, but gives worse results than using the "Additional Networks" through the UI.

So I suppose the same story as @unoriginalscreenname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants