run_webui.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. from argparse import ArgumentParser
  3. from pathlib import Path
  4. import pyrootutils
  5. import torch
  6. from loguru import logger
  7. pyrootutils.setup_root(__file__, indicator=".project-root", pythonpath=True)
  8. from fish_speech.inference_engine import TTSInferenceEngine
  9. from fish_speech.models.dac.inference import load_model as load_decoder_model
  10. from fish_speech.models.text2semantic.inference import launch_thread_safe_queue
  11. from fish_speech.utils.schema import ServeTTSRequest
  12. from tools.webui import build_app
  13. from tools.webui.inference import get_inference_wrapper
  14. # Make einx happy
  15. os.environ["EINX_FILTER_TRACEBACK"] = "false"
  16. def parse_args():
  17. parser = ArgumentParser()
  18. parser.add_argument(
  19. "--llama-checkpoint-path",
  20. type=Path,
  21. default="checkpoints/openaudio-s1-mini",
  22. )
  23. parser.add_argument(
  24. "--decoder-checkpoint-path",
  25. type=Path,
  26. default="checkpoints/openaudio-s1-mini/codec.pth",
  27. )
  28. parser.add_argument("--decoder-config-name", type=str, default="modded_dac_vq")
  29. parser.add_argument("--device", type=str, default="cuda")
  30. parser.add_argument("--half", action="store_true")
  31. parser.add_argument("--compile", action="store_true")
  32. parser.add_argument("--max-gradio-length", type=int, default=0)
  33. parser.add_argument("--theme", type=str, default="light")
  34. return parser.parse_args()
  35. if __name__ == "__main__":
  36. args = parse_args()
  37. args.precision = torch.half if args.half else torch.bfloat16
  38. # Check if MPS or CUDA is available
  39. if torch.backends.mps.is_available():
  40. args.device = "mps"
  41. logger.info("mps is available, running on mps.")
  42. elif not torch.cuda.is_available():
  43. logger.info("CUDA is not available, running on CPU.")
  44. args.device = "cpu"
  45. logger.info("Loading Llama model...")
  46. llama_queue = launch_thread_safe_queue(
  47. checkpoint_path=args.llama_checkpoint_path,
  48. device=args.device,
  49. precision=args.precision,
  50. compile=args.compile,
  51. )
  52. logger.info("Loading VQ-GAN model...")
  53. decoder_model = load_decoder_model(
  54. config_name=args.decoder_config_name,
  55. checkpoint_path=args.decoder_checkpoint_path,
  56. device=args.device,
  57. )
  58. logger.info("Decoder model loaded, warming up...")
  59. # Create the inference engine
  60. inference_engine = TTSInferenceEngine(
  61. llama_queue=llama_queue,
  62. decoder_model=decoder_model,
  63. compile=args.compile,
  64. precision=args.precision,
  65. )
  66. # Dry run to check if the model is loaded correctly and avoid the first-time latency
  67. list(
  68. inference_engine.inference(
  69. ServeTTSRequest(
  70. text="Hello world.",
  71. references=[],
  72. reference_id=None,
  73. max_new_tokens=1024,
  74. chunk_length=200,
  75. top_p=0.7,
  76. repetition_penalty=1.5,
  77. temperature=0.7,
  78. format="wav",
  79. )
  80. )
  81. )
  82. logger.info("Warming up done, launching the web UI...")
  83. # Get the inference function with the immutable arguments
  84. inference_fct = get_inference_wrapper(inference_engine)
  85. app = build_app(inference_fct, args.theme)
  86. app.launch(show_api=True)