main.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import os
  2. import gradio as gr
  3. import json
  4. import shutil
  5. from tts_webui.history_tab.collections_directories_atom import (
  6. collections_directories_atom,
  7. get_collections,
  8. )
  9. from tts_webui.history_tab.delete_generation_cb import delete_generation_cb
  10. from tts_webui.history_tab.save_to_favorites import save_to_collection, save_to_favorites
  11. from tts_webui.history_tab.open_folder import open_folder
  12. import glob
  13. import os
  14. def extension__tts_generation_webui():
  15. history_content(directory="outputs", show_collections=True)
  16. return {
  17. "package_name": "extension_gallery_history",
  18. "name": "Gallery History",
  19. "version": "0.0.1",
  20. "requirements": "git+https://github.com/rsxdalv/extension_gallery_history@main",
  21. "description": "Gallery History allows selecting previously generated audio files by looking at their waveforms",
  22. "extension_type": "interface",
  23. "extension_class": "outputs",
  24. "author": "rsxdalv",
  25. "extension_author": "rsxdalv",
  26. "license": "MIT",
  27. "website": "https://github.com/rsxdalv/extension_gallery_history",
  28. "extension_website": "https://github.com/rsxdalv/extension_gallery_history",
  29. "extension_platform_version": "0.0.1",
  30. }
  31. audio_list_img = []
  32. def get_wav_files_img(directory: str):
  33. list_of_directories = glob.glob(f"{directory}/**/*.png", recursive=True)
  34. return list_of_directories
  35. def clear_audio():
  36. return [
  37. gr.Audio(value=None),
  38. gr.Image(value=None),
  39. gr.JSON(value=None),
  40. gr.Button(visible=False),
  41. ]
  42. def save_to_voices_cb(npz_filename: str):
  43. shutil.copy(npz_filename, "voices/")
  44. return gr.Button(value="Saved")
  45. def history_content(directory, show_collections):
  46. directories = get_collections()
  47. directory_dropdown = gr.Dropdown(
  48. value=directory,
  49. choices=directories,
  50. label="Select directory of the collection",
  51. visible=show_collections,
  52. )
  53. collections_directories_atom.change(
  54. fn=lambda x: gr.Dropdown(choices=x),
  55. inputs=[collections_directories_atom],
  56. outputs=[directory_dropdown],
  57. )
  58. if show_collections:
  59. create_collection_ui(collections_directories_atom)
  60. gr.Markdown(
  61. """
  62. ### Disclaimer:
  63. This gallery shows only the files that have an image in the same directory as the audio file.
  64. The audio generations without an image will not appear at all.
  65. """
  66. )
  67. with gr.Row():
  68. with gr.Column():
  69. with gr.Row():
  70. button_output = gr.Button(value=f"Open collection folder")
  71. reload_button = gr.Button(value="Refresh", variant="secondary")
  72. button_output.click(
  73. lambda x: open_folder(x),
  74. inputs=[directory_dropdown],
  75. )
  76. history_list_as_gallery = gr.Gallery(
  77. value=[], columns=4, object_fit="contain", height="auto"
  78. )
  79. with gr.Column():
  80. history_bundle_name = gr.Markdown(visible=True)
  81. folder_root = gr.Textbox(visible=False)
  82. history_audio = gr.Audio(visible=True, type="filepath", show_label=False)
  83. history_image = gr.Image(show_label=False)
  84. history_json = gr.JSON()
  85. history_npz = gr.Textbox(visible=False)
  86. with gr.Row():
  87. delete_from_history = gr.Button(
  88. value="Delete", variant="stop", visible=False
  89. )
  90. save_to_favorites_history = gr.Button(
  91. value="Save to favorites", variant="primary", visible=False
  92. )
  93. gr.Markdown("""Use as voice button is now only available in React UI""")
  94. save_to_voices = gr.Button(
  95. value="Save to voices", variant="secondary", visible=False
  96. )
  97. open_folder_button = gr.Button(
  98. value="Open folder", variant="secondary", visible=False
  99. )
  100. open_folder_button.click(open_folder, inputs=folder_root)
  101. save_to_favorites_history.click(
  102. fn=save_to_favorites,
  103. inputs=folder_root,
  104. outputs=save_to_favorites_history,
  105. )
  106. save_to_voices.click(
  107. fn=save_to_voices_cb,
  108. inputs=history_npz,
  109. outputs=save_to_voices,
  110. )
  111. save_to_collection_ui(
  112. directory,
  113. directories,
  114. folder_root,
  115. collections_directories_atom,
  116. )
  117. def _select_audio_history(filename: str, json_text):
  118. return {
  119. history_bundle_name: gr.Textbox(value=os.path.dirname(filename)),
  120. folder_root: os.path.dirname(filename),
  121. history_audio: gr.Audio(value=filename, label=filename),
  122. history_image: gr.Image(value=filename.replace(".wav", ".png")),
  123. history_json: gr.JSON(value=json_text),
  124. history_npz: gr.Textbox(value=filename.replace(".wav", ".npz")),
  125. delete_from_history: gr.Button(visible=True),
  126. save_to_favorites_history: gr.Button(
  127. visible=directory != "favorites", value="Save to favorites"
  128. ),
  129. save_to_voices: gr.Button(visible=True, value="Save to voices"),
  130. open_folder_button: gr.Button(visible=True),
  131. }
  132. def select_audio_history2(_list, evt: gr.SelectData):
  133. filename = audio_list_img[evt.index].replace(".png", ".wav") # type: ignore
  134. json_text = json.load(open(filename.replace(".wav", ".json")))
  135. return _select_audio_history(filename, json_text)
  136. outputs = [
  137. history_bundle_name,
  138. folder_root,
  139. history_audio,
  140. history_image,
  141. history_json,
  142. history_npz,
  143. delete_from_history,
  144. save_to_favorites_history,
  145. save_to_voices,
  146. open_folder_button,
  147. ]
  148. history_list_as_gallery.select(
  149. fn=select_audio_history2,
  150. inputs=[history_list_as_gallery],
  151. outputs=outputs,
  152. preprocess=False,
  153. )
  154. def update_history_tab(directory: str):
  155. global audio_list_img
  156. audio_list_img = get_wav_files_img(directory)
  157. return gr.Gallery(value=audio_list_img)
  158. delete_from_history.click(
  159. fn=clear_audio,
  160. outputs=[history_audio, history_image, history_json, delete_from_history],
  161. )
  162. delete_from_history.click(
  163. fn=delete_generation_cb(update_history_tab),
  164. inputs=[folder_root, directory_dropdown],
  165. outputs=[history_list_as_gallery],
  166. )
  167. directory_dropdown.change(
  168. fn=update_history_tab,
  169. inputs=[directory_dropdown],
  170. outputs=[history_list_as_gallery],
  171. )
  172. reload_button.click(
  173. fn=update_history_tab,
  174. inputs=[directory_dropdown],
  175. outputs=[history_list_as_gallery],
  176. )
  177. def save_to_collection_ui(
  178. directory: str,
  179. directories: list[str],
  180. folder_root: gr.Textbox,
  181. directories_state: gr.JSON,
  182. ):
  183. with gr.Row():
  184. move_to_collection = gr.Dropdown(
  185. label="Save to collection",
  186. choices=directories,
  187. value=directory,
  188. )
  189. move_to_collection.select(
  190. fn=save_to_collection,
  191. inputs=[folder_root, move_to_collection],
  192. outputs=[move_to_collection],
  193. )
  194. directories_state.change(
  195. fn=lambda x: gr.Dropdown(choices=x),
  196. inputs=[directories_state],
  197. outputs=[move_to_collection],
  198. )
  199. def create_collection_ui(directories_state):
  200. new_collection_name = gr.Textbox(label="New collection name", value="")
  201. def create_collection(new_collection_name):
  202. os.makedirs(os.path.join("collections", new_collection_name))
  203. return [
  204. get_collections(),
  205. gr.Button(value="Created"),
  206. ]
  207. create_collection_button = gr.Button(value="Create collection")
  208. new_collection_name.change(
  209. fn=lambda: gr.Button(value="Create collection"),
  210. outputs=[create_collection_button],
  211. )
  212. create_collection_button.click(
  213. fn=create_collection,
  214. inputs=[new_collection_name],
  215. outputs=[directories_state, create_collection_button],
  216. )