diff --git a/abogen/conversion.py b/abogen/conversion.py index 1d3d645..7be5fc8 100644 --- a/abogen/conversion.py +++ b/abogen/conversion.py @@ -23,6 +23,7 @@ import abogen.hf_tracker as hf_tracker import static_ffmpeg import threading # for efficient waiting import subprocess +import platform def get_sample_voice_text(lang_code): @@ -310,8 +311,15 @@ class ConversionThread(QThread): self.log_updated.emit("\nInitializing TTS pipeline...") - # Set device based on use_gpu setting - device = "cuda" if self.use_gpu else "cpu" + # Set device based on use_gpu setting and platform + if self.use_gpu: + if platform.system() == "Darwin" and platform.processor() == "arm": + device = "mps" # Use MPS for Apple Silicon + else: + device = "cuda" # Use CUDA for other platforms + else: + device = "cpu" + tts = self.KPipeline( lang_code=self.lang_code, repo_id="hexgrad/Kokoro-82M", device=device ) @@ -1319,7 +1327,16 @@ class VoicePreviewThread(QThread): # Generate the preview and save to cache try: - device = "cuda" if self.use_gpu else "cpu" + + # Set device based on use_gpu setting and platform + if self.use_gpu: + if platform.system() == "Darwin" and platform.processor() == "arm": + device = "mps" # Use MPS for Apple Silicon + else: + device = "cuda" # Use CUDA for other platforms + else: + device = "cpu" + tts = self.kpipeline_class( lang_code=self.lang_code, repo_id="hexgrad/Kokoro-82M", device=device ) diff --git a/abogen/utils.py b/abogen/utils.py index 27c8833..b08bd64 100644 --- a/abogen/utils.py +++ b/abogen/utils.py @@ -239,13 +239,23 @@ def calculate_text_length(text): def get_gpu_acceleration(enabled): try: import torch - from torch.cuda import is_available + from torch.cuda import is_available as cuda_available if not enabled: - return "CUDA GPU available but using CPU.", False - if is_available(): + return "GPU available but using CPU.", False + + # Check for Apple Silicon MPS + if platform.system() == "Darwin" and platform.processor() == "arm": + if torch.backends.mps.is_available(): + return "MPS GPU available and enabled.", True + else: + return "MPS GPU not available on Apple Silicon. Using CPU.", False + + # Check for CUDA + if cuda_available(): return "CUDA GPU available and enabled.", True - # Gather more diagnostic info if CUDA is not available + + # Gather CUDA diagnostic info if not available try: cuda_devices = torch.cuda.device_count() cuda_error = ( @@ -257,7 +267,7 @@ def get_gpu_acceleration(enabled): cuda_error = str(e) return f"CUDA GPU is not available. Using CPU. ({cuda_error})", False except Exception as e: - return f"Error checking CUDA GPU: {e}", False + return f"Error checking GPU: {e}", False def prevent_sleep_start():