2
0

TortoiseParameters.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import gradio as gr
  2. from typing import TypedDict, Optional
  3. class _TortoiseParametersTypedDict(TypedDict):
  4. text: str
  5. voice: str
  6. preset: str
  7. seed: Optional[int]
  8. cvvp_amount: float
  9. split_prompt: bool
  10. num_autoregressive_samples: int
  11. diffusion_iterations: int
  12. temperature: float
  13. length_penalty: float
  14. repetition_penalty: float
  15. top_p: float
  16. max_mel_tokens: int
  17. cond_free: bool
  18. cond_free_k: int
  19. diffusion_temperature: float
  20. model: str
  21. name: str
  22. class TortoiseParameters:
  23. def __init__(
  24. self,
  25. text: str,
  26. voice: str = "random",
  27. preset: str = "ultra_fast",
  28. seed: int | None = None,
  29. cvvp_amount: float = 0.0,
  30. split_prompt: bool = False,
  31. num_autoregressive_samples: int = 16,
  32. diffusion_iterations: int = 16,
  33. temperature: float = 0.8,
  34. length_penalty: float = 1.0,
  35. repetition_penalty: float = 2.0,
  36. top_p: float = 0.8,
  37. max_mel_tokens: int = 500,
  38. cond_free: bool = True,
  39. cond_free_k: int = 2,
  40. diffusion_temperature: float = 1.0,
  41. model: str = "Default",
  42. name: str = "",
  43. ): # sourcery skip: remove-unnecessary-cast
  44. self.text = text
  45. self.voice = voice
  46. self.preset = preset
  47. self.seed = seed
  48. self.cvvp_amount = float(cvvp_amount)
  49. self.split_prompt = split_prompt
  50. self.num_autoregressive_samples = num_autoregressive_samples
  51. self.diffusion_iterations = diffusion_iterations
  52. self.temperature = float(temperature)
  53. self.length_penalty = float(length_penalty)
  54. self.repetition_penalty = float(repetition_penalty)
  55. self.top_p = float(top_p)
  56. self.max_mel_tokens = max_mel_tokens
  57. self.cond_free = cond_free
  58. self.cond_free_k = cond_free_k
  59. self.diffusion_temperature = float(diffusion_temperature)
  60. self.model = model
  61. self.name = name
  62. def __repr__(self):
  63. params = ",\n ".join(f"{k}={v!r}" for k, v in self.__dict__.items())
  64. return f"TortoiseParameters(\n {params}\n)"
  65. def __iter__(self):
  66. return iter(TortoiseParameterZipper.to_list(self))
  67. def to_dict(self):
  68. return self.__dict__
  69. def to_metadata(self):
  70. return {
  71. **self.__dict__,
  72. "seed": str(self.seed),
  73. }
  74. @staticmethod
  75. def from_list(components: list):
  76. return TortoiseParameters(
  77. **TortoiseParameterZipper.from_list_to_dict(components)
  78. )
  79. class TortoiseParameterComponents:
  80. def __init__(
  81. self,
  82. text: gr.Textbox,
  83. voice: gr.Dropdown,
  84. preset: gr.Dropdown,
  85. seed: gr.Textbox,
  86. cvvp_amount: gr.Slider,
  87. split_prompt: gr.Checkbox,
  88. num_autoregressive_samples: gr.Slider,
  89. diffusion_iterations: gr.Slider,
  90. temperature: gr.Slider,
  91. length_penalty: gr.Slider,
  92. repetition_penalty: gr.Slider,
  93. top_p: gr.Slider,
  94. max_mel_tokens: gr.Slider,
  95. cond_free: gr.Checkbox,
  96. cond_free_k: gr.Slider,
  97. diffusion_temperature: gr.Slider,
  98. model: gr.Dropdown,
  99. name: gr.Textbox,
  100. ):
  101. self.text = text
  102. self.voice = voice
  103. self.preset = preset
  104. self.seed = seed
  105. self.cvvp_amount = cvvp_amount
  106. self.split_prompt = split_prompt
  107. self.num_autoregressive_samples = num_autoregressive_samples
  108. self.diffusion_iterations = diffusion_iterations
  109. self.temperature = temperature
  110. self.length_penalty = length_penalty
  111. self.repetition_penalty = repetition_penalty
  112. self.top_p = top_p
  113. self.max_mel_tokens = max_mel_tokens
  114. self.cond_free = cond_free
  115. self.cond_free_k = cond_free_k
  116. self.diffusion_temperature = diffusion_temperature
  117. self.model = model
  118. self.name = name
  119. def __repr__(self):
  120. params = ",\n ".join(f"{k}={v!r}" for k, v in self.__dict__.items())
  121. return f"TortoiseParameterComponents(\n {params}\n)"
  122. def __iter__(self):
  123. return iter(TortoiseParameterZipper.to_list(self))
  124. class TortoiseParameterZipper:
  125. @staticmethod
  126. def to_list(components: TortoiseParameterComponents | TortoiseParameters):
  127. return [
  128. components.text,
  129. components.voice,
  130. components.preset,
  131. components.seed,
  132. components.cvvp_amount,
  133. components.split_prompt,
  134. components.num_autoregressive_samples,
  135. components.diffusion_iterations,
  136. components.temperature,
  137. components.length_penalty,
  138. components.repetition_penalty,
  139. components.top_p,
  140. components.max_mel_tokens,
  141. components.cond_free,
  142. components.cond_free_k,
  143. components.diffusion_temperature,
  144. components.model,
  145. components.name,
  146. ]
  147. @staticmethod
  148. def from_list_to_dict(components: list):
  149. def next_idx():
  150. next_idx.idx += 1
  151. return next_idx.idx - 1
  152. next_idx.idx = 0
  153. return {
  154. "text": components[next_idx()],
  155. "voice": components[next_idx()],
  156. "preset": components[next_idx()],
  157. "seed": components[next_idx()],
  158. "cvvp_amount": components[next_idx()],
  159. "split_prompt": components[next_idx()],
  160. "num_autoregressive_samples": components[next_idx()],
  161. "diffusion_iterations": components[next_idx()],
  162. "temperature": components[next_idx()],
  163. "length_penalty": components[next_idx()],
  164. "repetition_penalty": components[next_idx()],
  165. "top_p": components[next_idx()],
  166. "max_mel_tokens": components[next_idx()],
  167. "cond_free": components[next_idx()],
  168. "cond_free_k": components[next_idx()],
  169. "diffusion_temperature": components[next_idx()],
  170. "model": components[next_idx()],
  171. "name": components[next_idx()],
  172. }
  173. if __name__ == "__main__":
  174. with gr.Blocks() as demo:
  175. b = TortoiseParameterComponents(
  176. text=gr.Textbox(label="Prompt", lines=3, placeholder="Enter text here..."),
  177. voice=gr.Dropdown(
  178. show_label=False,
  179. choices=["random"],
  180. value="random",
  181. ),
  182. preset=gr.Dropdown(
  183. show_label=False,
  184. choices=[
  185. "ultra_fast",
  186. "fast",
  187. "standard",
  188. "high_quality",
  189. ],
  190. value="ultra_fast",
  191. ),
  192. seed=gr.Textbox(label="Seed", value=None),
  193. cvvp_amount=gr.Slider(
  194. label="CVVP Amount", value=0.0, minimum=0.0, maximum=1.0, step=0.1
  195. ),
  196. split_prompt=gr.Checkbox(label="Split prompt by lines", value=False),
  197. num_autoregressive_samples=gr.Slider(
  198. label="Num Autoregressive Samples",
  199. value=16,
  200. minimum=1,
  201. maximum=256,
  202. step=1,
  203. ),
  204. diffusion_iterations=gr.Slider(
  205. label="Diffusion Iterations", value=30, minimum=1, maximum=400, step=1
  206. ),
  207. temperature=gr.Slider(
  208. label="Autoregressive Temperature",
  209. value=0.8,
  210. minimum=0.0,
  211. maximum=1.0,
  212. step=0.1,
  213. ),
  214. length_penalty=gr.Slider(
  215. label="Autoregressive Length Penalty",
  216. value=1.0,
  217. minimum=0.0,
  218. maximum=10.0,
  219. step=0.1,
  220. ),
  221. repetition_penalty=gr.Slider(
  222. label="Autoregressive Repetition Penalty",
  223. value=2.0,
  224. minimum=0.0,
  225. maximum=10.0,
  226. step=0.1,
  227. ),
  228. top_p=gr.Slider(
  229. label="Autoregressive Top P",
  230. value=0.8,
  231. minimum=0.0,
  232. maximum=1.0,
  233. step=0.1,
  234. ),
  235. max_mel_tokens=gr.Slider(
  236. label="Autoregressive Max Mel Tokens",
  237. value=500,
  238. minimum=0,
  239. maximum=600,
  240. step=1,
  241. ),
  242. cond_free=gr.Checkbox(label="Diffusion Cond Free", value=True),
  243. cond_free_k=gr.Slider(
  244. label="Diffusion Cond Free K", value=2, minimum=0, maximum=10, step=1
  245. ),
  246. diffusion_temperature=gr.Slider(
  247. label="Diffusion Temperature",
  248. value=1.0,
  249. minimum=0.0,
  250. maximum=1.0,
  251. step=0.1,
  252. ),
  253. model=gr.Dropdown(
  254. show_label=False,
  255. choices=["Default"],
  256. value="Default",
  257. ),
  258. name=gr.Textbox(
  259. label="Name",
  260. placeholder="Enter name here...",
  261. ),
  262. )
  263. button = gr.Button("Generate")
  264. button.click(
  265. lambda *x: print(
  266. TortoiseParameters(**TortoiseParameterZipper.from_list_to_dict(list(x)))
  267. ),
  268. inputs=list(b),
  269. )
  270. demo.launch()