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

99 lines
2.8 KiB
Python
Raw Normal View History

2023-02-26 10:17:57 -05:00
from pathlib import Path
import gradio as gr
from modules.html_generator import get_image_cache
from modules.chat import load_character
from modules.shared import gradio, settings
def generate_css():
2023-02-26 10:17:57 -05:00
css = """
.character-gallery > .gallery {
margin: 1rem 0;
display: grid !important;
2023-02-27 09:30:52 -05: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;
}
.character-container {
cursor: pointer;
text-align: center;
position: relative;
2023-02-27 09:55:36 -05: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 11:49:55 -05:00
box-shadow: 3px 3px 6px 0px rgb(0 0 0 / 50%);
}
.character-name {
2023-02-27 11:49:55 -05:00
margin-top: 0.3rem;
display: block;
font-size: 1.2rem;
font-weight: 600;
2023-02-27 09:46:15 -05:00
overflow-wrap: anywhere;
}
2023-02-26 10:17:57 -05:00
"""
return css
2023-02-26 10:17:57 -05:00
def generate_html():
cards = []
2023-02-26 10:17:57 -05:00
# Iterate through files in image folder
2023-02-27 11:04:06 -05:00
for file in sorted(Path("characters").glob("*")):
2023-02-26 10:17:57 -05:00
if file.name.endswith(".json"):
character = file.name.replace(".json", "")
container_html = f'<div class="character-container">'
image_html = "<div class='placeholder'></div>"
for i in [
f"characters/{character}.png",
f"characters/{character}.jpg",
f"characters/{character}.jpeg",
]:
path = Path(i)
if path.exists():
try:
image_html = f'<img src="file/{get_image_cache(path)}">'
break
except:
continue
container_html += f'{image_html} <span class="character-name">{character}</span>'
container_html += "</div>"
cards.append([container_html, character])
return cards
def select_character(evt: gr.SelectData):
return (evt.value[1])
2023-02-26 10:17:57 -05:00
def ui():
with gr.Accordion("Character gallery", open=False):
2023-02-26 10:17:57 -05:00
update = gr.Button("Refresh")
gr.HTML(value="<style>"+generate_css()+"</style>")
gallery = gr.Dataset(components=[gr.HTML(visible=False)],
label="",
samples=generate_html(),
elem_classes=["character-gallery"],
samples_per_page=50
)
2023-02-26 10:17:57 -05:00
update.click(generate_html, [], gallery)
gallery.select(select_character, None, gradio['character_menu'])