Improve frontend responsiveness for some buttons
diff --git a/javascript/ui.js b/javascript/ui.js
index ed9673d64e1..56ee05aa2c7 100644
--- a/javascript/ui.js
+++ b/javascript/ui.js
@@ -438,4 +438,54 @@ function updateImg2imgResizeToTextAfterChangingImage(){
     }, 500);
 
     return []
+
+}
+
+function setRandomSeed(target_interface) {
+    let seed = gradioApp().querySelector(`#${target_interface}_seed input`);
+    if (!seed) {
+        return [];
+    }
+    seed.value = "-1";
+    seed.dispatchEvent(new Event("input"));
+    return [];
+}
+
+function setRandomSubseed(target_interface) {
+    let subseed = gradioApp().querySelector(`#${target_interface}_subseed input`);
+    if (!subseed) {
+        return [];
+    }
+    subseed.value = "-1";
+    subseed.dispatchEvent(new Event("input"));
+    return [];
+}
+
+function switchWidthHeightTxt2Img() {
+    let width = gradioApp().querySelector("#txt2img_width input[type=number]");
+    let height = gradioApp().querySelector("#txt2img_height input[type=number]");
+    if (!width || !height) {
+        return [];
+    }
+    let tmp = width.value;
+    width.value = height.value;
+    height.value = tmp;
+    width.dispatchEvent(new Event("input"));
+    height.dispatchEvent(new Event("input"));
+    return [];
+}
+
+function switchWidthHeightImg2Img() {
+    let width = gradioApp().querySelector("#img2img_width input[type=number]");
+    let height = gradioApp().querySelector("#img2img_height input[type=number]");
+    if (!width || !height) {
+        return [];
+    }
+    let tmp = width.value;
+    width.value = height.value;
+    height.value = tmp;
+    width.dispatchEvent(new Event("input"));
+    height.dispatchEvent(new Event("input"));
+    return [];
 }
+
diff --git a/modules/ui.py b/modules/ui.py
index a47af214ab4..552a8af27c2 100644
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -189,8 +189,9 @@ def create_seed_inputs(target_interface):
         seed_resize_from_w = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from width", value=0, elem_id=f"{target_interface}_seed_resize_from_w")
         seed_resize_from_h = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize seed from height", value=0, elem_id=f"{target_interface}_seed_resize_from_h")
 
-    random_seed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[seed])
-    random_subseed.click(fn=lambda: -1, show_progress=False, inputs=[], outputs=[subseed])
+    target_interface_state = gr.Textbox(target_interface, visible=False)
+    random_seed.click(fn=None, _js="setRandomSeed", show_progress=False, inputs=[target_interface_state], outputs=[])
+    random_subseed.click(fn=None, _js="setRandomSubseed", show_progress=False, inputs=[target_interface_state], outputs=[])
 
     def change_visibility(show):
         return {comp: gr_show(show) for comp in seed_extras}
@@ -574,7 +575,7 @@ def create_ui():
             txt2img_prompt.submit(**txt2img_args)
             submit.click(**txt2img_args)
 
-            res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
+            res_switch_btn.click(fn=None, _js="switchWidthHeightTxt2Img", inputs=None, outputs=None, show_progress=False)
 
             restore_progress_button.click(
                 fn=progress.restore_progress,
@@ -949,7 +950,8 @@ def select_img2img_tab(tab):
 
             img2img_prompt.submit(**img2img_args)
             submit.click(**img2img_args)
-            res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height], show_progress=False)
+
+            res_switch_btn.click(fn=None, _js="switchWidthHeightImg2Img", inputs=None, outputs=None, show_progress=False)
 
             restore_progress_button.click(
                 fn=progress.restore_progress,

**Describe what this pull request is trying to achieve.**

Since some of the UI buttons do a simple action like set the seed to `-1`, can improve responsiveness by offloading that behavior to JS, instead of taking a full round trip from the backend to frontend then back again

This makes the response to clicking the "reset seed" buttons near-instant

**Additional notes and description of your changes**

Gradio event handlers are bypassed and the reset logic was moved into JS

**Environment this was tested in**

List the environment you have developed / tested this on. As per the contributing page, changes should be able to work on Windows out of the box.
 - OS: Windows
 - Browser: Chrome
 - Graphics card: NVIDIA RTX 3090

**Screenshots or videos of your changes**
N/A