Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Feature Request]: Add an API endpoint for refreshing Embeddings
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What would your feature do ?

I am currently running 2 instances of the WebUI locally, and that (for performance reasons) both use different hardware for different tasks. I interact with those via a backend communicating with the API. When I create an embedding on one instance it needs a manual refresh (e.g. refreshing the browser window, using the button in the UI etc) before the other instance can use it. 

As far as I know its currently possible to do something like refreshing via the API only for Checkpoints and VAE's, not for embeddings. 

An API endpoint for refreshing the embeddings would be a big problem solver for not only my use-case but lots of others in terms of automation. 

### Proposed workflow

In my case:
1. Perform an operation on an embedding that needs a refresh to be accessed somewhere else (e.g. another instance)
2. Make a request to the corresponding URL for refreshing embeddings as shown in the API docs 
3. Use the newly available embedding any way you want in your preferred location



### Additional information

_No response_


Discussion:
**Bad practice temporary solution I used for this:**
- Everytime my application creates an embedding on one instance of the server, it also creates one on the other, which refreshes the embeddings list on that other instance.
- I gave this embedding a placeholder name like 'refresh' (or anything that is noticably different to your other "real" embeddings) with the property 'overwrite_old' set to _true_ so that my embeddings list doesnt get cluttered with these refreshers.
- When showing the embeddings on my frontend I just filtered out the refresh embedding. 
Here's how I built it（sdapi/v1/refresh-embeddings）in api.py, after refreshing the query in the api updated（sdapi/v1/embeddings）, but the ui content is not updated.All this is for reference only：
1. 
`self.add_api_route("/sdapi/v1/refresh-embeddings", self.refresh_embeddings, methods=["POST"])`
2.
`def refresh_embeddings(self):
        sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings(force_reload=True) `

![image](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/33999813/8d15e01b-4d31-4920-8426-2d268f08a0cb)


![image](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/33999813/ed105f21-6f1b-4a58-b4dc-139f50f08872)

![image](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/33999813/71837179-51a6-4bf8-9978-a459d75e7d42)


Resolving patch:
diff --git a/modules/api/api.py b/modules/api/api.py
--- a/modules/api/api.py
+++ b/modules/api/api.py
@@ -230,6 +230,7 @@ def __init__(self, app: FastAPI, queue_lock: Lock):
         self.add_api_route("/sdapi/v1/realesrgan-models", self.get_realesrgan_models, methods=["GET"], response_model=list[models.RealesrganItem])
         self.add_api_route("/sdapi/v1/prompt-styles", self.get_prompt_styles, methods=["GET"], response_model=list[models.PromptStyleItem])
         self.add_api_route("/sdapi/v1/embeddings", self.get_embeddings, methods=["GET"], response_model=models.EmbeddingsResponse)
+        self.add_api_route("/sdapi/v1/refresh-embeddings", self.refresh_embeddings, methods=["POST"])
         self.add_api_route("/sdapi/v1/refresh-checkpoints", self.refresh_checkpoints, methods=["POST"])
         self.add_api_route("/sdapi/v1/refresh-vae", self.refresh_vae, methods=["POST"])
         self.add_api_route("/sdapi/v1/create/embedding", self.create_embedding, methods=["POST"], response_model=models.CreateResponse)
@@ -643,6 +644,10 @@ def convert_embeddings(embeddings):
             "skipped": convert_embeddings(db.skipped_embeddings),
         }
 
+    def refresh_embeddings(self):
+        with self.queue_lock:
+            sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings(force_reload=True)
+
     def refresh_checkpoints(self):
         with self.queue_lock:
             shared.refresh_checkpoints()


Test patch: