Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Feature Request]: Script API for modifying pasted infotext (in case of missing parameters, etc.)
### 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 ?

The webui code has some special logic in `parse_generation_parameters` for fixing up the infotext if some of the fields are missing, like restoring the default Clip Skip/hypernet strength values. Extensions can add their own infotext, but currently there is no way for them to run their own custom "fix infotext" code in case they need the same

For example, the additional_networks extension attaches up to 5 models/weights to the infotext, omitting any where the model is None or the weight is 0. When the infotext is pasted, if an infotext parameter is missing a default `gr.update()` is passed, so in that case the unused sliders for those parameters are not cleared from the previous settings

### Proposed workflow

Add a `Script.fix_generation_parameters()` or similar method to the scripting interface for modifying the infotext when a paste is detected.

### Additional information

_No response_


Discussion:

Resolving patch:
diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py
--- a/modules/generation_parameters_copypaste.py
+++ b/modules/generation_parameters_copypaste.py
@@ -7,7 +7,7 @@
 
 import gradio as gr
 from modules.shared import script_path
-from modules import shared, ui_tempdir
+from modules import shared, ui_tempdir, script_callbacks
 import tempfile
 from PIL import Image
 
@@ -298,6 +298,7 @@ def paste_func(prompt):
                     prompt = file.read()
 
         params = parse_generation_parameters(prompt)
+        script_callbacks.infotext_pasted_callback(prompt, params)
         res = []
 
         for output, key in paste_fields:
diff --git a/modules/script_callbacks.py b/modules/script_callbacks.py
--- a/modules/script_callbacks.py
+++ b/modules/script_callbacks.py
@@ -2,7 +2,7 @@
 import traceback
 from collections import namedtuple
 import inspect
-from typing import Optional
+from typing import Optional, Dict, Any
 
 from fastapi import FastAPI
 from gradio import Blocks
@@ -71,6 +71,7 @@ def __init__(self, imgs, cols, rows):
     callbacks_before_component=[],
     callbacks_after_component=[],
     callbacks_image_grid=[],
+    callbacks_infotext_pasted=[],
     callbacks_script_unloaded=[],
 )
 
@@ -172,6 +173,14 @@ def image_grid_callback(params: ImageGridLoopParams):
             report_exception(c, 'image_grid')
 
 
+def infotext_pasted_callback(infotext: str, params: Dict[str, Any]):
+    for c in callback_map['callbacks_infotext_pasted']:
+        try:
+            c.callback(infotext, params)
+        except Exception:
+            report_exception(c, 'infotext_pasted')
+
+
 def script_unloaded_callback():
     for c in reversed(callback_map['callbacks_script_unloaded']):
         try:
@@ -290,6 +299,15 @@ def on_image_grid(callback):
     add_callback(callback_map['callbacks_image_grid'], callback)
 
 
+def on_infotext_pasted(callback):
+    """register a function to be called before applying an infotext.
+    The callback is called with two arguments:
+       - infotext: str - raw infotext.
+       - result: Dict[str, any] - parsed infotext parameters.
+    """
+    add_callback(callback_map['callbacks_infotext_pasted'], callback)
+
+
 def on_script_unloaded(callback):
     """register a function to be called before the script is unloaded. Any hooks/hijacks/monkeying about that
     the script did should be reverted here"""


Test patch: