CodeExplode / stable-diffusion-webui-embedding-editor

Embedding editor extension for web ui

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tryind to save the edited embedding results in such message, despite embedding being obviously chosen for editing.

mart-hill opened this issue · comments

Traceback (most recent call last):
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\gradio\routes.py", line 321, in run_predict
    output = await app.blocks.process_api(
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 1015, in process_api
    result = await self.call_function(fn_index, inputs, iterator, request)
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\gradio\blocks.py", line 856, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\anyio\to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "O:\AI\stable-diffusion-webui\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "O:\AI\stable-diffusion-webui\extensions\stable-diffusion-webui-embedding-editor\scripts\embedding_editor.py", line 251, in save_embedding_weights
    save_embedding(embedding, checkpoint, embedding_name, filename, remove_cached_checksum=True)
TypeError: save_embedding() missing 1 required positional argument: 'filename'

Though, first, I did try to select vector 2 on said embedding, which only has 2 vectors (0 and 1), which resulted in such error (which is understandable):

File "O:\AI\stable-diffusion-webui\extensions\stable-diffusion-webui-embedding-editor\scripts\embedding_editor.py", line 195, in select_embedding
    vec = embedding.vec[int(vector_num)]
IndexError: index 2 is out of bounds for dimension 0 with size 2

Should I try to hardcode some filename, while not trying to reload the whole UI? I don't want to lose the changes I've made... :)

See #8, should be resolved when that goes through.

In terms of saving, if you keep the client open, make those changes in the python code, and then restart the UI. You should be able to save it (will keep on the sliders that you currently have set). Won't save it if you close the window though as it will lose those settings.

Thank you, I replaced the embedding_editor.py with your commit and I'm going to try to save after restarting the UI. :)

Saving embeddings now works, with optim files, too. Thanks!

If I wanted to merge locally #8 what git command would I need to use?

@illtellyoulater hmm if unfamilar with git, it might make upgrading this extension more difficult in the future. You could manually make the changes to the lines described in #8

See

def save_embedding_weights(embedding_name, vector_num, *weights):
apply_slider_weights(embedding_name, vector_num, weights)
embedding = sd_hijack.model_hijack.embedding_db.word_embeddings[embedding_name]
checkpoint = sd_models.select_checkpoint()
filename = os.path.join(shared.cmd_opts.embeddings_dir, f'{embedding_name}.pt')
optimizer = torch.optim.AdamW([embedding.vec])
save_embedding(embedding, optimizer, checkpoint, embedding_name, filename, remove_cached_checksum=True)
print(f"Saved embedding to {filename}")
and made the changes in your extensions/stable-diffusion-webui-embedding-editor/scripts/embedding_editor.py

Making that change will make it so updating will fail due to local changes. So if you have future problems, you could uninstall and re-install the extension.

yes I know I just wanted to take the occasion to learn how to do this with git, as it's something that might come handy for other projects too. If I got it correctly to merge the PR locally with git I should first fork the repo and then I could merge it... but anyway, in the end I updated this manually and can confirm the embeddings are now saved successfully. Thanks!

Ahh I see. Sorry about that, lots of people in this space may not be aware of coding so I didn't want you to get into a bad situation.

Could check out the GH version to check out the PR https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Using just git you would want to add my fork as a remote repo.

git remote add boo https://github.com/rockerBOO/stable-diffusion-webui-embedding-editor.git

then you can check with:

git remote -v

To list the remote repos. Will lilkely show origin to this repo and then a boo one of my other repo.

Then you'll need to fetch, then check out the branch.

git fetch
git checkout rockerBOO/save_embedding

then you can confirm

git branch

Should list it as rockerBOO/save_embedding

Eh... no surprise I keep delaying learning git... Although I expected this operation to be a little less convoluted, your comment makes it more clear, thank you!

Yeah, the idea is there are 3 different repos, the one on GitHub, my fork repo, and your local repo. So it makes it more complicated as you need to finagle with those. That's why GitHub made some CLI tooling to work with their pull request feature

gh pr checkout 8

Which does all the things behind the scenes to get you the pull request. Good to know both ways, but it may be easier to use the gh tooling. Less room for error, and generally should just work.

Ah yeah I recently tried running that gh line but I got an error saying the command was not present on my system despite having git installed, so I ended up thinking it was something not simpler but more advanced perhaps dedicated to the git freaks out there 😉 But if it works like you said then you're opening entirely new worlds to me!

I'll check it out soon, and hopefully my git life will become a lot easier for a while! 👍🔝

Need to install the github cli (gh) https://github.com/cli/cli#installation