stable-diffusion-webui
123 строки · 5.8 Кб
1import gradio as gr2
3from modules import shared, ui_common, ui_components, styles4
5styles_edit_symbol = '\U0001f58c\uFE0F' # 🖌️6styles_materialize_symbol = '\U0001f4cb' # 📋7styles_copy_symbol = '\U0001f4dd' # 📝8
9
10def select_style(name):11style = shared.prompt_styles.styles.get(name)12existing = style is not None13empty = not name14
15prompt = style.prompt if style else gr.update()16negative_prompt = style.negative_prompt if style else gr.update()17
18return prompt, negative_prompt, gr.update(visible=existing), gr.update(visible=not empty)19
20
21def save_style(name, prompt, negative_prompt):22if not name:23return gr.update(visible=False)24
25existing_style = shared.prompt_styles.styles.get(name)26path = existing_style.path if existing_style is not None else None27
28style = styles.PromptStyle(name, prompt, negative_prompt, path)29shared.prompt_styles.styles[style.name] = style30shared.prompt_styles.save_styles()31
32return gr.update(visible=True)33
34
35def delete_style(name):36if name == "":37return38
39shared.prompt_styles.styles.pop(name, None)40shared.prompt_styles.save_styles()41
42return '', '', ''43
44
45def materialize_styles(prompt, negative_prompt, styles):46prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)47negative_prompt = shared.prompt_styles.apply_negative_styles_to_prompt(negative_prompt, styles)48
49return [gr.Textbox.update(value=prompt), gr.Textbox.update(value=negative_prompt), gr.Dropdown.update(value=[])]50
51
52def refresh_styles():53return gr.update(choices=list(shared.prompt_styles.styles)), gr.update(choices=list(shared.prompt_styles.styles))54
55
56class UiPromptStyles:57def __init__(self, tabname, main_ui_prompt, main_ui_negative_prompt):58self.tabname = tabname59self.main_ui_prompt = main_ui_prompt60self.main_ui_negative_prompt = main_ui_negative_prompt61
62with gr.Row(elem_id=f"{tabname}_styles_row"):63self.dropdown = gr.Dropdown(label="Styles", show_label=False, elem_id=f"{tabname}_styles", choices=list(shared.prompt_styles.styles), value=[], multiselect=True, tooltip="Styles")64edit_button = ui_components.ToolButton(value=styles_edit_symbol, elem_id=f"{tabname}_styles_edit_button", tooltip="Edit styles")65
66with gr.Box(elem_id=f"{tabname}_styles_dialog", elem_classes="popup-dialog") as styles_dialog:67with gr.Row():68self.selection = gr.Dropdown(label="Styles", elem_id=f"{tabname}_styles_edit_select", choices=list(shared.prompt_styles.styles), value=[], allow_custom_value=True, info="Styles allow you to add custom text to prompt. Use the {prompt} token in style text, and it will be replaced with user's prompt when applying style. Otherwise, style's text will be added to the end of the prompt.")69ui_common.create_refresh_button([self.dropdown, self.selection], shared.prompt_styles.reload, lambda: {"choices": list(shared.prompt_styles.styles)}, f"refresh_{tabname}_styles")70self.materialize = ui_components.ToolButton(value=styles_materialize_symbol, elem_id=f"{tabname}_style_apply_dialog", tooltip="Apply all selected styles from the style selction dropdown in main UI to the prompt.")71self.copy = ui_components.ToolButton(value=styles_copy_symbol, elem_id=f"{tabname}_style_copy", tooltip="Copy main UI prompt to style.")72
73with gr.Row():74self.prompt = gr.Textbox(label="Prompt", show_label=True, elem_id=f"{tabname}_edit_style_prompt", lines=3, elem_classes=["prompt"])75
76with gr.Row():77self.neg_prompt = gr.Textbox(label="Negative prompt", show_label=True, elem_id=f"{tabname}_edit_style_neg_prompt", lines=3, elem_classes=["prompt"])78
79with gr.Row():80self.save = gr.Button('Save', variant='primary', elem_id=f'{tabname}_edit_style_save', visible=False)81self.delete = gr.Button('Delete', variant='primary', elem_id=f'{tabname}_edit_style_delete', visible=False)82self.close = gr.Button('Close', variant='secondary', elem_id=f'{tabname}_edit_style_close')83
84self.selection.change(85fn=select_style,86inputs=[self.selection],87outputs=[self.prompt, self.neg_prompt, self.delete, self.save],88show_progress=False,89)90
91self.save.click(92fn=save_style,93inputs=[self.selection, self.prompt, self.neg_prompt],94outputs=[self.delete],95show_progress=False,96).then(refresh_styles, outputs=[self.dropdown, self.selection], show_progress=False)97
98self.delete.click(99fn=delete_style,100_js='function(name){ if(name == "") return ""; return confirm("Delete style " + name + "?") ? name : ""; }',101inputs=[self.selection],102outputs=[self.selection, self.prompt, self.neg_prompt],103show_progress=False,104).then(refresh_styles, outputs=[self.dropdown, self.selection], show_progress=False)105
106self.setup_apply_button(self.materialize)107
108self.copy.click(109fn=lambda p, n: (p, n),110inputs=[main_ui_prompt, main_ui_negative_prompt],111outputs=[self.prompt, self.neg_prompt],112show_progress=False,113)114
115ui_common.setup_dialog(button_show=edit_button, dialog=styles_dialog, button_close=self.close)116
117def setup_apply_button(self, button):118button.click(119fn=materialize_styles,120inputs=[self.main_ui_prompt, self.main_ui_negative_prompt, self.dropdown],121outputs=[self.main_ui_prompt, self.main_ui_negative_prompt, self.dropdown],122show_progress=False,123).then(fn=None, _js="function(){update_"+self.tabname+"_tokens(); closePopup();}", show_progress=False)124