Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: Error on launching - UnboundLocalError: local variable 'res_switch_btn' referenced before assignment
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

An error occurs on launching.

### Steps to reproduce the problem

Launch app.

Edit: I've worked out that this occurs if the "Show Width/Height and Batch sliders in same row" setting is unchecked.

### What should have happened?

No error.

### Commit where the problem happens

e8a41df49fadd2cf9f23b1f02d75a4947bec5646

### What platforms do you use to access the UI ?

Windows

### What browsers do you use to access the UI ?

Mozilla Firefox

### Command Line Arguments

```Shell
None
```


### List of extensions

No

### Console logs

```Shell
venv "C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\venv\Scripts\Python.exe"
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
Commit hash: e8a41df49fadd2cf9f23b1f02d75a4947bec5646
Installing requirements for Web UI
Launching Web UI with arguments:
No module 'xformers'. Proceeding without it.
Loading weights [cc6cb27103] from C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\models\Stable-diffusion\StableDiffusion_v15.ckpt
Creating model from config: C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\configs\v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Applying cross attention optimization (Doggettx).
Textual inversion embeddings loaded(0):
Model loaded in 3.0s (create model: 0.4s, apply weights to model: 0.7s, apply half(): 0.6s, move model to device: 0.4s, load textual inversion embeddings: 0.8s).
Traceback (most recent call last):
  File "C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\launch.py", line 360, in <module>
    start()
  File "C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\launch.py", line 355, in start
    webui.webui()
  File "C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\webui.py", line 196, in webui
    shared.demo = modules.ui.create_ui()
  File "C:\Users\<USER>\Documents\GitHub\stable-diffusion-webui\modules\ui.py", line 572, in create_ui
    res_switch_btn.click(lambda w, h: (h, w), inputs=[width, height], outputs=[width, height])
UnboundLocalError: local variable 'res_switch_btn' referenced before assignment
Press any key to continue . . .
```


### Additional information

_No response_
Add button to switch width and height
**Describe what this pull request is trying to achieve.**

Adds a button to switch width and height, allowing quick and easy switching between landscape and portrait.

**Additional notes and description of your changes**

I'm not sure how to better adjust the position of the button, this was the best I could come up with - further suggestions are welcomed

**Environment this was tested in**

 - OS: Windows 11 21H2 
 - Browser: Firefox, Chrome, Edge
 - Graphics card: RTX 3050 Ti

**Screenshots or videos of your changes**

![image](https://user-images.githubusercontent.com/73721238/215168280-aec51267-78c3-4238-aeee-343f34e5eb80.png)



Discussion:
Also experiencing this issue.
This must be related to config as I've done a hard-reset and the problem has gone.
Temporary fix, edit the "dimensions_and_batch_together" line in your config.json file so the value is true:

`"dimensions_and_batch_together": true,`
Cannot confirm. Config.json is not being changed/properly read from the settings panel on a 100% stock install.
Even changing the option manually in editor, changing any other value immediately reverts to the default invalid state.

Edit: Did a full reinstall again. The **only** option I changed was to the show width/height in line option. And then saved. Settings said 7 options were changed, and the UI option was not applied. Immediately returns to invalid state.
![firefox_Z0fFBO2Qu8](https://user-images.githubusercontent.com/7834910/215279792-6a899ef9-4f78-4a48-a2a7-d688c0b499ad.png)
Having same issue and can confirm setting "dimensions_and_batch_together": true, works as a workaround. I don't know the code but suspect this relates to the recent update that added a button for switching height/width. 
It seems to be the case. As long as I do not change the width/height inline option, the settings appear to work.
you can do `a,b = b,a` in python

Resolving patch:
diff --git a/modules/ui.py b/modules/ui.py
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -466,8 +466,8 @@ def create_ui():
                                 width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="txt2img_width")
                                 height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="txt2img_height")
 
+                            res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="txt2img_res_switch_btn")
                             if opts.dimensions_and_batch_together:
-                                res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="txt2img_res_switch_btn")
                                 with gr.Column(elem_id="txt2img_column_batch"):
                                     batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="txt2img_batch_count")
                                     batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="txt2img_batch_size")
@@ -737,8 +737,8 @@ def copy_image(img):
                                 width = gr.Slider(minimum=64, maximum=2048, step=8, label="Width", value=512, elem_id="img2img_width")
                                 height = gr.Slider(minimum=64, maximum=2048, step=8, label="Height", value=512, elem_id="img2img_height")
 
+                            res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="img2img_res_switch_btn")
                             if opts.dimensions_and_batch_together:
-                                res_switch_btn = ToolButton(value=switch_values_symbol, elem_id="img2img_res_switch_btn")
                                 with gr.Column(elem_id="img2img_column_batch"):
                                     batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1, elem_id="img2img_batch_count")
                                     batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1, elem_id="img2img_batch_size")


Test patch: