├── .gitignore ├── images ├── div_pic.png ├── select.png ├── division.png ├── lat_temp.png ├── lr_helper.png ├── lr_helper1.png ├── lr_helper2.png ├── reg_temp.png ├── lat_settings.png ├── reg_settings.png ├── weight_back.png ├── latent_output.png ├── regional_output.png └── weight_back_rel.png ├── LICENSE ├── README_jp.md ├── README.md └── scripts └── latent_regional_helper.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore .pyc files 2 | *.pyc 3 | 4 | # Ignore log files 5 | *.log 6 | -------------------------------------------------------------------------------- /images/div_pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/div_pic.png -------------------------------------------------------------------------------- /images/select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/select.png -------------------------------------------------------------------------------- /images/division.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/division.png -------------------------------------------------------------------------------- /images/lat_temp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/lat_temp.png -------------------------------------------------------------------------------- /images/lr_helper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/lr_helper.png -------------------------------------------------------------------------------- /images/lr_helper1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/lr_helper1.png -------------------------------------------------------------------------------- /images/lr_helper2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/lr_helper2.png -------------------------------------------------------------------------------- /images/reg_temp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/reg_temp.png -------------------------------------------------------------------------------- /images/lat_settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/lat_settings.png -------------------------------------------------------------------------------- /images/reg_settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/reg_settings.png -------------------------------------------------------------------------------- /images/weight_back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/weight_back.png -------------------------------------------------------------------------------- /images/latent_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/latent_output.png -------------------------------------------------------------------------------- /images/regional_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/regional_output.png -------------------------------------------------------------------------------- /images/weight_back_rel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/safubuki/sd-webui-latent-regional-helper/HEAD/images/weight_back_rel.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 safubuki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README_jp.md: -------------------------------------------------------------------------------- 1 | # sd-webui-latent-regional-helper 2 | 3 | [English(英語)](./README.md) | [Japanese(日本語)](./README_jp.md) 4 | 5 | ## 目次 6 | 7 | - [sd-webui-latent-regional-helper](#sd-webui-latent-regional-helper) 8 | - [目次](#目次) 9 | - [リリースノート](#リリースノート) 10 | - [概要](#概要) 11 | - [インストール方法](#インストール方法) 12 | - [使い方](#使い方) 13 | - [共通設定](#共通設定) 14 | - [個別設定と出力結果利用](#個別設定と出力結果利用) 15 | - [Latent Coupleの場合](#latent-coupleの場合) 16 | - [Regional Prompterの場合](#regional-prompterの場合) 17 | - [リンク](#リンク) 18 | - [ライセンス](#ライセンス) 19 | - [最後に](#最後に) 20 | 21 | ## リリースノート 22 | 23 | - 2024/02/13 24 | - デザインの変更 25 | - プロンプトのテンプレートを出力する機能を追加 26 | - プロンプトのテンプレートをtxt2img、またはimg2imgに送信する機能を追加 27 | 28 | ## 概要 29 | 30 | `Stable Diffusion Web UI`は、テキストから画像を生成したり、既存の画像にテキストを加えて変化させたりできるAIツールです。そして、このツールには`Latent Couple`と`Regional Prompter`という2つの拡張機能があります。これらは、画像を複数の領域に分割して、それぞれの領域に異なるテキスト(プロンプト)を適用することで、より自由に画像を生成し変化させる機能を持ちます。 31 | 32 | 画像生成の可能性を広げる非常に便利な機能ですが、領域分割のための設定が難しく、私はしばしば頭を悩ませていました。そこで私は、領域分割の設定を簡単にするために`Latent Regional Helper`という拡張機能を作成しました。この拡張機能は、各行の列の数をドロップダウンリストから選択するだけで、領域分割のための設定値を出力することができます。 33 | 34 | `Latent Regional Helper`は、とても小さいプログラムですが、あなたの領域分割の助けとなり、悩みから解放するでしょう。 35 | 36 | ![lr_helper1](./images/lr_helper1.png) 37 | ![lr_helper2](./images/lr_helper2.png) 38 | 39 | ## インストール方法 40 | 41 | 1. WebUIを起動 42 | 2. `Extensions`タブを開く 43 | 3. `Install from URL`タブを開く 44 | 4. `URL for extension's git repository` テキストボックスに 45 | 46 | を入力 47 | 5. `Install`ボタンをクリック 48 | 6. インストールが完了したら、`Installed`タブを開く 49 | 7. `Apply and restart UI`ボタンをクリックしてWebUIを再起動 50 | 51 | ## 使い方 52 | 53 | 以下に`Latent Regional Helper`の使い方を示す。 54 | 55 | ### 共通設定 56 | 57 | 1. 拡張機能のタブを開く 58 | - `LR Helper`タブを開く 59 | 2. 出力フォーマットを選択 60 | - `Latent Couple` か `Regional Prompter` のどちらか一方を選択する 61 | ![select](./images/select.png) 62 | 3. 領域分割を設定 63 | - `row1 column num`から`row5 column num`にそれぞれ列数を設定する 64 | ![division](./images/division.png) 65 | - NOTE: 66 | - 列数が0の行は、スキップする 67 | - 列数が設定されている行の間に、列数が0の行がある場合、その行はスキップされて、行の間を詰める 68 | - 最大で5(行)×5(列)の分割が可能 69 | - もし、次のように設定したい場合 70 | ![div_pic](./images/div_pic.png) 71 | `Divisions Settings`は次のように設定する 72 | - `row1 column num` = 1 73 | - `row2 column num` = 5 74 | - `row3 column num` = 2 75 | - `row4 column num` = 0 76 | - `row5 column num` = 0 77 | 78 | ### 個別設定と出力結果利用 79 | 80 | #### Latent Coupleの場合 81 | 82 | 1. Weightと背景の設定 83 | - 共通設定で`Latent Couple`を選択した場合、次の設定ができる 84 | ![lat_settings](./images/lat_settings.png) 85 | - Divisions Weight 86 | - `Divisions Weight`に分割領域のベースとなるweightを設定 87 | - Background Weight and Enable setting 88 | - `Background Weight`に背景のweightを設定 89 | - Background Enableにチェックを入れる 90 | - 設定は、`Latent Couple`の次の箇所に利用される 91 | ![weight_back_rel](./images/weight_back_rel.png) 92 | Enable Backgroundは、後述のPrompt Templeteにも利用する 93 | 94 | 2. 実行 95 | - 全てのInput設定が済んだら`execute`ボタンをクリック 96 | - Outputのテキストボックスに結果が出力される 97 | 98 | 3. 出力結果の利用 99 | - 分割設定の出力結果は、手動で`Latent Couple`にコピー&ペースト 100 | ![latent_output](./images/latent_output.png) 101 | - Prompt Templateは、`Send to txt2img`または`Send to img2img`ボタンをクリックすると、`txt2img` `img2img`のプロンプトエリアにコピーできる 102 | ![lat_temp](./images/lat_temp.png) 103 | 104 | #### Regional Prompterの場合 105 | 106 | 1. Base and Common プロンプト設定 107 | - 共通設定で`Regional Prompter`を選択した場合、次の設定ができる 108 | ![reg_settings](./images/reg_settings.png) 109 | - Base and Common Prompt Settings 110 | - それぞれ、チェックを入れるとOutputの`Prompt Template`に`ADDBASE` `ADDCOMM`が出力される 111 | なお、このチェックは分割設定の出力には影響を与えない 112 | 113 | 2. 実行 114 | - 全てのInput設定が済んだら`execute`ボタンをクリック 115 | - Outputのテキストボックスに結果が出力される 116 | 117 | 3. 出力結果の利用 118 | - 分割設定の出力結果は、手動で`Regional Prompter`にコピー&ペースト 119 | ![regional_output](./images/regional_output.png) 120 | - `Position`と`Weight`は使用しない 121 | - `Regional Prompter`の`Main Splitting`設定は`Columns`を選択すること 122 | - Prompt Templateは、`Send to txt2img`または`Send to img2img`ボタンをクリックすると、`txt2img` `img2img`のプロンプトエリアにコピーできる 123 | ![reg_temp](./images/reg_temp.png) 124 | 4. 以降は、`Latent Couple`または`Regional Prompter`で作業を進める 125 | 126 | ## リンク 127 | 128 | - Latent Couple (original) 129 | 130 | - Latent Couple (forked) 131 | 132 | - Regional Prompter(original) 133 | 134 | 135 | ## ライセンス 136 | 137 | ライセンスファイルを参照のこと 138 | [LICENSEファイルへ](./LICENSE) 139 | 140 | ## 最後に 141 | 142 | もし問題、要望などありましたら、issueでご連絡ください。 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sd-webui-latent-regional-helper 2 | 3 | [English(英語)](./README.md) | [Japanese(日本語)](./README_jp.md) 4 | 5 | ## Table of Contents 6 | 7 | - [sd-webui-latent-regional-helper](#sd-webui-latent-regional-helper) 8 | - [Table of Contents](#table-of-contents) 9 | - [Release Notes](#release-notes) 10 | - [Overview](#overview) 11 | - [Installation](#installation) 12 | - [Usage](#usage) 13 | - [Common Setting](#common-setting) 14 | - [Individual Settings and Output Utilization](#individual-settings-and-output-utilization) 15 | - [For Latent Couple](#for-latent-couple) 16 | - [For Regional Prompter](#for-regional-prompter) 17 | - [Links](#links) 18 | - [License](#license) 19 | - [Conclusion](#conclusion) 20 | 21 | ## Release Notes 22 | 23 | - 2024/02/13 24 | - Design changes 25 | - Added functionality to output prompt templates 26 | - Added functionality to send prompt templates to txt2img or img2img 27 | 28 | ## Overview 29 | 30 | `Stable Diffusion Web UI` is an AI tool that can generate images from text and modify existing images by adding text. It includes two extensions called `Latent Couple` and `Regional Prompter`. These extensions allow you to divide an image into multiple regions and apply different text (prompts) to each region, enabling more flexible image generation and modification. 31 | 32 | While this feature greatly expands the possibilities of image generation, configuring the region division can be challenging and often causes confusion. To simplify the region division settings, I created an extension called `Latent Regional Helper`. This extension allows you to easily output the configuration values for region division by selecting the number of columns for each row from a dropdown list. 33 | 34 | `Latent Regional Helper` is a small program that will assist you with region division and free you from the confusion. 35 | 36 | ![lr_helper1](./images/lr_helper1.png) 37 | ![lr_helper2](./images/lr_helper2.png) 38 | 39 | ## Installation 40 | 41 | 1. Launch the WebUI. 42 | 2. Open the `Extensions` tab. 43 | 3. Open the `Install from URL` tab. 44 | 4. Enter the following URL in the `URL for extension's git repository` text box: 45 | 46 | 5. Click the `Install` button. 47 | 6. After the installation is complete, open the `Installed` tab. 48 | 7. Click the `Apply and restart UI` button to restart the WebUI. 49 | 50 | ## Usage 51 | 52 | Here is how to use `Latent Regional Helper`: 53 | 54 | ### Common Setting 55 | 56 | 1. Open the Extensions tab. 57 | - Open the `LR Helper` tab. 58 | 2. Select the output format. 59 | - Choose either `Latent Couple` or `Regional Prompter`. 60 | ![select](./images/select.png) 61 | 3. Configure the region division. 62 | - Set the number of columns for each row from `row1 column num` to `row5 column num`. 63 | ![division](./images/division.png) 64 | - NOTE: 65 | - Rows with a column number of 0 will be skipped. 66 | - If there is a row with a column number of 0 between rows with configured column numbers, that row will be skipped and the rows will be compacted. 67 | - Up to a maximum of 5 rows and 5 columns can be divided. 68 | - If you want to configure it as follows: 69 | ![div_pic](./images/div_pic.png) 70 | Set the `Divisions Settings` as follows: 71 | - `row1 column num` = 1 72 | - `row2 column num` = 5 73 | - `row3 column num` = 2 74 | - `row4 column num` = 0 75 | - `row5 column num` = 0 76 | 77 | ### Individual Settings and Output Utilization 78 | 79 | #### For Latent Couple 80 | 81 | 1. Weight and Background Settings 82 | - If you select `Latent Couple` in the common settings, you can configure the following settings: 83 | ![lat_settings](./images/lat_settings.png) 84 | - Divisions Weight 85 | - Set the weight for the divided regions in the `Divisions Weight` field. 86 | - Background Weight and Enable setting 87 | - Set the weight for the background in the `Background Weight` field. 88 | - Check the `Background Enable` checkbox. 89 | - These settings will be used in the following section of `Latent Couple`: 90 | ![weight_back_rel](./images/weight_back_rel.png) 91 | The `Enable Background` setting is also used in the Prompt Template mentioned below. 92 | 93 | 2. Execution 94 | - Once all input settings are done, click the `execute` button. 95 | - The result will be output in the text box under Output. 96 | 97 | 3. Utilizing the Output 98 | - Manually copy and paste the output of the division settings to `Latent Couple`. 99 | ![latent_output](./images/latent_output.png) 100 | - Clicking the `Send to txt2img` or `Send to img2img` button will copy the Prompt Template to the prompt area of `txt2img` or `img2img`. 101 | ![lat_temp](./images/lat_temp.png) 102 | 103 | #### For Regional Prompter 104 | 105 | 1. Base and Common Prompt Settings 106 | - If you select `Regional Prompter` in the common settings, you can configure the following settings: 107 | ![reg_settings](./images/reg_settings.png) 108 | - Base and Common Prompt Settings 109 | - Checking these options will output `ADDBASE` and `ADDCOMM` in the `Prompt Template` of the Output. 110 | Note that these checkboxes do not affect the output of the division settings. 111 | 112 | 2. Execution 113 | - Once all input settings are done, click the `execute` button. 114 | - The result will be output in the text box under Output. 115 | 116 | 3. Utilizing the Output 117 | - Manually copy and paste the output of the division settings to `Regional Prompter`. 118 | ![regional_output](./images/regional_output.png) 119 | - `Position` and `Weight` are not used. 120 | - Select `Columns` for the `Main Splitting` setting in `Regional Prompter`. 121 | - Clicking the `Send to txt2img` or `Send to img2img` button will copy the Prompt Template to the prompt area of `txt2img` or `img2img`. 122 | ![reg_temp](./images/reg_temp.png) 123 | 4. Continue working with either `Latent Couple` or `Regional Prompter`. 124 | 125 | ## Links 126 | 127 | - Latent Couple (original) 128 | 129 | - Latent Couple (forked) 130 | 131 | - Regional Prompter (original) 132 | 133 | 134 | ## License 135 | 136 | Please refer to the license file for details. 137 | [Go to LICENSE file](./LICENSE) 138 | 139 | ## Conclusion 140 | 141 | If you have any issues or requests, please contact me through the issue tracker. 142 | -------------------------------------------------------------------------------- /scripts/latent_regional_helper.py: -------------------------------------------------------------------------------- 1 | """Script that defines the UI and processing for Latent Regional Helper.""" 2 | from typing import List, Tuple 3 | 4 | # NOTE: gradio is available in the StableDiffusion environment (no separate installation required) 5 | import gradio as gr 6 | 7 | from modules import generation_parameters_copypaste as params_copypaste 8 | from modules import script_callbacks 9 | 10 | # import modules.scripts as scripts 11 | 12 | # Default values for weight 13 | default_div_weight: float = 0.8 14 | default_back_weight: float = 0.2 15 | # Minimum/maximum values for weight 16 | weight_threshold_min: float = 0.0 17 | weight_threshold_max: float = 1.0 18 | 19 | 20 | def div_latent_couple(exist_col_num_list: List[str], div_weight: float, back_weight: float, 21 | chkbox_back: bool) -> Tuple[str, str, str, str]: 22 | """ 23 | Create division parameters for Latent Couple. 24 | 25 | Args: 26 | exist_col_num_list (List[str]): List of column numbers for existing divisions. 27 | div_weight (float): weight for divided regions setting. 28 | back_weight (float): weight for the background setting. 29 | chkbox_back (bool): Flag indicating whether the background setting is enabled. 30 | 31 | Returns: 32 | Tuple[str, str, str, str]: A tuple containing the division, position, weight, and prompt. 33 | """ 34 | division: str = '' 35 | position: str = '' 36 | weight: str = '' 37 | prompt: str = '' 38 | # If background setting is enabled 39 | if chkbox_back is True: 40 | division += '1:1,' 41 | position += '0:0,' 42 | weight += str(clamp_value(back_weight, weight_threshold_min, weight_threshold_max)) + ',' 43 | prompt += '\nAND ' 44 | else: 45 | pass 46 | # Setting for divided regions 47 | pos_row: int = 0 48 | pos_col: int = 0 49 | for col_num in exist_col_num_list: 50 | for _ in range(int(col_num)): 51 | # Add division, position, and weight 52 | division += str(len(exist_col_num_list)) + ':' + str(col_num) + ',' 53 | position += str(pos_row) + ':' + str(pos_col) + ',' 54 | weight += str(clamp_value(div_weight, weight_threshold_min, weight_threshold_max)) + ',' 55 | pos_col += 1 56 | # Add prompt 57 | prompt += '\nAND ' * (int(col_num) - 1) # Add AND for each column 58 | prompt += '\nAND ' # Add AND for each row 59 | pos_col = 0 60 | pos_row += 1 61 | # remove trailing commas and AND 62 | division = division.rstrip(',') 63 | position = position.rstrip(',') 64 | weight = weight.rstrip(',') 65 | prompt = prompt[:prompt.rfind('\nAND ')] # Remove last occurrence of '\nAND ' 66 | 67 | return division, position, weight, prompt 68 | 69 | 70 | def div_regional_prompter(exist_col_num_list: List[str], chkbox_base_prompt: bool, 71 | chkbox_common_prompt: bool) -> Tuple[str, str, str, str]: 72 | """ 73 | Generate division, position, weight, and prompt based on the given parameters. 74 | 75 | Args: 76 | exist_col_num_list (List[str]): A list of column numbers. 77 | chkbox_base_prompt (bool): A boolean indicating whether the base prompt checkbox is checked. 78 | chkbox_common_prompt (bool): 79 | A boolean indicating whether the common prompt checkbox is checked. 80 | 81 | Returns: 82 | Tuple[str, str, str, str]: A tuple containing the division, position, weight, and prompt. 83 | 84 | """ 85 | division: str = '' 86 | prompt: str = '' 87 | 88 | # Add base and common prompt settings 89 | if chkbox_common_prompt: 90 | prompt += ' ADDCOMM\n' 91 | if chkbox_base_prompt: 92 | prompt += ' ADDBASE\n' 93 | 94 | # Add division settings 95 | if len(exist_col_num_list) >= 1: 96 | for col_num in exist_col_num_list: 97 | # Add division settings 98 | division += '1' + (',1' * int(col_num)) 99 | division += ';' 100 | # Add prompt settings 101 | prompt += ' ADDCOL\n' * (int(col_num) - 1) 102 | prompt += ' ADDROW\n' 103 | # Remove trailing semicolons and ADDROW 104 | division = division.rstrip(';') # Remove trailing semicolons 105 | prompt = prompt[:prompt.rfind(' ADDROW\n')] # Remove last occurrence of ' ADDROW\n' 106 | else: 107 | division = '(Not used)' 108 | prompt = '(Not used)' 109 | 110 | position: str = '(Not used)' 111 | weight: str = '(Not used)' 112 | 113 | return division, position, weight, prompt 114 | 115 | 116 | def division_output(radio_sel: str, col_num_1: str, col_num_2: str, col_num_3: str, col_num_4: str, 117 | col_num_5: str, div_weight: str, back_weight: str, chkbox_back: bool, 118 | chkbox_base_prompt: bool, 119 | chkbox_common_prompt: bool) -> Tuple[str, str, str, str]: 120 | """ 121 | Create division parameters. 122 | 123 | Args: 124 | radio_sel (str): The selected radio button value. 125 | col_num_1 (str): The column number for row 1. 126 | col_num_2 (str): The column number for row 2. 127 | col_num_3 (str): The column number for row 3. 128 | col_num_4 (str): The column number for row 4. 129 | col_num_5 (str): The column number for row 5. 130 | div_weight (str): The division weight. 131 | back_weight (str): The background weight. 132 | chkbox_back (bool): Flag indicating whether the background setting is enabled. 133 | 134 | Returns: 135 | Tuple[str, str, str]: A tuple containing the division, position, weight and prompt. 136 | """ 137 | # Combine dropdown list into a list 138 | col_num_list: List[str] = [col_num_1, col_num_2, col_num_3, col_num_4, col_num_5] 139 | 140 | # Extract non-zero and non-empty values from the dropdown list 141 | exist_col_num_list: List[str] = [] 142 | for col_num in col_num_list: 143 | if isinstance(col_num, str) and col_num != '0' and col_num != '': 144 | exist_col_num_list.append(col_num) 145 | 146 | if len(exist_col_num_list) == 0: 147 | # If there is no input 148 | division = '(No divisions settings)' 149 | position = '(No divisions settings)' 150 | weight = '(No divisions settings)' 151 | prompt = '(No divisions settings)' 152 | else: 153 | if radio_sel == 'Latent Couple': 154 | # Parse weight values 155 | div_weight_f = parse_float(div_weight, default_div_weight) 156 | back_weight_f = parse_float(back_weight, default_back_weight) 157 | # Process for Latent Couple 158 | division, position, weight, prompt = div_latent_couple(exist_col_num_list, div_weight_f, 159 | back_weight_f, chkbox_back) 160 | elif radio_sel == 'Regional Prompter': 161 | # Process for Regional Prompter 162 | division, position, weight, prompt = div_regional_prompter( 163 | exist_col_num_list, chkbox_base_prompt, chkbox_common_prompt) 164 | else: 165 | # Error handling: If an invalid selection is made 166 | division = '(Latent selection error)' 167 | position = '(Latent selection error)' 168 | weight = '(Latent selection error)' 169 | prompt = '(Latent selection error)' 170 | 171 | return division, position, weight, prompt 172 | 173 | 174 | def parse_float(value: str, default_value: float) -> float: 175 | """ 176 | Parse a string value to float. 177 | 178 | Args: 179 | value (str): The value to be parsed. 180 | default_value (float): The default value to be returned if parsing fails. 181 | 182 | Returns: 183 | float: The parsed float value or the default value. 184 | """ 185 | try: 186 | return float(value) 187 | except ValueError: 188 | return default_value 189 | 190 | 191 | def clamp_value(value: float, min_value: float, max_value: float) -> float: 192 | """ 193 | Clamp a value between a minimum and maximum value. 194 | 195 | Args: 196 | value (float): The value to be clamped. 197 | min_value (float): The minimum value. 198 | max_value (float): The maximum value. 199 | 200 | Returns: 201 | float: The clamped value. 202 | """ 203 | return max(min_value, min(max_value, float(value))) 204 | 205 | 206 | def on_ui_tabs() -> List[Tuple[gr.Blocks, str, str]]: 207 | """ 208 | Define the UI components and their behavior. 209 | 210 | Returns: 211 | List[Tuple[gr.Blocks, str, str]]: A list of tuples containing the UI components, 212 | tab name, and tab ID. 213 | """ 214 | # Create UI screen 215 | with gr.Blocks(analytics_enabled=False) as ui_component: 216 | gr.HTML(value='Latent Regional Helper') 217 | gr.HTML(value='Input') 218 | with gr.Column(): 219 | # Input 220 | gr.HTML(value='Common Settings') 221 | # Radio buttons for extension selection 222 | radio_sel: gr.Radio = gr.Radio( 223 | ['Latent Couple', 'Regional Prompter'], 224 | label='Select output format \'Latent Couple\' or \'Regional Prompter\'', 225 | value='Latent Couple' # Specify default value 226 | ) 227 | # Dropdown list for Divisions Setting 228 | gr.HTML(value='Divisions') 229 | dropdown_col_num_list: List[gr.Dropdown] = [] 230 | for i in range(5): 231 | dropdown_col_num_list.append( 232 | gr.Dropdown( 233 | ['0', '1', '2', '3', '4', '5'], 234 | label=f'row{i+1} column num', 235 | value='0' # Specify default value 236 | )) 237 | gr.HTML(value='Individual Settings') 238 | with gr.Row(): 239 | with gr.Column(variant='panel'): # Add a new column 240 | gr.HTML(value='Latent Couple Settings') 241 | gr.HTML(value='Weight and Background') 242 | with gr.Row(): 243 | textbox_div_weight: gr.Textbox = gr.Textbox(label='Divisions Weight', 244 | interactive=True, 245 | value=str(default_div_weight)) 246 | textbox_back_weight: gr.Textbox = gr.Textbox(label='Background Weight', 247 | interactive=True, 248 | value=str(default_back_weight)) 249 | chkbox_back: gr.Checkbox = gr.Checkbox(label='Enable Background', 250 | value=False) 251 | with gr.Column(variant='panel'): # Add a new column 252 | gr.HTML(value='Regional Prompter Settings') 253 | gr.HTML(value='Base and Common Prompt Settings') 254 | with gr.Row(): 255 | # Checkbox for Use base prompt 256 | chkbox_base_prompt: gr.Checkbox = gr.Checkbox(label='Use base prompt', 257 | value=False) 258 | 259 | # Checkbox for Use common prompt 260 | chkbox_common_prompt: gr.Checkbox = gr.Checkbox(label='Use common prompt', 261 | value=False) 262 | # Run button 263 | button_execute: gr.Button = gr.Button(value='execute', variant='primary') 264 | # Output 265 | gr.HTML(value='Output') 266 | # Textbox for output 267 | textbox_division: gr.Textbox = gr.Textbox(label='Divisions Ratio', interactive=True) 268 | with gr.Row(): 269 | textbox_weight: gr.Textbox = gr.Textbox(label='Weight (Latent Only)', 270 | interactive=True) 271 | textbox_position: gr.Textbox = gr.Textbox(label='Position (Latent Only)', 272 | interactive=True) 273 | # Textbox for prompt template output 274 | textbox_prompt_output: gr.Textbox = gr.Textbox(label='Prompt Template', 275 | interactive=True) 276 | # Button to copy the contents of prompt_output to txt2img and img2img 277 | gr.HTML(value='Send to Prompt Template to txt2img or img2img') 278 | with gr.Row(): 279 | buttons = params_copypaste.create_buttons(['txt2img', 'img2img']) 280 | params_copypaste.bind_buttons(buttons, None, textbox_prompt_output) 281 | 282 | # Processing when button_execute is clicked 283 | button_execute.click( 284 | # Function to be executed when button_execute is clicked 285 | fn=division_output, 286 | # Arguments for the division_output function 287 | # NOTE: column_num_row_list cannot be passed as a list. 288 | # It needs to be passed as a gradio block object. 289 | inputs=[ 290 | radio_sel, dropdown_col_num_list[0], dropdown_col_num_list[1], 291 | dropdown_col_num_list[2], dropdown_col_num_list[3], dropdown_col_num_list[4], 292 | textbox_div_weight, textbox_back_weight, chkbox_back, chkbox_base_prompt, 293 | chkbox_common_prompt 294 | ], 295 | # Return values of the division_output function 296 | outputs=[textbox_division, textbox_position, textbox_weight, textbox_prompt_output]) 297 | 298 | return [(ui_component, 'LR Helper', 'lr_helper_tab')] 299 | 300 | 301 | script_callbacks.on_ui_tabs(on_ui_tabs) 302 | --------------------------------------------------------------------------------