ui_prompt_styles.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import gradio as gr
  2. from modules import shared, ui_common, ui_components, styles
  3. styles_edit_symbol = '\U0001f58c\uFE0F' # 🖌️
  4. styles_materialize_symbol = '\U0001f4cb' # 📋
  5. styles_copy_symbol = '\U0001f4dd' # 📝
  6. def select_style(name):
  7. style = shared.prompt_styles.styles.get(name)
  8. existing = style is not None
  9. empty = not name
  10. prompt = style.prompt if style else gr.update()
  11. negative_prompt = style.negative_prompt if style else gr.update()
  12. return prompt, negative_prompt, gr.update(visible=existing), gr.update(visible=not empty)
  13. def save_style(name, prompt, negative_prompt):
  14. if not name:
  15. return gr.update(visible=False)
  16. existing_style = shared.prompt_styles.styles.get(name)
  17. path = existing_style.path if existing_style is not None else None
  18. style = styles.PromptStyle(name, prompt, negative_prompt, path)
  19. shared.prompt_styles.styles[style.name] = style
  20. shared.prompt_styles.save_styles()
  21. return gr.update(visible=True)
  22. def delete_style(name):
  23. if name == "":
  24. return
  25. shared.prompt_styles.styles.pop(name, None)
  26. shared.prompt_styles.save_styles()
  27. return '', '', ''
  28. def materialize_styles(prompt, negative_prompt, styles):
  29. prompt = shared.prompt_styles.apply_styles_to_prompt(prompt, styles)
  30. negative_prompt = shared.prompt_styles.apply_negative_styles_to_prompt(negative_prompt, styles)
  31. return [gr.Textbox.update(value=prompt), gr.Textbox.update(value=negative_prompt), gr.Dropdown.update(value=[])]
  32. def refresh_styles():
  33. return gr.update(choices=list(shared.prompt_styles.styles)), gr.update(choices=list(shared.prompt_styles.styles))
  34. class UiPromptStyles:
  35. def __init__(self, tabname, main_ui_prompt, main_ui_negative_prompt):
  36. self.tabname = tabname
  37. self.main_ui_prompt = main_ui_prompt
  38. self.main_ui_negative_prompt = main_ui_negative_prompt
  39. with gr.Row(elem_id=f"{tabname}_styles_row"):
  40. self.dropdown = gr.Dropdown(label="Styles", show_label=False, elem_id=f"{tabname}_styles", choices=list(shared.prompt_styles.styles), value=[], multiselect=True, tooltip="Styles")
  41. edit_button = ui_components.ToolButton(value=styles_edit_symbol, elem_id=f"{tabname}_styles_edit_button", tooltip="Edit styles")
  42. with gr.Box(elem_id=f"{tabname}_styles_dialog", elem_classes="popup-dialog") as styles_dialog:
  43. with gr.Row():
  44. self.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.")
  45. ui_common.create_refresh_button([self.dropdown, self.selection], shared.prompt_styles.reload, lambda: {"choices": list(shared.prompt_styles.styles)}, f"refresh_{tabname}_styles")
  46. self.materialize = ui_components.ToolButton(value=styles_materialize_symbol, elem_id=f"{tabname}_style_apply_dialog", tooltip="Apply all selected styles from the style selection dropdown in main UI to the prompt.")
  47. self.copy = ui_components.ToolButton(value=styles_copy_symbol, elem_id=f"{tabname}_style_copy", tooltip="Copy main UI prompt to style.")
  48. with gr.Row():
  49. self.prompt = gr.Textbox(label="Prompt", show_label=True, elem_id=f"{tabname}_edit_style_prompt", lines=3, elem_classes=["prompt"])
  50. with gr.Row():
  51. self.neg_prompt = gr.Textbox(label="Negative prompt", show_label=True, elem_id=f"{tabname}_edit_style_neg_prompt", lines=3, elem_classes=["prompt"])
  52. with gr.Row():
  53. self.save = gr.Button('Save', variant='primary', elem_id=f'{tabname}_edit_style_save', visible=False)
  54. self.delete = gr.Button('Delete', variant='primary', elem_id=f'{tabname}_edit_style_delete', visible=False)
  55. self.close = gr.Button('Close', variant='secondary', elem_id=f'{tabname}_edit_style_close')
  56. self.selection.change(
  57. fn=select_style,
  58. inputs=[self.selection],
  59. outputs=[self.prompt, self.neg_prompt, self.delete, self.save],
  60. show_progress=False,
  61. )
  62. self.save.click(
  63. fn=save_style,
  64. inputs=[self.selection, self.prompt, self.neg_prompt],
  65. outputs=[self.delete],
  66. show_progress=False,
  67. ).then(refresh_styles, outputs=[self.dropdown, self.selection], show_progress=False)
  68. self.delete.click(
  69. fn=delete_style,
  70. _js='function(name){ if(name == "") return ""; return confirm("Delete style " + name + "?") ? name : ""; }',
  71. inputs=[self.selection],
  72. outputs=[self.selection, self.prompt, self.neg_prompt],
  73. show_progress=False,
  74. ).then(refresh_styles, outputs=[self.dropdown, self.selection], show_progress=False)
  75. self.setup_apply_button(self.materialize)
  76. self.copy.click(
  77. fn=lambda p, n: (p, n),
  78. inputs=[main_ui_prompt, main_ui_negative_prompt],
  79. outputs=[self.prompt, self.neg_prompt],
  80. show_progress=False,
  81. )
  82. ui_common.setup_dialog(button_show=edit_button, dialog=styles_dialog, button_close=self.close)
  83. def setup_apply_button(self, button):
  84. button.click(
  85. fn=materialize_styles,
  86. inputs=[self.main_ui_prompt, self.main_ui_negative_prompt, self.dropdown],
  87. outputs=[self.main_ui_prompt, self.main_ui_negative_prompt, self.dropdown],
  88. show_progress=False,
  89. ).then(fn=None, _js="function(){update_"+self.tabname+"_tokens(); closePopup();}", show_progress=False)