interface_extensions.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import json
  2. import importlib
  3. import importlib.util
  4. from importlib.metadata import version
  5. import time
  6. import gradio as gr
  7. from tts_webui.utils.pip_install import pip_install_wrapper, pip_uninstall_wrapper
  8. from tts_webui.utils.generic_error_tab_advanced import generic_error_tab_advanced
  9. def uninstall_extension(package_name):
  10. yield from pip_uninstall_wrapper(package_name, package_name)()
  11. def check_if_package_installed(package_name):
  12. spec = importlib.util.find_spec(package_name)
  13. return spec is not None
  14. # disabled_extensions = ["extension_xtts_rvc_ui"]
  15. disabled_extensions = []
  16. def _handle_package(package_name, title_name, requirements):
  17. if package_name in disabled_extensions:
  18. print(f"Skipping disabled {title_name} Extension...")
  19. return
  20. if check_if_package_installed(package_name):
  21. print(f"Loading {title_name} Extension...")
  22. start_time = time.time()
  23. try:
  24. module = importlib.import_module(f"{package_name}.main")
  25. if "builtin" in package_name:
  26. package_version = "0.0.1"
  27. else:
  28. package_version = version(package_name)
  29. main_tab = getattr(module, "extension__tts_generation_webui")
  30. with gr.Tab(f"{title_name} (v{package_version}) Extension"):
  31. if "builtin" in package_name:
  32. gr.Markdown(f"{title_name} Extension is up to date")
  33. else:
  34. if hasattr(module, "update_button"):
  35. update_button = getattr(module, "update_button")
  36. update_button()
  37. else:
  38. _extension_management_ui(package_name, title_name, requirements)
  39. # with gr.Tabs():
  40. main_tab()
  41. except Exception as e:
  42. generic_error_tab_advanced(
  43. e, name=title_name + " Extension", requirements=requirements
  44. )
  45. finally:
  46. elapsed_time = time.time() - start_time
  47. print(f" Done in {elapsed_time:.2f} seconds. ({title_name} Extension)\n")
  48. else:
  49. with gr.Tab(f"[Available] {title_name} Extension"):
  50. gr.Markdown(f"{title_name} Extension not installed")
  51. install_btn = gr.Button(f"Install {title_name} Extension")
  52. gr.Markdown("Installation console:")
  53. console_text = gr.HTML()
  54. install_btn.click(
  55. pip_install_wrapper(requirements, title_name),
  56. outputs=[console_text],
  57. )
  58. def disable_extension(package_name):
  59. def _disable_extension():
  60. disabled_extensions.append(package_name)
  61. print(f"Disabled extension {package_name}")
  62. return _disable_extension
  63. def get_latest_version(package_name):
  64. def _get_latest_version():
  65. print(f"Getting latest version of {package_name}")
  66. for line in pip_install_wrapper(
  67. f"--dry-run --no-deps {package_name}",
  68. f"{package_name} (dry run, update check)",
  69. )():
  70. if "Would install" in line:
  71. return line.split(" ")[-1]
  72. return "Already up to date"
  73. return _get_latest_version
  74. def _extension_management_ui(package_name, title_name, requirements):
  75. with gr.Accordion("Manage Extension", open=True):
  76. output = gr.HTML(render=False)
  77. with gr.Row():
  78. gr.Button("Check for updates").click(
  79. get_latest_version(package_name),
  80. outputs=[output],
  81. )
  82. gr.Button("Attempt Update", variant="primary").click(
  83. pip_install_wrapper(requirements, title_name),
  84. outputs=[output],
  85. )
  86. gr.Button("Uninstall Extension", variant="stop").click(
  87. pip_uninstall_wrapper(package_name, title_name),
  88. outputs=[output],
  89. )
  90. gr.Button("Disable Extension", visible=False).click(
  91. fn=disable_extension(package_name),
  92. outputs=[output],
  93. )
  94. gr.Markdown("Console:")
  95. output.render()
  96. def get_extension_list_json():
  97. try:
  98. return json.load(open("extensions.json"))["tabs"]
  99. except Exception as e:
  100. print("\n! Failed to load extensions.json:", e)
  101. return []
  102. extension_list_json = get_extension_list_json()
  103. def handle_extension_class(extension_class, config):
  104. for x in extension_list_json:
  105. if (
  106. x["extension_type"] == "interface"
  107. and x["extension_class"] == extension_class
  108. ):
  109. # x["package_name"], f"{x['name']} (v{x['version']})", x["requirements"]
  110. _handle_package(x["package_name"], x["name"], x["requirements"])
  111. def extension_list_tab():
  112. with gr.Tab("Extensions List"):
  113. gr.Markdown("List of all extensions")
  114. table_string = """| Title | Description |\n| --- | --- |\n"""
  115. for x in extension_list_json:
  116. table_string += (
  117. # f"| {x['name']} (v{x['version']}) "
  118. f"| {x['name']} "
  119. + f"| {x['description']} (website: {x['website']}) (extension_website: {x['extension_website']}) |\n"
  120. )
  121. gr.Markdown(table_string)
  122. external_extension_list = [
  123. x for x in extension_list_json if "builtin" not in x["package_name"]
  124. ]
  125. with gr.Row():
  126. with gr.Column():
  127. gr.Markdown("Install/Uninstall Extensions")
  128. install_dropdown = gr.Dropdown(
  129. label="Select Extension to Install",
  130. choices=[x["package_name"] for x in external_extension_list],
  131. )
  132. install_button = gr.Button("Install extension")
  133. def install_extension(package_name):
  134. requirements = [
  135. x["requirements"]
  136. for x in external_extension_list
  137. if x["package_name"] == package_name
  138. ][0]
  139. yield from pip_install_wrapper(requirements, package_name)()
  140. install_button.click(
  141. fn=install_extension,
  142. inputs=[install_dropdown],
  143. outputs=[gr.HTML()],
  144. api_name="install_extension",
  145. )
  146. with gr.Column():
  147. gr.Markdown("Uninstall Extensions")
  148. uninstall_dropdown = gr.Dropdown(
  149. label="Select Extension to Uninstall",
  150. choices=[x["package_name"] for x in external_extension_list],
  151. )
  152. uninstall_button = gr.Button("Uninstall extension")
  153. uninstall_button.click(
  154. fn=uninstall_extension,
  155. inputs=[uninstall_dropdown],
  156. outputs=[gr.HTML()],
  157. api_name="uninstall_extension",
  158. )
  159. if __name__ == "__main__":
  160. with gr.Blocks() as demo:
  161. handle_extension_class("audio-music-generation")
  162. handle_extension_class("audio-conversion")
  163. handle_extension_class("text-to-speech")
  164. demo.launch()