lora_prompt_tool.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import modules.scripts as scripts
  2. import gradio as gr
  3. import modules
  4. from modules import script_callbacks
  5. from modules import shared
  6. from modules.ui_components import ToolButton
  7. from modules.ui import create_refresh_button
  8. from scripts.loraprompt_lib import libdata
  9. from scripts.loraprompt_lib import model
  10. from scripts.loraprompt_lib import ajax_action
  11. from scripts.loraprompt_lib import dataframe_edit
  12. from scripts.loraprompt_lib import localization
  13. from scripts.loraprompt_lib import util
  14. from scripts.loraprompt_lib import setting
  15. model.get_custom_model_folder()
  16. setting.load_setting()
  17. util.set_debug_logging_state(ajax_action.flag_to_boolean(setting.get_setting("debug")))
  18. def on_ui_tabs():
  19. local_code = getattr(shared.opts, "localization", "en")
  20. if getattr(shared.opts, "bilingual_localization_enabled", False) and (local_code == "en" or str(local_code).lower() == "none"):
  21. local_code = getattr(shared.opts, "bilingual_localization_file", local_code)
  22. localization.load_localization(local_code)
  23. with gr.Blocks(analytics_enabled=False) as lora_prompt_helper:
  24. txt2img_prompt = modules.ui.txt2img_paste_fields[0][0]
  25. txt2img_neg_prompt = modules.ui.txt2img_paste_fields[1][0]
  26. img2img_prompt = modules.ui.img2img_paste_fields[0][0]
  27. img2img_neg_prompt = modules.ui.img2img_paste_fields[1][0]
  28. with gr.Tab(localization.get_localize('Edit Model Trigger Words')):
  29. with gr.Box(elem_classes="lorahelp_box"):
  30. #模型基礎資料區
  31. with gr.Column():
  32. gr.Markdown(f"### {localization.get_localize('Edit Model Basic Data')}")
  33. with gr.Row():
  34. with gr.Column():
  35. js_model_type = gr.Textbox(label=localization.get_localize("Model type"), interactive=False)
  36. js_subtype = gr.Textbox(label=localization.get_localize("Type"), interactive=True, placeholder="EX: LoCon")
  37. js_model_name = gr.Textbox(label=localization.get_localize("Name"), interactive=True)
  38. js_model_path = gr.Textbox(label=localization.get_localize("Model Path"), interactive=False)
  39. gr.HTML(f"<div id=\"lorahelp_js_image_area\">{localization.get_localize('You DID NOT load any model!')}</div>")
  40. with gr.Row():
  41. js_suggested_weight = gr.Textbox(label=localization.get_localize("Suggested weight"), interactive=True, placeholder="EX: 1.0")
  42. js_model_params = gr.Textbox(label=localization.get_localize("Model params"), interactive=True, placeholder="EX: 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
  43. with gr.Box(elem_classes="lorahelp_box"):
  44. #提詞編輯區
  45. with gr.Column():
  46. gr.Markdown(f"### {localization.get_localize('Edit Model Trigger Words')}")
  47. with gr.Tab(localization.get_localize('Easy editing')):
  48. with gr.Column(elem_id="lorahelp_simpleedit_group_main"):
  49. simpleedit_main_name = gr.Textbox(label=localization.get_localize("name"), interactive=True, placeholder=localization.get_localize("EX: draw a Mahiro"))
  50. simpleedit_main_triggerword = gr.Textbox(label=localization.get_localize("Trigger Word"), interactive=True, placeholder="EX: mahiro_\\(onimai\\)")
  51. with gr.Column(elem_id="lorahelp_simpleedit_supergroup_other"):
  52. with gr.Column(elem_id="lorahelp_simpleedit_group_extra"):
  53. simpleedit_extra_enable = gr.Checkbox(value=False,label=localization.get_localize("Additional description"), interactive=True, elem_id="lorahelp_simpleedit_group_extra_enabled")
  54. with gr.Column(elem_id="lorahelp_simpleedit_group_extra_body"):
  55. simpleedit_extra_name = gr.Textbox(label=localization.get_localize("Additional description name"), interactive=True, placeholder=localization.get_localize("EX: Characteristics of Mahiro"))
  56. simpleedit_extra_triggerword = gr.Textbox(label=localization.get_localize("Description prompt"), interactive=True, placeholder="EX: long_hair, brown_eyes")
  57. with gr.Column(elem_id="lorahelp_simpleedit_group_neg"):
  58. simpleedit_neg_enable = gr.Checkbox(value=False,label=localization.get_localize("Dedicated negative prompt"), interactive=True, elem_id="lorahelp_simpleedit_group_neg_enabled")
  59. with gr.Column(elem_id="lorahelp_simpleedit_group_neg_body"):
  60. simpleedit_neg_name = gr.Textbox(label=localization.get_localize("Dedicated negative prompt name"), interactive=True, placeholder=localization.get_localize("EX: negative prompt for Mahiro"))
  61. simpleedit_neg_triggerword = gr.Textbox(label=localization.get_localize("Negative prompt"), interactive=True, placeholder="EX: ugly, bad")
  62. simpleedit_apply = gr.Button(value=localization.get_localize("Apply data"))
  63. simpleedit_parms=[simpleedit_main_name, simpleedit_main_triggerword, simpleedit_extra_enable, simpleedit_extra_name, simpleedit_extra_triggerword, simpleedit_neg_enable, simpleedit_neg_name, simpleedit_neg_triggerword]
  64. with gr.Tab(localization.get_localize('Advanced editing'),elem_id="js_tab_adv_edit"):
  65. js_dataedit_select_index = gr.Textbox(label="Select Index", visible=False, lines=1, value="", elem_id="lorahelp_dataedit_select_index_txtbox")
  66. js_copy_paste_box = gr.Textbox(label="Copy paste", visible=False, lines=1, value="", elem_id="lorahelp_copy_paste_txtbox")
  67. dataedit_add_event = gr.Button(value=libdata.add_symbol, visible=False, elem_id="lorahelp_dataedit_add_event")
  68. dataedit_delete_event = gr.Button(value=libdata.delete_symbol, visible=False, elem_id="lorahelp_dataedit_delete_event")
  69. dataedit_up_event = gr.Button(value=libdata.up_symbol, visible=False, elem_id="lorahelp_dataedit_up_event")
  70. dataedit_down_event = gr.Button(value=libdata.down_symbol, visible=False, elem_id="lorahelp_dataedit_down_event")
  71. dataedit_paste_event = gr.Button(value=libdata.paste_symbol, visible=False, elem_id="lorahelp_dataedit_paste_event")
  72. dataedit_paste_append_event = gr.Button(value=libdata.paste_append_symbol, visible=False, elem_id="lorahelp_dataedit_paste_append_event")
  73. #編輯工具列
  74. with gr.Row():
  75. ToolButton(value=libdata.add_symbol, elem_id="lorahelp_dataedit_add_btn")
  76. ToolButton(value=libdata.delete_symbol, elem_id="lorahelp_dataedit_delete_btn")
  77. ToolButton(value=libdata.up_symbol, elem_id="lorahelp_dataedit_up_btn")
  78. ToolButton(value=libdata.down_symbol, elem_id="lorahelp_dataedit_down_btn")
  79. ToolButton(value=libdata.copy_symbol, elem_id="lorahelp_dataedit_copy_btn")
  80. ToolButton(value=libdata.paste_symbol, elem_id="lorahelp_dataedit_paste_btn")
  81. ToolButton(value=libdata.paste_append_symbol, elem_id="lorahelp_dataedit_paste_append_btn")
  82. ToolButton(value="", elem_id="lorahelp_oyama_mahiro")
  83. dataedit_refresh_event = ToolButton(value=libdata.refresh_symbol, elem_id="lorahelp_dataedit_refresh_event_btn")
  84. gr.Button(value=localization.get_localize("Translate prompt words into:"), elem_id="lorahelp_dataedit_translate_btn")
  85. gr.HTML(f"<div id=\"lorahelp_translate_area\"></div>")
  86. #編輯區: 資料表
  87. js_dataedit = gr.Dataframe(headers=[
  88. localization.get_localize("name"),
  89. localization.get_localize("Enter your prompt word (trigger word/prompt/negative prompt)"),
  90. localization.get_localize("Categorys of prompt"),
  91. localization.get_localize("Negative prompt: please enter Y if this prompt is a negative prompt.")
  92. ], datatype=["str", "str", "str", "str"], col_count=(4, "fixed"), elem_id="lorahelp_js_trigger_words_dataframe", interactive=True)
  93. #操作區
  94. with gr.Row():
  95. js_remove_duplicate_prompt_btn = gr.Button(value=localization.get_localize("Remove duplicate prompts"))
  96. js_remove_empty_prompt_btn = gr.Button(value=localization.get_localize("Remove empty prompts"))
  97. with gr.Column():
  98. with gr.Row():
  99. js_dataframe_filter = gr.Textbox(label=localization.get_localize("Search..."), interactive=True, elem_id="lorahelp_js_dataframe_filter")
  100. gr.Checkbox(value=False,label=localization.get_localize("Sorting"), interactive=True, elem_id="lorahelp_sorting_group_enabled")
  101. with gr.Column(elem_id="lorahelp_sorting_group"):
  102. #排序功能
  103. with gr.Row():
  104. js_sort_order = gr.Radio(
  105. choices=[e.value for e in libdata.SortOrder], interactive=True,
  106. label=localization.get_localize('Sort Order'),
  107. elem_id="lorahelp_js_sort_order_radio"
  108. )
  109. js_sort_by_title_btn = gr.Button(value=localization.get_localize("Sort by title"))
  110. js_sort_by_prompt_btn = gr.Button(value=localization.get_localize("Sort by prompt"))
  111. with gr.Tab(localization.get_localize('JSON')):
  112. with gr.Column():
  113. with gr.Row():
  114. json_refresh_event = ToolButton(value=libdata.refresh_symbol, elem_id="lorahelp_json_refresh_event_btn")
  115. js_json_preview = gr.JSON()
  116. #輸出訊息框
  117. with gr.Box(elem_classes="lorahelp_box"):
  118. with gr.Column():
  119. js_message_report = gr.Textbox(label=localization.get_localize("Message"), interactive=False, elem_id="lorahelp_js_output_message")
  120. js_save_model_setting_btn = gr.Button(value=localization.get_localize("Save"),
  121. elem_id="lorahelp_js_save_model_setting_btn", variant="primary"
  122. )
  123. #導入功能區
  124. with gr.Box(elem_classes="lorahelp_box"):
  125. with gr.Column():
  126. gr.Markdown(f"### {localization.get_localize('Batch import prompts')}")
  127. with gr.Row():
  128. with gr.Column():
  129. js_load_textbox_prompt_btn = gr.Button(value=localization.get_localize("Read prompts from text boxes"), elem_id="lorahelp_js_load_textbox_prompt_btn")
  130. js_load_civitai_setting_btn = gr.Button(value=localization.get_localize("Download configuration files from CivitAI"),
  131. elem_id="lorahelp_js_load_civitai_setting_btn")
  132. with gr.Row():
  133. js_db_model_name = gr.Dropdown(
  134. label="Model", choices=sorted(model.get_db_models()), interactive=True
  135. )
  136. create_refresh_button(
  137. js_db_model_name,
  138. model.get_db_models,
  139. lambda: {"choices": sorted(model.get_db_models())},
  140. "lorahelp_refresh_db_models",
  141. )
  142. js_load_dreambooth_setting_btn = gr.Button(value=localization.get_localize("Load trigger words from Dreambooth model"),
  143. elem_id="lorahelp_js_load_dreambooth_setting_btn")
  144. text_import_txtbox = gr.Textbox(label=localization.get_localize("Enter prompts (one line for one trigger words)"), lines=10, value="",
  145. elem_id="lorahelp_text_import_txtbox")
  146. json_ajax_txtbox = gr.Textbox(label="Model JSON", visible=False, lines=1, value="", elem_id="lorahelp_model_json_txtbox")
  147. with gr.Tab(localization.get_localize('Settings')):
  148. with gr.Box(elem_classes="lorahelp_box"):
  149. with gr.Column():
  150. gr.Markdown(f"### {localization.get_localize_message('Settings')}")
  151. with gr.Row():
  152. js_debug_logging = gr.Checkbox(label=localization.get_localize("Show debug message"), value=ajax_action.flag_to_boolean(setting.get_setting("debug")), elem_id="lorahelp_js_debug_logging")
  153. js_touch_mode = gr.Checkbox(label=localization.get_localize("Force touch mode"), value=ajax_action.flag_to_boolean(setting.get_setting("touch_mode")), elem_id="lorahelp_js_touch_mode")
  154. js_save_ext_setting_btn = gr.Button(value=localization.get_localize("Save Setting"),
  155. elem_id="lorahelp_js_save_ext_setting_btn", variant="primary"
  156. )
  157. js_save_ext_setting_btn.click(setting.save_setting)
  158. try:
  159. js_dataedit.select(dataframe_edit.get_select_index, outputs=[js_dataedit_select_index])
  160. except:
  161. gr.Textbox(label="select not support", lines=1, visible=False, value="", elem_id="lorahelp_select_not_support")
  162. from scripts.loraprompt_lib import extension_data
  163. gr.Textbox(label="extension name", lines=1, visible=False, value=extension_data.extension_name, elem_id="lorahelp_extension_name")
  164. js_debug_logging.change(util.set_debug_logging_state, inputs=[js_debug_logging])
  165. js_touch_mode.change(setting.set_touch_mode, inputs=[js_touch_mode])
  166. model_data_ui_input = [js_subtype, js_model_name, js_model_path, js_suggested_weight, js_model_params]
  167. #工具列事件
  168. dataedit_add_event.click(dataframe_edit.add_row, inputs=[js_dataedit_select_index, js_dataedit], outputs=[js_dataedit])
  169. dataedit_delete_event.click(dataframe_edit.delete_row, inputs=[js_dataedit_select_index, js_dataedit], outputs=[js_dataedit])
  170. dataedit_up_event.click(dataframe_edit.up_row, inputs=[js_dataedit_select_index, js_dataedit], outputs=[js_dataedit])
  171. dataedit_down_event.click(dataframe_edit.down_row, inputs=[js_dataedit_select_index, js_dataedit], outputs=[js_dataedit])
  172. dataedit_paste_event.click(dataframe_edit.paste_cell, inputs=[js_dataedit_select_index, js_copy_paste_box, js_dataedit], outputs=[js_dataedit])
  173. dataedit_paste_append_event.click(dataframe_edit.paste_merge_cell, inputs=[js_dataedit_select_index, js_copy_paste_box, js_dataedit], outputs=[js_dataedit])
  174. simpleedit_apply.click(dataframe_edit.save_to_dataframe, inputs=[js_dataedit, *simpleedit_parms], outputs=[js_dataedit])
  175. dataedit_refresh_event.click(ajax_action.reload_trigger_words, inputs=[js_model_type, js_model_path],
  176. outputs=[js_model_type, *model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox, js_json_preview])
  177. json_refresh_event.click(ajax_action.update_trigger_words_json, inputs=[*model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox],
  178. outputs=[js_json_preview])
  179. js_save_model_setting_btn.click(ajax_action.save_trigger_words,
  180. inputs=[js_model_type, *model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox],
  181. outputs=[js_message_report]
  182. )
  183. js_load_civitai_setting_btn.click(ajax_action.get_setting_from_Civitai,
  184. inputs=[js_model_type, *model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox],
  185. outputs=[js_message_report, js_model_type, *model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox, js_json_preview]
  186. )
  187. js_load_dreambooth_setting_btn.click(ajax_action.get_setting_from_dreambooth,
  188. inputs=[js_db_model_name, js_dataedit],
  189. outputs=[js_message_report, js_dataedit]
  190. )
  191. js_load_textbox_prompt_btn.click(dataframe_edit.load_prompt_from_textbox,
  192. inputs=[text_import_txtbox, js_dataedit],
  193. outputs=[js_dataedit]
  194. )
  195. js_remove_duplicate_prompt_btn.click(dataframe_edit.remove_duplicate_prompt,
  196. inputs=[js_dataedit],
  197. outputs=[js_dataedit]
  198. )
  199. js_remove_empty_prompt_btn.click(dataframe_edit.remove_empty_prompt,
  200. inputs=[js_dataedit],
  201. outputs=[js_dataedit]
  202. )
  203. js_sort_by_title_btn.click(dataframe_edit.sort_by_title,
  204. inputs=[js_sort_order, js_dataedit],
  205. outputs=[js_dataedit]
  206. )
  207. js_sort_by_prompt_btn.click(dataframe_edit.sort_by_prompt,
  208. inputs=[js_sort_order, js_dataedit],
  209. outputs=[js_dataedit]
  210. )
  211. #, visible=False,
  212. js_ajax_txtbox = gr.Textbox(label="Request Msg From Js", visible=False, lines=1, value="", elem_id="lorahelp_js_ajax_txtbox")
  213. py_ajax_txtbox = gr.Textbox(label="Response Msg From Python", visible=False, lines=1, value="", elem_id="lorahelp_py_ajax_txtbox")
  214. js_cors_request_btn = gr.Button(value="CORS request", visible=False, elem_id="lorahelp_js_cors_request_btn")
  215. js_cors_request_btn.click(ajax_action.cors_request, inputs=[js_ajax_txtbox], outputs=[js_ajax_txtbox])
  216. js_update_trigger_words_btn = gr.Button(value="Update Trigger Words", visible=False, elem_id="lorahelp_js_update_trigger_words_btn")
  217. js_update_trigger_words_btn.click(ajax_action.update_trigger_words,
  218. inputs=[js_ajax_txtbox],
  219. outputs=[js_model_type, *model_data_ui_input, js_dataedit, *simpleedit_parms, json_ajax_txtbox, js_json_preview]
  220. )
  221. js_show_trigger_words_btn = gr.Button(value="Show Trigger Words", visible=False, elem_id="lorahelp_js_show_trigger_words_btn")
  222. js_show_trigger_words_btn.click(ajax_action.show_trigger_words, inputs=[js_ajax_txtbox], outputs=[js_ajax_txtbox])
  223. js_add_selected_trigger_word_btn = gr.Button(value="Add Selected Trigger Words", visible=False, elem_id="lorahelp_js_add_selected_trigger_word_btn")
  224. js_add_selected_trigger_word_btn.click(ajax_action.add_selected_trigger_word, inputs=[js_ajax_txtbox], outputs=[txt2img_prompt, img2img_prompt])
  225. js_add_selected_neg_trigger_word_btn = gr.Button(value="Add Selected Neg Trigger Words", visible=False, elem_id="lorahelp_js_add_selected_neg_trigger_word_btn")
  226. js_add_selected_neg_trigger_word_btn.click(ajax_action.add_selected_trigger_word, inputs=[js_ajax_txtbox], outputs=[txt2img_neg_prompt, img2img_neg_prompt])
  227. from scripts.loraprompt_lib import extension_data
  228. # the third parameter is the element id on html, with a "tab_" as prefix
  229. return (lora_prompt_helper , extension_data.extension_name_display, extension_data.extension_id),
  230. script_callbacks.on_ui_tabs(on_ui_tabs)