run_webui.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 tools.inference_engine import TTSInferenceEngine
  9. from tools.llama.generate import launch_thread_safe_queue
  10. from tools.schema import ServeTTSRequest
  11. from tools.vqgan.inference import load_model as load_decoder_model
  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/fish-speech-1.5",
  22. )
  23. parser.add_argument(
  24. "--decoder-checkpoint-path",
  25. type=Path,
  26. default="checkpoints/fish-speech-1.5/firefly-gan-vq-fsq-8x1024-21hz-generator.pth",
  27. )
  28. parser.add_argument("--decoder-config-name", type=str, default="firefly_gan_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 is available
  39. if torch.backends.mps.is_available():
  40. args.device = "mps"
  41. logger.info("mps is available, running on mps.")
  42. # Check if CUDA is available
  43. if not torch.cuda.is_available():
  44. logger.info("CUDA is not available, running on CPU.")
  45. args.device = "cpu"
  46. logger.info("Loading Llama model...")
  47. llama_queue = launch_thread_safe_queue(
  48. checkpoint_path=args.llama_checkpoint_path,
  49. device=args.device,
  50. precision=args.precision,
  51. compile=args.compile,
  52. )
  53. logger.info("Loading VQ-GAN model...")
  54. decoder_model = load_decoder_model(
  55. config_name=args.decoder_config_name,
  56. checkpoint_path=args.decoder_checkpoint_path,
  57. device=args.device,
  58. )
  59. logger.info("Decoder model loaded, warming up...")
  60. # Create the inference engine
  61. inference_engine = TTSInferenceEngine(
  62. llama_queue=llama_queue,
  63. decoder_model=decoder_model,
  64. compile=args.compile,
  65. precision=args.precision,
  66. )
  67. # Dry run to check if the model is loaded correctly and avoid the first-time latency
  68. list(
  69. inference_engine.inference(
  70. ServeTTSRequest(
  71. text="Hello world.",
  72. references=[],
  73. reference_id=None,
  74. max_new_tokens=1024,
  75. chunk_length=200,
  76. top_p=0.7,
  77. repetition_penalty=1.5,
  78. temperature=0.7,
  79. format="wav",
  80. )
  81. )
  82. )
  83. logger.info("Warming up done, launching the web UI...")
  84. # Get the inference function with the immutable arguments
  85. inference_fct = get_inference_wrapper(inference_engine)
  86. app = build_app(inference_fct, args.theme)
  87. app.launch(show_api=True)