script.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from pathlib import Path
  2. import gradio as gr
  3. from modules.html_generator import get_image_cache
  4. from modules.shared import gradio
  5. def generate_css():
  6. css = """
  7. .character-gallery > .gallery {
  8. margin: 1rem 0;
  9. display: grid !important;
  10. grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  11. grid-column-gap: 0.4rem;
  12. grid-row-gap: 1.2rem;
  13. }
  14. .character-gallery > .label {
  15. display: none !important;
  16. }
  17. .character-gallery button.gallery-item {
  18. display: contents;
  19. }
  20. .character-container {
  21. cursor: pointer;
  22. text-align: center;
  23. position: relative;
  24. opacity: 0.85;
  25. }
  26. .character-container:hover {
  27. opacity: 1;
  28. }
  29. .character-container .placeholder, .character-container img {
  30. width: 150px;
  31. height: 200px;
  32. background-color: gray;
  33. object-fit: cover;
  34. margin: 0 auto;
  35. border-radius: 1rem;
  36. border: 3px solid white;
  37. box-shadow: 3px 3px 6px 0px rgb(0 0 0 / 50%);
  38. }
  39. .character-name {
  40. margin-top: 0.3rem;
  41. display: block;
  42. font-size: 1.2rem;
  43. font-weight: 600;
  44. overflow-wrap: anywhere;
  45. }
  46. """
  47. return css
  48. def generate_html():
  49. cards = []
  50. # Iterate through files in image folder
  51. for file in sorted(Path("characters").glob("*")):
  52. if file.suffix in [".json", ".yml", ".yaml"]:
  53. character = file.stem
  54. container_html = '<div class="character-container">'
  55. image_html = "<div class='placeholder'></div>"
  56. for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]:
  57. if path.exists():
  58. image_html = f'<img src="file/{get_image_cache(path)}">'
  59. break
  60. container_html += f'{image_html} <span class="character-name">{character}</span>'
  61. container_html += "</div>"
  62. cards.append([container_html, character])
  63. return cards
  64. def select_character(evt: gr.SelectData):
  65. return (evt.value[1])
  66. def ui():
  67. with gr.Accordion("Character gallery", open=False):
  68. update = gr.Button("Refresh")
  69. gr.HTML(value="<style>"+generate_css()+"</style>")
  70. gallery = gr.Dataset(components=[gr.HTML(visible=False)],
  71. label="",
  72. samples=generate_html(),
  73. elem_classes=["character-gallery"],
  74. samples_per_page=50
  75. )
  76. update.click(generate_html, [], gallery)
  77. gallery.select(select_character, None, gradio['character_menu'])