xinference 0.14.1.post1__py3-none-any.whl → 0.14.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xinference might be problematic. Click here for more details.

Files changed (194) hide show
  1. xinference/_version.py +3 -3
  2. xinference/api/restful_api.py +15 -34
  3. xinference/client/restful/restful_client.py +2 -2
  4. xinference/core/chat_interface.py +45 -10
  5. xinference/core/image_interface.py +9 -0
  6. xinference/core/model.py +8 -5
  7. xinference/core/scheduler.py +1 -2
  8. xinference/core/worker.py +49 -42
  9. xinference/deploy/cmdline.py +2 -2
  10. xinference/deploy/test/test_cmdline.py +7 -7
  11. xinference/model/audio/chattts.py +24 -9
  12. xinference/model/audio/core.py +8 -2
  13. xinference/model/audio/fish_speech.py +228 -0
  14. xinference/model/audio/model_spec.json +8 -0
  15. xinference/model/embedding/core.py +23 -1
  16. xinference/model/image/model_spec.json +2 -1
  17. xinference/model/image/model_spec_modelscope.json +2 -1
  18. xinference/model/image/stable_diffusion/core.py +49 -1
  19. xinference/model/llm/__init__.py +26 -27
  20. xinference/model/llm/{ggml/llamacpp.py → llama_cpp/core.py} +2 -35
  21. xinference/model/llm/llm_family.json +606 -1266
  22. xinference/model/llm/llm_family.py +16 -139
  23. xinference/model/llm/llm_family_modelscope.json +276 -313
  24. xinference/model/llm/lmdeploy/__init__.py +0 -0
  25. xinference/model/llm/lmdeploy/core.py +557 -0
  26. xinference/model/llm/memory.py +9 -9
  27. xinference/model/llm/sglang/core.py +2 -2
  28. xinference/model/llm/{pytorch → transformers}/chatglm.py +6 -13
  29. xinference/model/llm/{pytorch → transformers}/cogvlm2.py +4 -45
  30. xinference/model/llm/transformers/cogvlm2_video.py +524 -0
  31. xinference/model/llm/{pytorch → transformers}/core.py +3 -10
  32. xinference/model/llm/{pytorch → transformers}/glm4v.py +2 -23
  33. xinference/model/llm/transformers/intern_vl.py +540 -0
  34. xinference/model/llm/{pytorch → transformers}/internlm2.py +4 -8
  35. xinference/model/llm/{pytorch → transformers}/minicpmv25.py +2 -23
  36. xinference/model/llm/{pytorch → transformers}/minicpmv26.py +66 -41
  37. xinference/model/llm/{pytorch → transformers}/utils.py +1 -2
  38. xinference/model/llm/{pytorch → transformers}/yi_vl.py +2 -24
  39. xinference/model/llm/utils.py +85 -70
  40. xinference/model/llm/vllm/core.py +110 -11
  41. xinference/model/utils.py +1 -95
  42. xinference/thirdparty/fish_speech/__init__.py +0 -0
  43. xinference/thirdparty/fish_speech/fish_speech/__init__.py +0 -0
  44. xinference/thirdparty/fish_speech/fish_speech/callbacks/__init__.py +3 -0
  45. xinference/thirdparty/fish_speech/fish_speech/callbacks/grad_norm.py +113 -0
  46. xinference/thirdparty/fish_speech/fish_speech/configs/__init__.py +0 -0
  47. xinference/thirdparty/fish_speech/fish_speech/configs/lora/__init__.py +0 -0
  48. xinference/thirdparty/fish_speech/fish_speech/conversation.py +2 -0
  49. xinference/thirdparty/fish_speech/fish_speech/datasets/__init__.py +0 -0
  50. xinference/thirdparty/fish_speech/fish_speech/datasets/concat_repeat.py +53 -0
  51. xinference/thirdparty/fish_speech/fish_speech/datasets/protos/__init__.py +0 -0
  52. xinference/thirdparty/fish_speech/fish_speech/datasets/protos/text_data_pb2.py +33 -0
  53. xinference/thirdparty/fish_speech/fish_speech/datasets/protos/text_data_stream.py +36 -0
  54. xinference/thirdparty/fish_speech/fish_speech/datasets/semantic.py +496 -0
  55. xinference/thirdparty/fish_speech/fish_speech/datasets/vqgan.py +147 -0
  56. xinference/thirdparty/fish_speech/fish_speech/i18n/__init__.py +3 -0
  57. xinference/thirdparty/fish_speech/fish_speech/i18n/core.py +40 -0
  58. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/__init__.py +0 -0
  59. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/en_US.json +122 -0
  60. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/es_ES.json +122 -0
  61. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/ja_JP.json +123 -0
  62. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/pt_BR.json +133 -0
  63. xinference/thirdparty/fish_speech/fish_speech/i18n/locale/zh_CN.json +122 -0
  64. xinference/thirdparty/fish_speech/fish_speech/i18n/scan.py +122 -0
  65. xinference/thirdparty/fish_speech/fish_speech/models/__init__.py +0 -0
  66. xinference/thirdparty/fish_speech/fish_speech/models/text2semantic/__init__.py +0 -0
  67. xinference/thirdparty/fish_speech/fish_speech/models/text2semantic/lit_module.py +202 -0
  68. xinference/thirdparty/fish_speech/fish_speech/models/text2semantic/llama.py +779 -0
  69. xinference/thirdparty/fish_speech/fish_speech/models/text2semantic/lora.py +92 -0
  70. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/__init__.py +3 -0
  71. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/lit_module.py +442 -0
  72. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/__init__.py +0 -0
  73. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/discriminator.py +44 -0
  74. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/firefly.py +625 -0
  75. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/fsq.py +139 -0
  76. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/reference.py +115 -0
  77. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/modules/wavenet.py +225 -0
  78. xinference/thirdparty/fish_speech/fish_speech/models/vqgan/utils.py +94 -0
  79. xinference/thirdparty/fish_speech/fish_speech/scheduler.py +40 -0
  80. xinference/thirdparty/fish_speech/fish_speech/text/__init__.py +4 -0
  81. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/__init__.py +0 -0
  82. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/basic_class.py +172 -0
  83. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/basic_constant.py +30 -0
  84. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/basic_util.py +342 -0
  85. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/cardinal.py +32 -0
  86. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/date.py +75 -0
  87. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/digit.py +32 -0
  88. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/fraction.py +35 -0
  89. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/money.py +43 -0
  90. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/percentage.py +33 -0
  91. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/telephone.py +51 -0
  92. xinference/thirdparty/fish_speech/fish_speech/text/chn_text_norm/text.py +177 -0
  93. xinference/thirdparty/fish_speech/fish_speech/text/clean.py +69 -0
  94. xinference/thirdparty/fish_speech/fish_speech/text/spliter.py +130 -0
  95. xinference/thirdparty/fish_speech/fish_speech/train.py +139 -0
  96. xinference/thirdparty/fish_speech/fish_speech/utils/__init__.py +23 -0
  97. xinference/thirdparty/fish_speech/fish_speech/utils/braceexpand.py +217 -0
  98. xinference/thirdparty/fish_speech/fish_speech/utils/context.py +13 -0
  99. xinference/thirdparty/fish_speech/fish_speech/utils/file.py +16 -0
  100. xinference/thirdparty/fish_speech/fish_speech/utils/instantiators.py +50 -0
  101. xinference/thirdparty/fish_speech/fish_speech/utils/logger.py +55 -0
  102. xinference/thirdparty/fish_speech/fish_speech/utils/logging_utils.py +48 -0
  103. xinference/thirdparty/fish_speech/fish_speech/utils/rich_utils.py +100 -0
  104. xinference/thirdparty/fish_speech/fish_speech/utils/spectrogram.py +122 -0
  105. xinference/thirdparty/fish_speech/fish_speech/utils/utils.py +114 -0
  106. xinference/thirdparty/fish_speech/fish_speech/webui/__init__.py +0 -0
  107. xinference/thirdparty/fish_speech/fish_speech/webui/launch_utils.py +120 -0
  108. xinference/thirdparty/fish_speech/fish_speech/webui/manage.py +1237 -0
  109. xinference/thirdparty/fish_speech/tools/__init__.py +0 -0
  110. xinference/thirdparty/fish_speech/tools/api.py +495 -0
  111. xinference/thirdparty/fish_speech/tools/auto_rerank.py +159 -0
  112. xinference/thirdparty/fish_speech/tools/download_models.py +55 -0
  113. xinference/thirdparty/fish_speech/tools/extract_model.py +21 -0
  114. xinference/thirdparty/fish_speech/tools/file.py +108 -0
  115. xinference/thirdparty/fish_speech/tools/gen_ref.py +36 -0
  116. xinference/thirdparty/fish_speech/tools/llama/__init__.py +0 -0
  117. xinference/thirdparty/fish_speech/tools/llama/build_dataset.py +169 -0
  118. xinference/thirdparty/fish_speech/tools/llama/eval_in_context.py +171 -0
  119. xinference/thirdparty/fish_speech/tools/llama/generate.py +698 -0
  120. xinference/thirdparty/fish_speech/tools/llama/merge_lora.py +95 -0
  121. xinference/thirdparty/fish_speech/tools/llama/quantize.py +497 -0
  122. xinference/thirdparty/fish_speech/tools/llama/rebuild_tokenizer.py +57 -0
  123. xinference/thirdparty/fish_speech/tools/merge_asr_files.py +55 -0
  124. xinference/thirdparty/fish_speech/tools/post_api.py +164 -0
  125. xinference/thirdparty/fish_speech/tools/sensevoice/__init__.py +0 -0
  126. xinference/thirdparty/fish_speech/tools/sensevoice/auto_model.py +573 -0
  127. xinference/thirdparty/fish_speech/tools/sensevoice/fun_asr.py +332 -0
  128. xinference/thirdparty/fish_speech/tools/sensevoice/vad_utils.py +61 -0
  129. xinference/thirdparty/fish_speech/tools/smart_pad.py +47 -0
  130. xinference/thirdparty/fish_speech/tools/vqgan/__init__.py +0 -0
  131. xinference/thirdparty/fish_speech/tools/vqgan/create_train_split.py +83 -0
  132. xinference/thirdparty/fish_speech/tools/vqgan/extract_vq.py +227 -0
  133. xinference/thirdparty/fish_speech/tools/vqgan/inference.py +120 -0
  134. xinference/thirdparty/fish_speech/tools/webui.py +619 -0
  135. xinference/thirdparty/fish_speech/tools/whisper_asr.py +176 -0
  136. xinference/thirdparty/internvl/__init__.py +0 -0
  137. xinference/thirdparty/internvl/conversation.py +393 -0
  138. xinference/thirdparty/omnilmm/model/utils.py +16 -1
  139. xinference/web/ui/build/asset-manifest.json +3 -3
  140. xinference/web/ui/build/index.html +1 -1
  141. xinference/web/ui/build/static/js/main.661c7b0a.js +3 -0
  142. xinference/web/ui/build/static/js/{main.17ca0398.js.map → main.661c7b0a.js.map} +1 -1
  143. xinference/web/ui/node_modules/.cache/babel-loader/070d8c6b3b0f3485c6d3885f0b6bbfdf9643e088a468acbd5d596f2396071c16.json +1 -0
  144. xinference/web/ui/node_modules/.cache/babel-loader/213b5913e164773c2b0567455377765715f5f07225fbac77ad8e1e9dc9648a47.json +1 -0
  145. xinference/web/ui/node_modules/.cache/babel-loader/4de9a6942c5f1749d6cbfdd54279699975f16016b182848bc253886f52ec2ec3.json +1 -0
  146. xinference/web/ui/node_modules/.cache/babel-loader/5391543180fead1eeef5364300301498d58a7d91d62de3841a32768b67f4552f.json +1 -0
  147. xinference/web/ui/node_modules/.cache/babel-loader/5c26a23b5eacf5b752a08531577ae3840bb247745ef9a39583dc2d05ba93a82a.json +1 -0
  148. xinference/web/ui/node_modules/.cache/babel-loader/714c37ce0ec5b5c591033f02be2f3f491fdd70da3ef568ee4a4f94689a3d5ca2.json +1 -0
  149. xinference/web/ui/node_modules/.cache/babel-loader/822586ed1077201b64b954f12f25e3f9b45678c1acbabe53d8af3ca82ca71f33.json +1 -0
  150. xinference/web/ui/node_modules/.cache/babel-loader/978b57d1a04a701bc3fcfebc511f5f274eed6ed7eade67f6fb76c27d5fd9ecc8.json +1 -0
  151. xinference/web/ui/node_modules/.cache/babel-loader/a797831de0dc74897f4b50b3426555d748f328b4c2cc391de709eadaf6a5f3e3.json +1 -0
  152. xinference/web/ui/node_modules/.cache/babel-loader/bd6ad8159341315a1764c397621a560809f7eb7219ab5174c801fca7e969d943.json +1 -0
  153. xinference/web/ui/node_modules/.cache/babel-loader/e64b7e8cedcf43d4c95deba60ec1341855c887705805bb62431693118b870c69.json +1 -0
  154. xinference/web/ui/node_modules/.cache/babel-loader/e91938976f229ce986b2907e51e1f00540b584ced0a315d498c172d13220739d.json +1 -0
  155. xinference/web/ui/node_modules/.cache/babel-loader/f72f011744c4649fabddca6f7a9327861ac0a315a89b1a2e62a39774e7863845.json +1 -0
  156. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/METADATA +22 -13
  157. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/RECORD +170 -79
  158. xinference/locale/utils.py +0 -39
  159. xinference/locale/zh_CN.json +0 -26
  160. xinference/model/llm/ggml/tools/__init__.py +0 -15
  161. xinference/model/llm/ggml/tools/convert_ggml_to_gguf.py +0 -498
  162. xinference/model/llm/ggml/tools/gguf.py +0 -884
  163. xinference/model/llm/pytorch/__init__.py +0 -13
  164. xinference/model/llm/pytorch/baichuan.py +0 -81
  165. xinference/model/llm/pytorch/falcon.py +0 -138
  166. xinference/model/llm/pytorch/intern_vl.py +0 -352
  167. xinference/model/llm/pytorch/vicuna.py +0 -69
  168. xinference/web/ui/build/static/js/main.17ca0398.js +0 -3
  169. xinference/web/ui/node_modules/.cache/babel-loader/1444c41a4d04494f1cbc2d8c1537df107b451cb569cb2c1fbf5159f3a4841a5f.json +0 -1
  170. xinference/web/ui/node_modules/.cache/babel-loader/2f40209b32e7e46a2eab6b8c8a355eb42c3caa8bc3228dd929f32fd2b3940294.json +0 -1
  171. xinference/web/ui/node_modules/.cache/babel-loader/44774c783428f952d8e2e4ad0998a9c5bc16a57cd9c68b7c5ff18aaa5a41d65c.json +0 -1
  172. xinference/web/ui/node_modules/.cache/babel-loader/5262556baf9207738bf6a8ba141ec6599d0a636345c245d61fdf88d3171998cb.json +0 -1
  173. xinference/web/ui/node_modules/.cache/babel-loader/6450605fac003812485f6251b9f0caafbf2e5bfc3bbe2f000050d9e2fdb8dcd3.json +0 -1
  174. xinference/web/ui/node_modules/.cache/babel-loader/71684495d995c7e266eecc6a0ad8ea0284cc785f80abddf863789c57a6134969.json +0 -1
  175. xinference/web/ui/node_modules/.cache/babel-loader/80acd1edf31542ab1dcccfad02cb4b38f3325cff847a781fcce97500cfd6f878.json +0 -1
  176. xinference/web/ui/node_modules/.cache/babel-loader/8a9742ddd8ba8546ef42dc14caca443f2b4524fabed7bf269e0eff3b7b64ee7d.json +0 -1
  177. xinference/web/ui/node_modules/.cache/babel-loader/d06a96a3c9c32e42689094aa3aaad41c8125894e956b8f84a70fadce6e3f65b3.json +0 -1
  178. xinference/web/ui/node_modules/.cache/babel-loader/d93730e2b5d7e8c957b4d0965d2ed1dac9045a649adbd47c220d11f255d4b1e0.json +0 -1
  179. xinference/web/ui/node_modules/.cache/babel-loader/e656dc00b4d8b387f0a81ba8fc558767df1601c66369e2eb86a5ef27cf080572.json +0 -1
  180. xinference/web/ui/node_modules/.cache/babel-loader/f28b83886159d83b84f099b05d607a822dca4dd7f2d8aa6d56fe08bab0b5b086.json +0 -1
  181. xinference/web/ui/node_modules/.cache/babel-loader/f3e02274cb1964e99b1fe69cbb6db233d3d8d7dd05d50ebcdb8e66d50b224b7b.json +0 -1
  182. /xinference/{locale → model/llm/llama_cpp}/__init__.py +0 -0
  183. /xinference/model/llm/{ggml → transformers}/__init__.py +0 -0
  184. /xinference/model/llm/{pytorch → transformers}/compression.py +0 -0
  185. /xinference/model/llm/{pytorch → transformers}/deepseek_vl.py +0 -0
  186. /xinference/model/llm/{pytorch → transformers}/llama_2.py +0 -0
  187. /xinference/model/llm/{pytorch → transformers}/omnilmm.py +0 -0
  188. /xinference/model/llm/{pytorch → transformers}/qwen_vl.py +0 -0
  189. /xinference/model/llm/{pytorch → transformers}/tensorizer_utils.py +0 -0
  190. /xinference/web/ui/build/static/js/{main.17ca0398.js.LICENSE.txt → main.661c7b0a.js.LICENSE.txt} +0 -0
  191. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/LICENSE +0 -0
  192. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/WHEEL +0 -0
  193. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/entry_points.txt +0 -0
  194. {xinference-0.14.1.post1.dist-info → xinference-0.14.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,619 @@
1
+ import gc
2
+ import html
3
+ import io
4
+ import os
5
+ import queue
6
+ import wave
7
+ from argparse import ArgumentParser
8
+ from functools import partial
9
+ from pathlib import Path
10
+
11
+ import gradio as gr
12
+ import librosa
13
+ import numpy as np
14
+ # import pyrootutils
15
+ import torch
16
+ from loguru import logger
17
+ from transformers import AutoTokenizer
18
+
19
+ # pyrootutils.setup_root(__file__, indicator=".project-root", pythonpath=True)
20
+
21
+
22
+ from fish_speech.i18n import i18n
23
+ from fish_speech.text.chn_text_norm.text import Text as ChnNormedText
24
+ from fish_speech.utils import autocast_exclude_mps
25
+ from tools.api import decode_vq_tokens, encode_reference
26
+ from tools.auto_rerank import batch_asr, calculate_wer, is_chinese, load_model
27
+ from tools.llama.generate import (
28
+ GenerateRequest,
29
+ GenerateResponse,
30
+ WrappedGenerateResponse,
31
+ launch_thread_safe_queue,
32
+ )
33
+ from tools.vqgan.inference import load_model as load_decoder_model
34
+
35
+ # Make einx happy
36
+ os.environ["EINX_FILTER_TRACEBACK"] = "false"
37
+
38
+
39
+ HEADER_MD = f"""# Fish Speech
40
+
41
+ {i18n("A text-to-speech model based on VQ-GAN and Llama developed by [Fish Audio](https://fish.audio).")}
42
+
43
+ {i18n("You can find the source code [here](https://github.com/fishaudio/fish-speech) and models [here](https://huggingface.co/fishaudio/fish-speech-1).")}
44
+
45
+ {i18n("Related code are released under BSD-3-Clause License, and weights are released under CC BY-NC-SA 4.0 License.")}
46
+
47
+ {i18n("We are not responsible for any misuse of the model, please consider your local laws and regulations before using it.")}
48
+ """
49
+
50
+ TEXTBOX_PLACEHOLDER = i18n("Put your text here.")
51
+ SPACE_IMPORTED = False
52
+
53
+
54
+ def build_html_error_message(error):
55
+ return f"""
56
+ <div style="color: red;
57
+ font-weight: bold;">
58
+ {html.escape(str(error))}
59
+ </div>
60
+ """
61
+
62
+
63
+ @torch.inference_mode()
64
+ def inference(
65
+ text,
66
+ enable_reference_audio,
67
+ reference_audio,
68
+ reference_text,
69
+ max_new_tokens,
70
+ chunk_length,
71
+ top_p,
72
+ repetition_penalty,
73
+ temperature,
74
+ streaming=False,
75
+ ):
76
+ if args.max_gradio_length > 0 and len(text) > args.max_gradio_length:
77
+ return (
78
+ None,
79
+ None,
80
+ i18n("Text is too long, please keep it under {} characters.").format(
81
+ args.max_gradio_length
82
+ ),
83
+ )
84
+
85
+ # Parse reference audio aka prompt
86
+ prompt_tokens = encode_reference(
87
+ decoder_model=decoder_model,
88
+ reference_audio=reference_audio,
89
+ enable_reference_audio=enable_reference_audio,
90
+ )
91
+
92
+ # LLAMA Inference
93
+ request = dict(
94
+ device=decoder_model.device,
95
+ max_new_tokens=max_new_tokens,
96
+ text=text,
97
+ top_p=top_p,
98
+ repetition_penalty=repetition_penalty,
99
+ temperature=temperature,
100
+ compile=args.compile,
101
+ iterative_prompt=chunk_length > 0,
102
+ chunk_length=chunk_length,
103
+ max_length=2048,
104
+ prompt_tokens=prompt_tokens if enable_reference_audio else None,
105
+ prompt_text=reference_text if enable_reference_audio else None,
106
+ )
107
+
108
+ response_queue = queue.Queue()
109
+ llama_queue.put(
110
+ GenerateRequest(
111
+ request=request,
112
+ response_queue=response_queue,
113
+ )
114
+ )
115
+
116
+ if streaming:
117
+ yield wav_chunk_header(), None, None
118
+
119
+ segments = []
120
+
121
+ while True:
122
+ result: WrappedGenerateResponse = response_queue.get()
123
+ if result.status == "error":
124
+ yield None, None, build_html_error_message(result.response)
125
+ break
126
+
127
+ result: GenerateResponse = result.response
128
+ if result.action == "next":
129
+ break
130
+
131
+ with autocast_exclude_mps(
132
+ device_type=decoder_model.device.type, dtype=args.precision
133
+ ):
134
+ fake_audios = decode_vq_tokens(
135
+ decoder_model=decoder_model,
136
+ codes=result.codes,
137
+ )
138
+
139
+ fake_audios = fake_audios.float().cpu().numpy()
140
+ segments.append(fake_audios)
141
+
142
+ if streaming:
143
+ yield (fake_audios * 32768).astype(np.int16).tobytes(), None, None
144
+
145
+ if len(segments) == 0:
146
+ return (
147
+ None,
148
+ None,
149
+ build_html_error_message(
150
+ i18n("No audio generated, please check the input text.")
151
+ ),
152
+ )
153
+
154
+ # No matter streaming or not, we need to return the final audio
155
+ audio = np.concatenate(segments, axis=0)
156
+ yield None, (decoder_model.spec_transform.sample_rate, audio), None
157
+
158
+ if torch.cuda.is_available():
159
+ torch.cuda.empty_cache()
160
+ gc.collect()
161
+
162
+
163
+ def inference_with_auto_rerank(
164
+ text,
165
+ enable_reference_audio,
166
+ reference_audio,
167
+ reference_text,
168
+ max_new_tokens,
169
+ chunk_length,
170
+ top_p,
171
+ repetition_penalty,
172
+ temperature,
173
+ use_auto_rerank,
174
+ streaming=False,
175
+ ):
176
+
177
+ max_attempts = 2 if use_auto_rerank else 1
178
+ best_wer = float("inf")
179
+ best_audio = None
180
+ best_sample_rate = None
181
+
182
+ for attempt in range(max_attempts):
183
+ audio_generator = inference(
184
+ text,
185
+ enable_reference_audio,
186
+ reference_audio,
187
+ reference_text,
188
+ max_new_tokens,
189
+ chunk_length,
190
+ top_p,
191
+ repetition_penalty,
192
+ temperature,
193
+ streaming=False,
194
+ )
195
+
196
+ # 获取音频数据
197
+ for _ in audio_generator:
198
+ pass
199
+ _, (sample_rate, audio), message = _
200
+
201
+ if audio is None:
202
+ return None, None, message
203
+
204
+ if not use_auto_rerank:
205
+ return None, (sample_rate, audio), None
206
+
207
+ asr_result = batch_asr(asr_model, [audio], sample_rate)[0]
208
+ wer = calculate_wer(text, asr_result["text"])
209
+ if wer <= 0.3 and not asr_result["huge_gap"]:
210
+ return None, (sample_rate, audio), None
211
+
212
+ if wer < best_wer:
213
+ best_wer = wer
214
+ best_audio = audio
215
+ best_sample_rate = sample_rate
216
+
217
+ if attempt == max_attempts - 1:
218
+ break
219
+
220
+ return None, (best_sample_rate, best_audio), None
221
+
222
+
223
+ inference_stream = partial(inference, streaming=True)
224
+
225
+ n_audios = 4
226
+
227
+ global_audio_list = []
228
+ global_error_list = []
229
+
230
+
231
+ def inference_wrapper(
232
+ text,
233
+ enable_reference_audio,
234
+ reference_audio,
235
+ reference_text,
236
+ max_new_tokens,
237
+ chunk_length,
238
+ top_p,
239
+ repetition_penalty,
240
+ temperature,
241
+ batch_infer_num,
242
+ if_load_asr_model,
243
+ ):
244
+ audios = []
245
+ errors = []
246
+
247
+ for _ in range(batch_infer_num):
248
+ result = inference_with_auto_rerank(
249
+ text,
250
+ enable_reference_audio,
251
+ reference_audio,
252
+ reference_text,
253
+ max_new_tokens,
254
+ chunk_length,
255
+ top_p,
256
+ repetition_penalty,
257
+ temperature,
258
+ if_load_asr_model,
259
+ )
260
+
261
+ _, audio_data, error_message = result
262
+
263
+ audios.append(
264
+ gr.Audio(value=audio_data if audio_data else None, visible=True),
265
+ )
266
+ errors.append(
267
+ gr.HTML(value=error_message if error_message else None, visible=True),
268
+ )
269
+
270
+ for _ in range(batch_infer_num, n_audios):
271
+ audios.append(
272
+ gr.Audio(value=None, visible=False),
273
+ )
274
+ errors.append(
275
+ gr.HTML(value=None, visible=False),
276
+ )
277
+
278
+ return None, *audios, *errors
279
+
280
+
281
+ def wav_chunk_header(sample_rate=44100, bit_depth=16, channels=1):
282
+ buffer = io.BytesIO()
283
+
284
+ with wave.open(buffer, "wb") as wav_file:
285
+ wav_file.setnchannels(channels)
286
+ wav_file.setsampwidth(bit_depth // 8)
287
+ wav_file.setframerate(sample_rate)
288
+
289
+ wav_header_bytes = buffer.getvalue()
290
+ buffer.close()
291
+ return wav_header_bytes
292
+
293
+
294
+ def normalize_text(user_input, use_normalization):
295
+ if use_normalization:
296
+ return ChnNormedText(raw_text=user_input).normalize()
297
+ else:
298
+ return user_input
299
+
300
+
301
+ asr_model = None
302
+
303
+
304
+ def change_if_load_asr_model(if_load):
305
+ global asr_model
306
+
307
+ if if_load:
308
+ gr.Warning("Loading faster whisper model...")
309
+ if asr_model is None:
310
+ asr_model = load_model()
311
+ return gr.Checkbox(label="Unload faster whisper model", value=if_load)
312
+
313
+ if if_load is False:
314
+ gr.Warning("Unloading faster whisper model...")
315
+ del asr_model
316
+ asr_model = None
317
+ if torch.cuda.is_available():
318
+ torch.cuda.empty_cache()
319
+ gc.collect()
320
+ return gr.Checkbox(label="Load faster whisper model", value=if_load)
321
+
322
+
323
+ def change_if_auto_label(if_load, if_auto_label, enable_ref, ref_audio, ref_text):
324
+ if if_load and asr_model is not None:
325
+ if (
326
+ if_auto_label
327
+ and enable_ref
328
+ and ref_audio is not None
329
+ and ref_text.strip() == ""
330
+ ):
331
+ data, sample_rate = librosa.load(ref_audio)
332
+ res = batch_asr(asr_model, [data], sample_rate)[0]
333
+ ref_text = res["text"]
334
+ else:
335
+ gr.Warning("Whisper model not loaded!")
336
+
337
+ return gr.Textbox(value=ref_text)
338
+
339
+
340
+ def build_app():
341
+ with gr.Blocks(theme=gr.themes.Base()) as app:
342
+ gr.Markdown(HEADER_MD)
343
+
344
+ # Use light theme by default
345
+ app.load(
346
+ None,
347
+ None,
348
+ js="() => {const params = new URLSearchParams(window.location.search);if (!params.has('__theme')) {params.set('__theme', '%s');window.location.search = params.toString();}}"
349
+ % args.theme,
350
+ )
351
+
352
+ # Inference
353
+ with gr.Row():
354
+ with gr.Column(scale=3):
355
+ text = gr.Textbox(
356
+ label=i18n("Input Text"), placeholder=TEXTBOX_PLACEHOLDER, lines=10
357
+ )
358
+ refined_text = gr.Textbox(
359
+ label=i18n("Realtime Transform Text"),
360
+ placeholder=i18n(
361
+ "Normalization Result Preview (Currently Only Chinese)"
362
+ ),
363
+ lines=5,
364
+ interactive=False,
365
+ )
366
+
367
+ with gr.Row():
368
+ if_refine_text = gr.Checkbox(
369
+ label=i18n("Text Normalization"),
370
+ value=True,
371
+ scale=1,
372
+ )
373
+
374
+ if_load_asr_model = gr.Checkbox(
375
+ label=i18n("Load / Unload ASR model for auto-reranking"),
376
+ value=False,
377
+ scale=3,
378
+ )
379
+
380
+ with gr.Row():
381
+ with gr.Tab(label=i18n("Advanced Config")):
382
+ chunk_length = gr.Slider(
383
+ label=i18n("Iterative Prompt Length, 0 means off"),
384
+ minimum=0,
385
+ maximum=500,
386
+ value=100,
387
+ step=8,
388
+ )
389
+
390
+ max_new_tokens = gr.Slider(
391
+ label=i18n("Maximum tokens per batch, 0 means no limit"),
392
+ minimum=0,
393
+ maximum=2048,
394
+ value=1024, # 0 means no limit
395
+ step=8,
396
+ )
397
+
398
+ top_p = gr.Slider(
399
+ label="Top-P",
400
+ minimum=0.6,
401
+ maximum=0.9,
402
+ value=0.7,
403
+ step=0.01,
404
+ )
405
+
406
+ repetition_penalty = gr.Slider(
407
+ label=i18n("Repetition Penalty"),
408
+ minimum=1,
409
+ maximum=1.5,
410
+ value=1.2,
411
+ step=0.01,
412
+ )
413
+
414
+ temperature = gr.Slider(
415
+ label="Temperature",
416
+ minimum=0.6,
417
+ maximum=0.9,
418
+ value=0.7,
419
+ step=0.01,
420
+ )
421
+
422
+ with gr.Tab(label=i18n("Reference Audio")):
423
+ gr.Markdown(
424
+ i18n(
425
+ "5 to 10 seconds of reference audio, useful for specifying speaker."
426
+ )
427
+ )
428
+
429
+ enable_reference_audio = gr.Checkbox(
430
+ label=i18n("Enable Reference Audio"),
431
+ )
432
+ reference_audio = gr.Audio(
433
+ label=i18n("Reference Audio"),
434
+ type="filepath",
435
+ )
436
+ with gr.Row():
437
+ if_auto_label = gr.Checkbox(
438
+ label=i18n("Auto Labeling"),
439
+ min_width=100,
440
+ scale=0,
441
+ value=False,
442
+ )
443
+ reference_text = gr.Textbox(
444
+ label=i18n("Reference Text"),
445
+ lines=1,
446
+ placeholder="在一无所知中,梦里的一天结束了,一个新的「轮回」便会开始。",
447
+ value="",
448
+ )
449
+ with gr.Tab(label=i18n("Batch Inference")):
450
+ batch_infer_num = gr.Slider(
451
+ label="Batch infer nums",
452
+ minimum=1,
453
+ maximum=n_audios,
454
+ step=1,
455
+ value=1,
456
+ )
457
+
458
+ with gr.Column(scale=3):
459
+ for _ in range(n_audios):
460
+ with gr.Row():
461
+ error = gr.HTML(
462
+ label=i18n("Error Message"),
463
+ visible=True if _ == 0 else False,
464
+ )
465
+ global_error_list.append(error)
466
+ with gr.Row():
467
+ audio = gr.Audio(
468
+ label=i18n("Generated Audio"),
469
+ type="numpy",
470
+ interactive=False,
471
+ visible=True if _ == 0 else False,
472
+ )
473
+ global_audio_list.append(audio)
474
+
475
+ with gr.Row():
476
+ stream_audio = gr.Audio(
477
+ label=i18n("Streaming Audio"),
478
+ streaming=True,
479
+ autoplay=True,
480
+ interactive=False,
481
+ show_download_button=True,
482
+ )
483
+ with gr.Row():
484
+ with gr.Column(scale=3):
485
+ generate = gr.Button(
486
+ value="\U0001F3A7 " + i18n("Generate"), variant="primary"
487
+ )
488
+ generate_stream = gr.Button(
489
+ value="\U0001F3A7 " + i18n("Streaming Generate"),
490
+ variant="primary",
491
+ )
492
+
493
+ text.input(
494
+ fn=normalize_text, inputs=[text, if_refine_text], outputs=[refined_text]
495
+ )
496
+
497
+ if_load_asr_model.change(
498
+ fn=change_if_load_asr_model,
499
+ inputs=[if_load_asr_model],
500
+ outputs=[if_load_asr_model],
501
+ )
502
+
503
+ if_auto_label.change(
504
+ fn=lambda: gr.Textbox(value=""),
505
+ inputs=[],
506
+ outputs=[reference_text],
507
+ ).then(
508
+ fn=change_if_auto_label,
509
+ inputs=[
510
+ if_load_asr_model,
511
+ if_auto_label,
512
+ enable_reference_audio,
513
+ reference_audio,
514
+ reference_text,
515
+ ],
516
+ outputs=[reference_text],
517
+ )
518
+
519
+ # # Submit
520
+ generate.click(
521
+ inference_wrapper,
522
+ [
523
+ refined_text,
524
+ enable_reference_audio,
525
+ reference_audio,
526
+ reference_text,
527
+ max_new_tokens,
528
+ chunk_length,
529
+ top_p,
530
+ repetition_penalty,
531
+ temperature,
532
+ batch_infer_num,
533
+ if_load_asr_model,
534
+ ],
535
+ [stream_audio, *global_audio_list, *global_error_list],
536
+ concurrency_limit=1,
537
+ )
538
+
539
+ generate_stream.click(
540
+ inference_stream,
541
+ [
542
+ refined_text,
543
+ enable_reference_audio,
544
+ reference_audio,
545
+ reference_text,
546
+ max_new_tokens,
547
+ chunk_length,
548
+ top_p,
549
+ repetition_penalty,
550
+ temperature,
551
+ ],
552
+ [stream_audio, global_audio_list[0], global_error_list[0]],
553
+ concurrency_limit=10,
554
+ )
555
+ return app
556
+
557
+
558
+ def parse_args():
559
+ parser = ArgumentParser()
560
+ parser.add_argument(
561
+ "--llama-checkpoint-path",
562
+ type=Path,
563
+ default="checkpoints/fish-speech-1.2-sft",
564
+ )
565
+ parser.add_argument(
566
+ "--decoder-checkpoint-path",
567
+ type=Path,
568
+ default="checkpoints/fish-speech-1.2-sft/firefly-gan-vq-fsq-4x1024-42hz-generator.pth",
569
+ )
570
+ parser.add_argument("--decoder-config-name", type=str, default="firefly_gan_vq")
571
+ parser.add_argument("--device", type=str, default="cuda")
572
+ parser.add_argument("--half", action="store_true")
573
+ parser.add_argument("--compile", action="store_true")
574
+ parser.add_argument("--max-gradio-length", type=int, default=0)
575
+ parser.add_argument("--theme", type=str, default="light")
576
+
577
+ return parser.parse_args()
578
+
579
+
580
+ if __name__ == "__main__":
581
+ args = parse_args()
582
+ args.precision = torch.half if args.half else torch.bfloat16
583
+
584
+ logger.info("Loading Llama model...")
585
+ llama_queue = launch_thread_safe_queue(
586
+ checkpoint_path=args.llama_checkpoint_path,
587
+ device=args.device,
588
+ precision=args.precision,
589
+ compile=args.compile,
590
+ )
591
+ logger.info("Llama model loaded, loading VQ-GAN model...")
592
+
593
+ decoder_model = load_decoder_model(
594
+ config_name=args.decoder_config_name,
595
+ checkpoint_path=args.decoder_checkpoint_path,
596
+ device=args.device,
597
+ )
598
+
599
+ logger.info("Decoder model loaded, warming up...")
600
+
601
+ # Dry run to check if the model is loaded correctly and avoid the first-time latency
602
+ list(
603
+ inference(
604
+ text="Hello, world!",
605
+ enable_reference_audio=False,
606
+ reference_audio=None,
607
+ reference_text="",
608
+ max_new_tokens=0,
609
+ chunk_length=100,
610
+ top_p=0.7,
611
+ repetition_penalty=1.2,
612
+ temperature=0.7,
613
+ )
614
+ )
615
+
616
+ logger.info("Warming up done, launching the web UI...")
617
+
618
+ app = build_app()
619
+ app.launch(show_api=True)