ui_prompt_styles.py 5.1 KB

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