2023-01-20 22:36:07 -07:00
|
|
|
import os.path
|
|
|
|
|
|
|
|
from modules import shared
|
|
|
|
import gradio as gr
|
|
|
|
import json
|
2023-01-23 13:11:34 -07:00
|
|
|
import html
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
from modules.generation_parameters_copypaste import image_from_url_text
|
|
|
|
|
|
|
|
extra_pages = []
|
|
|
|
|
|
|
|
|
|
|
|
def register_page(page):
|
2023-01-21 06:15:53 -07:00
|
|
|
"""registers extra networks page for the UI; recommend doing it in on_before_ui() callback for extensions"""
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
extra_pages.append(page)
|
|
|
|
|
|
|
|
|
|
|
|
class ExtraNetworksPage:
|
|
|
|
def __init__(self, title):
|
|
|
|
self.title = title
|
2023-01-21 07:20:24 -07:00
|
|
|
self.name = title.lower()
|
2023-01-20 22:36:07 -07:00
|
|
|
self.card_page = shared.html("extra-networks-card.html")
|
|
|
|
self.allow_negative_prompt = False
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
pass
|
|
|
|
|
2023-01-22 18:01:24 -07:00
|
|
|
def create_html(self, tabname):
|
|
|
|
view = shared.opts.extra_networks_default_view
|
2023-01-20 22:36:07 -07:00
|
|
|
items_html = ''
|
|
|
|
|
|
|
|
for item in self.list_items():
|
|
|
|
items_html += self.create_html_for_item(item, tabname)
|
|
|
|
|
|
|
|
if items_html == '':
|
|
|
|
dirs = "".join([f"<li>{x}</li>" for x in self.allowed_directories_for_previews()])
|
|
|
|
items_html = shared.html("extra-networks-no-cards.html").format(dirs=dirs)
|
|
|
|
|
2023-01-21 07:20:24 -07:00
|
|
|
res = f"""
|
2023-01-21 13:45:36 -07:00
|
|
|
<div id='{tabname}_{self.name}_cards' class='extra-network-{view}'>
|
2023-01-21 07:20:24 -07:00
|
|
|
{items_html}
|
|
|
|
</div>
|
|
|
|
"""
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def list_items(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def allowed_directories_for_previews(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def create_html_for_item(self, item, tabname):
|
|
|
|
preview = item.get("preview", None)
|
|
|
|
|
|
|
|
args = {
|
2023-01-23 13:11:34 -07:00
|
|
|
"preview_html": "style='background-image: url(\"" + html.escape(preview) + "\")'" if preview else '',
|
2023-01-21 14:02:41 -07:00
|
|
|
"prompt": item["prompt"],
|
2023-01-20 22:36:07 -07:00
|
|
|
"tabname": json.dumps(tabname),
|
|
|
|
"local_preview": json.dumps(item["local_preview"]),
|
|
|
|
"name": item["name"],
|
2023-01-23 13:11:34 -07:00
|
|
|
"card_clicked": '"' + html.escape(f"""return cardClicked({json.dumps(tabname)}, {item["prompt"]}, {"true" if self.allow_negative_prompt else "false"})""") + '"',
|
|
|
|
"save_card_preview": '"' + html.escape(f"""return saveCardPreview(event, {json.dumps(tabname)}, {json.dumps(item["local_preview"])})""") + '"',
|
2023-01-20 22:36:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.card_page.format(**args)
|
|
|
|
|
|
|
|
|
|
|
|
def intialize():
|
|
|
|
extra_pages.clear()
|
|
|
|
|
|
|
|
|
|
|
|
class ExtraNetworksUi:
|
|
|
|
def __init__(self):
|
|
|
|
self.pages = None
|
|
|
|
self.stored_extra_pages = None
|
|
|
|
|
|
|
|
self.button_save_preview = None
|
|
|
|
self.preview_target_filename = None
|
|
|
|
|
|
|
|
self.tabname = None
|
|
|
|
|
|
|
|
|
2023-01-22 01:00:05 -07:00
|
|
|
def pages_in_preferred_order(pages):
|
|
|
|
tab_order = [x.lower().strip() for x in shared.opts.ui_extra_networks_tab_reorder.split(",")]
|
|
|
|
|
|
|
|
def tab_name_score(name):
|
|
|
|
name = name.lower()
|
|
|
|
for i, possible_match in enumerate(tab_order):
|
|
|
|
if possible_match in name:
|
|
|
|
return i
|
|
|
|
|
|
|
|
return len(pages)
|
|
|
|
|
|
|
|
tab_scores = {page.name: (tab_name_score(page.name), original_index) for original_index, page in enumerate(pages)}
|
|
|
|
|
|
|
|
return sorted(pages, key=lambda x: tab_scores[x.name])
|
|
|
|
|
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
def create_ui(container, button, tabname):
|
|
|
|
ui = ExtraNetworksUi()
|
|
|
|
ui.pages = []
|
2023-01-22 01:04:02 -07:00
|
|
|
ui.stored_extra_pages = pages_in_preferred_order(extra_pages.copy())
|
2023-01-20 22:36:07 -07:00
|
|
|
ui.tabname = tabname
|
|
|
|
|
|
|
|
with gr.Tabs(elem_id=tabname+"_extra_tabs") as tabs:
|
2023-01-22 01:04:02 -07:00
|
|
|
for page in ui.stored_extra_pages:
|
2023-01-20 22:36:07 -07:00
|
|
|
with gr.Tab(page.title):
|
|
|
|
page_elem = gr.HTML(page.create_html(ui.tabname))
|
|
|
|
ui.pages.append(page_elem)
|
|
|
|
|
2023-01-21 07:20:24 -07:00
|
|
|
filter = gr.Textbox('', show_label=False, elem_id=tabname+"_extra_search", placeholder="Search...", visible=False)
|
|
|
|
button_refresh = gr.Button('Refresh', elem_id=tabname+"_extra_refresh")
|
|
|
|
button_close = gr.Button('Close', elem_id=tabname+"_extra_close")
|
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
ui.button_save_preview = gr.Button('Save preview', elem_id=tabname+"_save_preview", visible=False)
|
|
|
|
ui.preview_target_filename = gr.Textbox('Preview save filename', elem_id=tabname+"_preview_filename", visible=False)
|
|
|
|
|
2023-01-25 09:55:31 -07:00
|
|
|
def toggle_visibility(is_visible):
|
|
|
|
is_visible = not is_visible
|
|
|
|
return is_visible, gr.update(visible=is_visible)
|
|
|
|
|
|
|
|
state_visible = gr.State(value=False)
|
|
|
|
button.click(fn=toggle_visibility, inputs=[state_visible], outputs=[state_visible, container])
|
|
|
|
button_close.click(fn=toggle_visibility, inputs=[state_visible], outputs=[state_visible, container])
|
2023-01-20 22:36:07 -07:00
|
|
|
|
2023-01-22 18:01:24 -07:00
|
|
|
def refresh():
|
2023-01-20 22:36:07 -07:00
|
|
|
res = []
|
|
|
|
|
|
|
|
for pg in ui.stored_extra_pages:
|
|
|
|
pg.refresh()
|
2023-01-22 18:01:24 -07:00
|
|
|
res.append(pg.create_html(ui.tabname))
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2023-01-22 18:01:24 -07:00
|
|
|
button_refresh.click(fn=refresh, inputs=[], outputs=ui.pages)
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
return ui
|
|
|
|
|
|
|
|
|
|
|
|
def path_is_parent(parent_path, child_path):
|
|
|
|
parent_path = os.path.abspath(parent_path)
|
|
|
|
child_path = os.path.abspath(child_path)
|
|
|
|
|
|
|
|
return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
|
|
|
|
|
|
|
|
|
|
|
|
def setup_ui(ui, gallery):
|
2023-01-22 18:01:24 -07:00
|
|
|
def save_preview(index, images, filename):
|
2023-01-20 22:36:07 -07:00
|
|
|
if len(images) == 0:
|
|
|
|
print("There is no image in gallery to save as a preview.")
|
|
|
|
return [page.create_html(ui.tabname) for page in ui.stored_extra_pages]
|
|
|
|
|
|
|
|
index = int(index)
|
|
|
|
index = 0 if index < 0 else index
|
|
|
|
index = len(images) - 1 if index >= len(images) else index
|
|
|
|
|
|
|
|
img_info = images[index if index >= 0 else 0]
|
|
|
|
image = image_from_url_text(img_info)
|
|
|
|
|
|
|
|
is_allowed = False
|
|
|
|
for extra_page in ui.stored_extra_pages:
|
|
|
|
if any([path_is_parent(x, filename) for x in extra_page.allowed_directories_for_previews()]):
|
|
|
|
is_allowed = True
|
|
|
|
break
|
|
|
|
|
|
|
|
assert is_allowed, f'writing to {filename} is not allowed'
|
|
|
|
|
|
|
|
image.save(filename)
|
|
|
|
|
2023-01-22 18:01:24 -07:00
|
|
|
return [page.create_html(ui.tabname) for page in ui.stored_extra_pages]
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
ui.button_save_preview.click(
|
|
|
|
fn=save_preview,
|
2023-01-22 18:01:24 -07:00
|
|
|
_js="function(x, y, z){console.log(x, y, z); return [selected_gallery_index(), y, z]}",
|
|
|
|
inputs=[ui.preview_target_filename, gallery, ui.preview_target_filename],
|
2023-01-20 22:36:07 -07:00
|
|
|
outputs=[*ui.pages]
|
|
|
|
)
|