ui_prompt_styles.py 5.7 KB

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