|
| 1 | +import gradio as gr |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from matplotlib.patches import Patch |
| 4 | +import io |
| 5 | +import json |
| 6 | +from PIL import Image |
| 7 | +from typing import List |
| 8 | + |
| 9 | +from scripts.enums import StableDiffusionVersion |
| 10 | +from scripts.global_state import get_sd_version |
| 11 | +from scripts.ipadapter.weight import calc_weights |
| 12 | + |
| 13 | + |
| 14 | +INPUT_BLOCK_COLOR = "#61bdee" |
| 15 | +MIDDLE_BLOCK_COLOR = "#e2e2e2" |
| 16 | +OUTPUT_BLOCK_COLOR = "#dc6e55" |
| 17 | + |
| 18 | + |
| 19 | +def get_bar_colors( |
| 20 | + sd_version: StableDiffusionVersion, input_color, middle_color, output_color |
| 21 | +): |
| 22 | + middle_block_idx = 4 if sd_version == StableDiffusionVersion.SDXL else 6 |
| 23 | + |
| 24 | + def get_color(idx): |
| 25 | + if idx < middle_block_idx: |
| 26 | + return input_color |
| 27 | + elif idx == middle_block_idx: |
| 28 | + return middle_color |
| 29 | + else: |
| 30 | + return output_color |
| 31 | + |
| 32 | + return [get_color(i) for i in range(sd_version.transformer_block_num)] |
| 33 | + |
| 34 | + |
| 35 | +def plot_weights( |
| 36 | + numbers: List[float], |
| 37 | + colors: List[str], |
| 38 | +): |
| 39 | + # Create a bar chart |
| 40 | + plt.figure(figsize=(8, 4)) |
| 41 | + plt.bar(range(len(numbers)), numbers, color=colors) |
| 42 | + plt.xlabel("Transformer Index") |
| 43 | + plt.ylabel("Weight") |
| 44 | + plt.legend( |
| 45 | + handles=[ |
| 46 | + Patch(color=color, label=label) |
| 47 | + for color, label in ( |
| 48 | + (INPUT_BLOCK_COLOR, "Input Block"), |
| 49 | + (MIDDLE_BLOCK_COLOR, "Middle Block"), |
| 50 | + (OUTPUT_BLOCK_COLOR, "Output Block"), |
| 51 | + ) |
| 52 | + ], |
| 53 | + loc="best", |
| 54 | + ) |
| 55 | + |
| 56 | + # Save the plot to a BytesIO buffer |
| 57 | + buffer = io.BytesIO() |
| 58 | + plt.savefig(buffer, format="png") |
| 59 | + plt.close() |
| 60 | + buffer.seek(0) |
| 61 | + |
| 62 | + # Convert the buffer to a PIL image and return it |
| 63 | + image = Image.open(buffer) |
| 64 | + return image |
| 65 | + |
| 66 | + |
| 67 | +class AdvancedWeightControl: |
| 68 | + def __init__(self): |
| 69 | + self.group = None |
| 70 | + self.weight_type = None |
| 71 | + self.weight_plot = None |
| 72 | + self.weight_editor = None |
| 73 | + self.weight_composition = None |
| 74 | + |
| 75 | + def render(self): |
| 76 | + with gr.Group(visible=False) as self.group: |
| 77 | + with gr.Row(): |
| 78 | + self.weight_type = gr.Dropdown( |
| 79 | + choices=[ |
| 80 | + "normal", |
| 81 | + "ease in", |
| 82 | + "ease out", |
| 83 | + "ease in-out", |
| 84 | + "reverse in-out", |
| 85 | + "weak input", |
| 86 | + "weak output", |
| 87 | + "weak middle", |
| 88 | + "strong middle", |
| 89 | + "style transfer", |
| 90 | + "composition", |
| 91 | + "strong style transfer", |
| 92 | + "style and composition", |
| 93 | + "strong style and composition", |
| 94 | + ], |
| 95 | + label="Weight Type", |
| 96 | + value="normal", |
| 97 | + ) |
| 98 | + self.weight_composition = gr.Slider( |
| 99 | + label="Composition Weight", |
| 100 | + minimum=0, |
| 101 | + maximum=2.0, |
| 102 | + value=1.0, |
| 103 | + step=0.01, |
| 104 | + visible=False, |
| 105 | + ) |
| 106 | + self.weight_editor = gr.Textbox(label="Weights", visible=False) |
| 107 | + |
| 108 | + self.weight_plot = gr.Image( |
| 109 | + value=None, |
| 110 | + label="Weight Plot", |
| 111 | + interactive=False, |
| 112 | + visible=False, |
| 113 | + ) |
| 114 | + |
| 115 | + def register_callbacks( |
| 116 | + self, |
| 117 | + weight_input: gr.Slider, |
| 118 | + advanced_weighting: gr.State, |
| 119 | + control_type: gr.Radio, |
| 120 | + update_unit_counter: gr.Number, |
| 121 | + ): |
| 122 | + def advanced_weighting_supported(control_type: str) -> bool: |
| 123 | + return control_type in ("IP-Adapter", "Instant-ID") |
| 124 | + |
| 125 | + self.weight_type.change( |
| 126 | + fn=lambda weight_type: gr.update( |
| 127 | + visible=weight_type |
| 128 | + in ("style and composition", "strong style and composition") |
| 129 | + ), |
| 130 | + inputs=[self.weight_type], |
| 131 | + outputs=[self.weight_composition], |
| 132 | + ) |
| 133 | + |
| 134 | + def update_weight_textbox( |
| 135 | + control_type: str, |
| 136 | + weight_type: str, |
| 137 | + weight: float, |
| 138 | + weight_composition: float, |
| 139 | + ): |
| 140 | + if not advanced_weighting_supported(control_type): |
| 141 | + return gr.update() |
| 142 | + |
| 143 | + sd_version = get_sd_version() |
| 144 | + weights = calc_weights(weight_type, weight, sd_version, weight_composition) |
| 145 | + return gr.update(value=str([round(w, 2) for w in weights]), visible=True) |
| 146 | + |
| 147 | + trigger_inputs = [self.weight_type, weight_input, self.weight_composition] |
| 148 | + for trigger_input in trigger_inputs: |
| 149 | + trigger_input.change( |
| 150 | + fn=update_weight_textbox, |
| 151 | + inputs=[ |
| 152 | + control_type, |
| 153 | + self.weight_type, |
| 154 | + weight_input, |
| 155 | + self.weight_composition, |
| 156 | + ], |
| 157 | + outputs=[self.weight_editor], |
| 158 | + ) |
| 159 | + |
| 160 | + def update_plot(weights_string: str): |
| 161 | + try: |
| 162 | + weights = json.loads(weights_string) |
| 163 | + assert isinstance(weights, list) |
| 164 | + except Exception: |
| 165 | + return gr.update(visible=False) |
| 166 | + |
| 167 | + sd_version = get_sd_version() |
| 168 | + weight_plot = plot_weights( |
| 169 | + weights, |
| 170 | + get_bar_colors( |
| 171 | + sd_version, |
| 172 | + input_color=INPUT_BLOCK_COLOR, |
| 173 | + middle_color=MIDDLE_BLOCK_COLOR, |
| 174 | + output_color=OUTPUT_BLOCK_COLOR, |
| 175 | + ), |
| 176 | + ) |
| 177 | + return gr.update(value=weight_plot, visible=True) |
| 178 | + |
| 179 | + def update_advanced_weighting(weights_string: str): |
| 180 | + try: |
| 181 | + weights = json.loads(weights_string) |
| 182 | + assert isinstance(weights, list) |
| 183 | + except Exception: |
| 184 | + return None |
| 185 | + return weights |
| 186 | + |
| 187 | + self.weight_editor.change( |
| 188 | + fn=update_plot, |
| 189 | + inputs=[self.weight_editor], |
| 190 | + outputs=[self.weight_plot], |
| 191 | + ) |
| 192 | + |
| 193 | + self.weight_editor.change( |
| 194 | + fn=update_advanced_weighting, |
| 195 | + inputs=[self.weight_editor], |
| 196 | + outputs=[advanced_weighting], |
| 197 | + ).then( |
| 198 | + fn=lambda x: gr.update(value=x + 1), |
| 199 | + inputs=[update_unit_counter], |
| 200 | + outputs=[update_unit_counter], |
| 201 | + ) # Necessary to flush gr.State change to unit state. |
| 202 | + |
| 203 | + # TODO: Expose advanced weighting control for other control types. |
| 204 | + def control_type_change(control_type: str, old_weights): |
| 205 | + supported = advanced_weighting_supported(control_type) |
| 206 | + if supported: |
| 207 | + return ( |
| 208 | + gr.update(visible=supported), |
| 209 | + old_weights, |
| 210 | + gr.update(), |
| 211 | + gr.update(), |
| 212 | + ) |
| 213 | + else: |
| 214 | + return ( |
| 215 | + gr.update(visible=supported), |
| 216 | + None, |
| 217 | + gr.update(visible=False), |
| 218 | + gr.update(visible=False), |
| 219 | + ) |
| 220 | + |
| 221 | + control_type.change( |
| 222 | + fn=control_type_change, |
| 223 | + inputs=[control_type, advanced_weighting], |
| 224 | + outputs=[ |
| 225 | + self.group, |
| 226 | + advanced_weighting, |
| 227 | + self.weight_editor, |
| 228 | + self.weight_plot, |
| 229 | + ], |
| 230 | + ) |
0 commit comments