Merge pull request #673 from ye7iaserag/patch-1

Implement character gallery using Dataset
This commit is contained in:
oobabooga 2023-03-31 02:04:52 -03:00 committed by GitHub
commit 3e1267af79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,19 +2,29 @@ from pathlib import Path
import gradio as gr import gradio as gr
from modules.chat import load_character
from modules.html_generator import get_image_cache from modules.html_generator import get_image_cache
from modules.shared import gradio, settings
def generate_html(): def generate_css():
css = """ css = """
.character-gallery { .character-gallery > .gallery {
margin: 1rem 0; margin: 1rem 0;
display: grid; display: grid !important;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
grid-column-gap: 0.4rem; grid-column-gap: 0.4rem;
grid-row-gap: 1.2rem; grid-row-gap: 1.2rem;
} }
.character-gallery > .label {
display: none !important;
}
.character-gallery button.gallery-item {
display: contents;
}
.character-container { .character-container {
cursor: pointer; cursor: pointer;
text-align: center; text-align: center;
@ -45,14 +55,16 @@ def generate_html():
overflow-wrap: anywhere; overflow-wrap: anywhere;
} }
""" """
return css
container_html = f'<style>{css}</style><div class="character-gallery">'
def generate_html():
cards = []
# Iterate through files in image folder # Iterate through files in image folder
for file in sorted(Path("characters").glob("*")): for file in sorted(Path("characters").glob("*")):
if file.name.endswith(".json"): if file.name.endswith(".json"):
character = file.name.replace(".json", "") character = file.name.replace(".json", "")
container_html += f'<div class="character-container" onclick=\'document.getElementById("character-menu").children[1].children[1].value = "{character}"; document.getElementById("character-menu").children[1].children[1].dispatchEvent(new Event("change"));\'>' container_html = f'<div class="character-container">'
image_html = "<div class='placeholder'></div>" image_html = "<div class='placeholder'></div>"
for i in [ for i in [
@ -71,12 +83,24 @@ def generate_html():
container_html += f'{image_html} <span class="character-name">{character}</span>' container_html += f'{image_html} <span class="character-name">{character}</span>'
container_html += "</div>" container_html += "</div>"
cards.append([container_html, character])
return cards
def select_character(evt: gr.SelectData):
return (evt.value[1])
container_html += "</div>"
return container_html
def ui(): def ui():
with gr.Accordion("Character gallery", open=False): with gr.Accordion("Character gallery", open=False):
update = gr.Button("Refresh") update = gr.Button("Refresh")
gallery = gr.HTML(value=generate_html()) 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
)
update.click(generate_html, [], gallery) update.click(generate_html, [], gallery)
gallery.select(select_character, None, gradio['character_menu'])