2023-02-26 10:17:57 -05:00
|
|
|
from pathlib import Path
|
2023-03-31 01:01:48 -04:00
|
|
|
|
2023-02-26 10:17:57 -05:00
|
|
|
import gradio as gr
|
2023-03-31 01:01:48 -04:00
|
|
|
|
|
|
|
from modules.html_generator import get_image_cache
|
2023-04-04 21:28:49 -04:00
|
|
|
from modules.shared import gradio
|
2023-02-26 10:37:26 -05:00
|
|
|
|
2023-03-31 01:01:48 -04:00
|
|
|
|
2023-03-30 22:56:49 -04:00
|
|
|
def generate_css():
|
2023-02-26 10:17:57 -05:00
|
|
|
css = """
|
2023-03-30 22:56:49 -04:00
|
|
|
.character-gallery > .gallery {
|
2023-02-27 08:52:43 -05:00
|
|
|
margin: 1rem 0;
|
2023-03-30 22:56:49 -04:00
|
|
|
display: grid !important;
|
2023-02-27 09:30:52 -05:00
|
|
|
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
2023-02-27 08:52:43 -05:00
|
|
|
grid-column-gap: 0.4rem;
|
|
|
|
grid-row-gap: 1.2rem;
|
|
|
|
}
|
|
|
|
|
2023-03-30 22:56:49 -04:00
|
|
|
.character-gallery > .label {
|
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
|
2023-03-31 00:54:24 -04:00
|
|
|
.character-gallery button.gallery-item {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
|
2023-02-27 08:52:43 -05:00
|
|
|
.character-container {
|
|
|
|
cursor: pointer;
|
|
|
|
text-align: center;
|
|
|
|
position: relative;
|
2023-02-27 09:55:36 -05:00
|
|
|
opacity: 0.85;
|
2023-02-27 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
.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%);
|
2023-02-27 08:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
.character-name {
|
2023-02-27 11:49:55 -05:00
|
|
|
margin-top: 0.3rem;
|
2023-02-27 08:52:43 -05:00
|
|
|
display: block;
|
|
|
|
font-size: 1.2rem;
|
|
|
|
font-weight: 600;
|
2023-02-27 09:46:15 -05:00
|
|
|
overflow-wrap: anywhere;
|
2023-02-27 08:52:43 -05:00
|
|
|
}
|
2023-02-26 10:17:57 -05:00
|
|
|
"""
|
2023-03-30 22:56:49 -04:00
|
|
|
return css
|
2023-02-26 10:17:57 -05:00
|
|
|
|
|
|
|
|
2023-03-30 22:56:49 -04: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-04-02 19:34:25 -04:00
|
|
|
if file.suffix in [".json", ".yml", ".yaml"]:
|
|
|
|
character = file.stem
|
2023-04-04 21:28:49 -04:00
|
|
|
container_html = '<div class="character-container">'
|
2023-02-26 10:37:26 -05:00
|
|
|
image_html = "<div class='placeholder'></div>"
|
|
|
|
|
2023-04-04 23:34:17 -04:00
|
|
|
for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]:
|
2023-02-26 10:37:26 -05:00
|
|
|
if path.exists():
|
2023-04-04 21:52:15 -04:00
|
|
|
image_html = f'<img src="file/{get_image_cache(path)}">'
|
|
|
|
break
|
2023-02-26 10:37:26 -05:00
|
|
|
|
2023-02-27 08:52:43 -05:00
|
|
|
container_html += f'{image_html} <span class="character-name">{character}</span>'
|
|
|
|
container_html += "</div>"
|
2023-03-30 22:56:49 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
2023-08-13 00:12:15 -04:00
|
|
|
def custom_js():
|
|
|
|
path_to_js = Path(__file__).parent.resolve() / 'script.js'
|
|
|
|
return open(path_to_js, 'r').read()
|
|
|
|
|
|
|
|
|
2023-02-26 10:17:57 -05:00
|
|
|
def ui():
|
2023-08-13 00:12:15 -04:00
|
|
|
with gr.Accordion("Character gallery", open=False, elem_id='gallery-extension'):
|
2023-02-26 10:17:57 -05:00
|
|
|
update = gr.Button("Refresh")
|
2023-04-06 23:15:45 -04:00
|
|
|
gr.HTML(value="<style>" + generate_css() + "</style>")
|
2023-03-30 22:56:49 -04:00
|
|
|
gallery = gr.Dataset(components=[gr.HTML(visible=False)],
|
2023-04-06 23:52:02 -04:00
|
|
|
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)
|
2023-04-06 23:15:45 -04:00
|
|
|
gallery.select(select_character, None, gradio['character_menu'])
|