text-generation-webui/extensions/gallery/script.py

130 lines
3.7 KiB
Python
Raw Normal View History

2023-02-26 16:17:57 +01:00
from pathlib import Path
2023-03-31 07:01:48 +02:00
2023-02-26 16:17:57 +01:00
import gradio as gr
2023-03-31 07:01:48 +02:00
from modules.html_generator import get_image_cache
2023-12-04 02:45:50 +01:00
from modules.shared import gradio, settings
2023-12-04 16:25:18 +01:00
2023-12-04 02:45:50 +01:00
cards = []
2023-03-31 07:01:48 +02:00
def generate_css():
2023-02-26 16:17:57 +01:00
css = """
2023-12-04 02:45:50 +01:00
.highlighted-border {
border-color: rgb(249, 115, 22) !important;
}
.character-gallery > .gallery {
margin: 1rem 0;
display: grid !important;
2023-02-27 15:30:52 +01:00
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
grid-column-gap: 0.4rem;
grid-row-gap: 1.2rem;
}
.character-gallery > .label {
display: none !important;
}
2023-03-31 06:54:24 +02:00
.character-gallery button.gallery-item {
display: contents;
}
.character-container {
cursor: pointer;
text-align: center;
position: relative;
2023-02-27 15:55:36 +01:00
opacity: 0.85;
}
.character-container:hover {
opacity: 1;
}
.character-container .placeholder, .character-container img {
width: 150px;
height: 200px;
background-color: gray;
object-fit: cover;
margin: 0 auto;
border-radius: 1rem;
border: 3px solid white;
2023-02-27 17:49:55 +01:00
box-shadow: 3px 3px 6px 0px rgb(0 0 0 / 50%);
}
.character-name {
2023-02-27 17:49:55 +01:00
margin-top: 0.3rem;
display: block;
font-size: 1.2rem;
font-weight: 600;
2023-02-27 15:46:15 +01:00
overflow-wrap: anywhere;
}
2023-02-26 16:17:57 +01:00
"""
return css
2023-02-26 16:17:57 +01:00
def generate_html():
2023-12-04 02:45:50 +01:00
global cards
cards = []
2023-02-26 16:17:57 +01:00
# Iterate through files in image folder
2023-02-27 17:04:06 +01:00
for file in sorted(Path("characters").glob("*")):
if file.suffix in [".json", ".yml", ".yaml"]:
character = file.stem
container_html = '<div class="character-container">'
image_html = "<div class='placeholder'></div>"
2023-04-05 05:34:17 +02:00
for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]:
if path.exists():
2023-04-05 03:52:15 +02:00
image_html = f'<img src="file/{get_image_cache(path)}">'
break
container_html += f'{image_html} <span class="character-name">{character}</span>'
container_html += "</div>"
cards.append([container_html, character])
return cards
2023-12-04 02:45:50 +01:00
def filter_cards(filter_str=''):
if filter_str == '':
return cards
filter_upper = filter_str.upper()
return [k for k in cards if filter_upper in k[1].upper()]
def select_character(evt: gr.SelectData):
return (evt.value[1])
2023-02-26 16:17:57 +01:00
2023-08-13 06:12:15 +02:00
def custom_js():
path_to_js = Path(__file__).parent.resolve() / 'script.js'
return open(path_to_js, 'r').read()
2023-02-26 16:17:57 +01:00
def ui():
2023-12-04 02:45:50 +01:00
with gr.Accordion("Character gallery", open=settings["gallery-open"], elem_id='gallery-extension'):
gr.HTML(value="<style>" + generate_css() + "</style>")
2023-12-04 02:45:50 +01:00
with gr.Row():
filter_box = gr.Textbox(label='', placeholder='Filter', lines=1, max_lines=1, container=False, elem_id='gallery-filter-box')
gr.ClearButton(filter_box, value='🗑️', elem_classes='refresh-button')
update = gr.Button("Refresh", elem_classes='refresh-button')
2023-12-04 16:25:18 +01:00
2023-11-19 17:31:01 +01:00
gallery = gr.Dataset(
components=[gr.HTML(visible=False)],
label="",
samples=generate_html(),
elem_classes=["character-gallery"],
2023-12-04 02:45:50 +01:00
samples_per_page=settings["gallery-items_per_page"]
2023-11-19 17:31:01 +01:00
)
2023-12-04 02:45:50 +01:00
filter_box.change(lambda: None, None, None, _js=f'() => {{{custom_js()}; gotoFirstPage()}}').success(
filter_cards, filter_box, gallery).then(
lambda x: gr.update(elem_classes='highlighted-border' if x != '' else ''), filter_box, filter_box, show_progress=False)
update.click(generate_html, [], None).success(
filter_cards, filter_box, gallery)
gallery.select(select_character, None, gradio['character_menu'])