shared_gradio_themes.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import os
  2. import gradio as gr
  3. from modules import errors, shared
  4. from modules.paths_internal import script_path
  5. # https://huggingface.co/datasets/freddyaboulton/gradio-theme-subdomains/resolve/main/subdomains.json
  6. gradio_hf_hub_themes = [
  7. "gradio/base",
  8. "gradio/glass",
  9. "gradio/monochrome",
  10. "gradio/seafoam",
  11. "gradio/soft",
  12. "gradio/dracula_test",
  13. "abidlabs/dracula_test",
  14. "abidlabs/Lime",
  15. "abidlabs/pakistan",
  16. "Ama434/neutral-barlow",
  17. "dawood/microsoft_windows",
  18. "finlaymacklon/smooth_slate",
  19. "Franklisi/darkmode",
  20. "freddyaboulton/dracula_revamped",
  21. "freddyaboulton/test-blue",
  22. "gstaff/xkcd",
  23. "Insuz/Mocha",
  24. "Insuz/SimpleIndigo",
  25. "JohnSmith9982/small_and_pretty",
  26. "nota-ai/theme",
  27. "nuttea/Softblue",
  28. "ParityError/Anime",
  29. "reilnuud/polite",
  30. "remilia/Ghostly",
  31. "rottenlittlecreature/Moon_Goblin",
  32. "step-3-profit/Midnight-Deep",
  33. "Taithrah/Minimal",
  34. "ysharma/huggingface",
  35. "ysharma/steampunk",
  36. "NoCrypt/miku"
  37. ]
  38. def reload_gradio_theme(theme_name=None):
  39. if not theme_name:
  40. theme_name = shared.opts.gradio_theme
  41. default_theme_args = dict(
  42. font=["Source Sans Pro", 'ui-sans-serif', 'system-ui', 'sans-serif'],
  43. font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'],
  44. )
  45. if theme_name == "Default":
  46. shared.gradio_theme = gr.themes.Default(**default_theme_args)
  47. else:
  48. try:
  49. theme_cache_dir = os.path.join(script_path, 'tmp', 'gradio_themes')
  50. theme_cache_path = os.path.join(theme_cache_dir, f'{theme_name.replace("/", "_")}.json')
  51. if shared.opts.gradio_themes_cache and os.path.exists(theme_cache_path):
  52. shared.gradio_theme = gr.themes.ThemeClass.load(theme_cache_path)
  53. else:
  54. os.makedirs(theme_cache_dir, exist_ok=True)
  55. shared.gradio_theme = gr.themes.ThemeClass.from_hub(theme_name)
  56. shared.gradio_theme.dump(theme_cache_path)
  57. except Exception as e:
  58. errors.display(e, "changing gradio theme")
  59. shared.gradio_theme = gr.themes.Default(**default_theme_args)
  60. # append additional values gradio_theme
  61. shared.gradio_theme.sd_webui_modal_lightbox_toolbar_opacity = shared.opts.sd_webui_modal_lightbox_toolbar_opacity
  62. shared.gradio_theme.sd_webui_modal_lightbox_icon_opacity = shared.opts.sd_webui_modal_lightbox_icon_opacity
  63. def resolve_var(name: str, gradio_theme=None, history=None):
  64. """
  65. Attempt to resolve a theme variable name to its value
  66. Parameters:
  67. name (str): The name of the theme variable
  68. ie "background_fill_primary", "background_fill_primary_dark"
  69. spaces and asterisk (*) prefix is removed from name before lookup
  70. gradio_theme (gradio.themes.ThemeClass): The theme object to resolve the variable from
  71. blank to use the webui default shared.gradio_theme
  72. history (list): A list of previously resolved variables to prevent circular references
  73. for regular use leave blank
  74. Returns:
  75. str: The resolved value
  76. Error handling:
  77. return either #000000 or #ffffff depending on initial name ending with "_dark"
  78. """
  79. try:
  80. if history is None:
  81. history = []
  82. if gradio_theme is None:
  83. gradio_theme = shared.gradio_theme
  84. name = name.strip()
  85. name = name[1:] if name.startswith("*") else name
  86. if name in history:
  87. raise ValueError(f'Circular references: name "{name}" in {history}')
  88. if value := getattr(gradio_theme, name, None):
  89. return resolve_var(value, gradio_theme, history + [name])
  90. else:
  91. return name
  92. except Exception:
  93. name = history[0] if history else name
  94. errors.report(f'resolve_color({name})', exc_info=True)
  95. return '#000000' if name.endswith("_dark") else '#ffffff'