run_webui.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 CUDA is available
  39. if not torch.cuda.is_available():
  40. logger.info("CUDA is not available, running on CPU.")
  41. args.device = "cpu"
  42. logger.info("Loading Llama model...")
  43. llama_queue = launch_thread_safe_queue(
  44. checkpoint_path=args.llama_checkpoint_path,
  45. device=args.device,
  46. precision=args.precision,
  47. compile=args.compile,
  48. )
  49. logger.info("Loading VQ-GAN model...")
  50. decoder_model = load_decoder_model(
  51. config_name=args.decoder_config_name,
  52. checkpoint_path=args.decoder_checkpoint_path,
  53. device=args.device,
  54. )
  55. logger.info("Decoder model loaded, warming up...")
  56. # Create the inference engine
  57. inference_engine = TTSInferenceEngine(
  58. llama_queue=llama_queue,
  59. decoder_model=decoder_model,
  60. compile=args.compile,
  61. precision=args.precision,
  62. )
  63. # Dry run to check if the model is loaded correctly and avoid the first-time latency
  64. list(
  65. inference_engine.inference(
  66. ServeTTSRequest(
  67. text="Hello world.",
  68. references=[],
  69. reference_id=None,
  70. max_new_tokens=0,
  71. chunk_length=200,
  72. top_p=0.7,
  73. repetition_penalty=1.5,
  74. temperature=0.7,
  75. format="wav",
  76. )
  77. )
  78. )
  79. logger.info("Warming up done, launching the web UI...")
  80. # Get the inference function with the immutable arguments
  81. inference_fct = get_inference_wrapper(inference_engine)
  82. app = build_app(inference_fct, args.theme)
  83. app.launch(show_api=True)