### Install lightning-whisper-mlx Source: https://github.com/mustafaaljadery/lightning-whisper-mlx/blob/main/index.html Installs the lightning-whisper-mlx library using pip. This is the primary method for adding the library to your Python environment, enabling its use in your projects. ```bash pip install lightning-whisper-mlx ``` -------------------------------- ### Fastest English Transcription Setup with LightningWhisperMLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Quickly set up and transcribe English audio using the LightningWhisperMLX class. This method is optimized for speed and ease of use, taking an audio file path as input and printing the transcribed text. It requires the `lightning_whisper_mlx` library. ```python from lightning_whisper_mlx import LightningWhisperMLX whisper = LightningWhisperMLX( model="distil-medium.en", batch_size=12, quant="4bit" ) result = whisper.transcribe(audio_path="/english_audio.mp3") print(result['text']) ``` -------------------------------- ### Audio Loading and Preprocessing Utilities in Python Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Demonstrates how to load, preprocess, and compute mel spectrograms from audio files using utilities from `lightning_whisper_mlx.audio`. It includes functions for resampling, padding/trimming, and calculating log-mel spectrograms, compatible with `mlx.core` arrays. Requires `lightning_whisper_mlx.audio` and `mlx.core`. ```python from lightning_whisper_mlx.audio import ( load_audio, log_mel_spectrogram, pad_or_trim, SAMPLE_RATE, N_SAMPLES ) import mlx.core as mx # Load audio file (resampled to 16kHz mono) audio = load_audio("/path/to/audio.mp3", sr=SAMPLE_RATE) print(f"Audio shape: {audio.shape}") # Output: Audio shape: (480000,) # 30 seconds at 16kHz # Pad or trim to 30 seconds audio_padded = pad_or_trim(audio, length=N_SAMPLES) # Compute log-mel spectrogram mel_spec = log_mel_spectrogram( audio="/path/to/audio.mp3", n_mels=80, padding=0 ) print(f"Mel spectrogram shape: {mel_spec.shape}") # Output: Mel spectrogram shape: (80, 3000) # 80 mel bins, 3000 frames # Process numpy array directly import numpy as np audio_np = np.random.randn(160000).astype(np.float32) # 10 seconds mel_from_array = log_mel_spectrogram(audio_np, n_mels=80) ``` -------------------------------- ### Model Loading and Configuration in Python with MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Shows how to load pre-trained Whisper models using `load_model` from `lightning_whisper_mlx.load_models` with specified data types (e.g., `float16`, `float32`) and from both Hugging Face repositories and local paths. It also demonstrates accessing model properties like dimensions and language support. Requires `lightning_whisper_mlx.load_models` and `mlx.core`. ```python from lightning_whisper_mlx.load_models import load_model import mlx.core as mx # Load model with specific dtype model_fp16 = load_model( path_or_hf_repo="mlx-community/whisper-tiny-mlx", dtype=mx.float16 ) model_fp32 = load_model( path_or_hf_repo="mlx-community/whisper-base-mlx", dtype=mx.float32 ) # Access model properties print(f"Model dimensions: {model_fp16.dims}") print(f"Is multilingual: {model_fp16.is_multilingual}") print(f"Number of languages: {model_fp16.num_languages}") # Load from local path local_model = load_model( path_or_hf_repo="./mlx_models/distil-medium.en", dtype=mx.float16 ) ``` -------------------------------- ### Model Initialization with Quantization in Lightning Whisper MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Demonstrates initializing the LightningWhisperMLX model with different quantization levels (4-bit, 8-bit, or None) to balance inference speed and memory usage. Quantization reduces model precision, enabling faster processing on resource-constrained devices. ```python from lightning_whisper_mlx import LightningWhisperMLX # 4-bit quantized model for maximum speed whisper_4bit = LightningWhisperMLX( model="large-v3", batch_size=6, # Lower batch size for large models quant="4bit" ) # 8-bit quantized model for balanced speed and accuracy whisper_8bit = LightningWhisperMLX( model="base", batch_size=16, # Higher batch size for smaller models quant="8bit" ) # No quantization for maximum accuracy whisper_full = LightningWhisperMLX( model="tiny", batch_size=24, # Highest batch size for smallest model quant=None ) result = whisper_4bit.transcribe(audio_path="/audio.wav") print(result['text']) ``` -------------------------------- ### Listing Available Models and Quantization Options in Lightning Whisper MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Provides lists of supported model identifiers and quantization options for initializing the LightningWhisperMLX. This helps users choose the appropriate model size and precision for their specific transcription needs, balancing speed, accuracy, and resource consumption. ```python from lightning_whisper_mlx import LightningWhisperMLX # Standard models (multilingual) models = [ "tiny", # Fastest, least accurate "small", # Fast, moderate accuracy "base", # Balanced speed and accuracy "medium", # High accuracy, slower "large", # Highest accuracy "large-v2", # OpenAI's v2 release "large-v3" # Latest OpenAI release ] # Distilled models (English only, faster) distilled_models = [ "distil-small.en", # Distilled small "distil-medium.en", # Distilled medium "distil-large-v2", # Distilled large v2 "distil-large-v3" # Distilled large v3 ] # Quantization options quantization = [None, "4bit", "8bit"] ``` -------------------------------- ### Transcribe Audio with LightningWhisperMLX in Python Source: https://github.com/mustafaaljadery/lightning-whisper-mlx/blob/main/README.md This Python code demonstrates how to initialize the LightningWhisperMLX model and transcribe an audio file. It allows specifying the model, batch size, and quantization level. The output is the transcribed text from the audio. ```python from lightning_whisper_mlx import LightningWhisperMLX whisper = LightningWhisperMLX(model="distil-medium.en", batch_size=12, quant=None) text = whisper.transcribe(audio_path="/audio.mp3")['text'] print(text) ``` -------------------------------- ### Transcribe Audio with Lightning Whisper MLX (Python) Source: https://github.com/mustafaaljadery/lightning-whisper-mlx/blob/main/index.html Demonstrates how to use the LightningWhisperMLX class to transcribe an audio file. It shows model initialization with specified parameters and the transcription process, outputting the transcribed text. ```python from lightning_whisper_mlx import LightningWhisperMLX whisper = LightningWhisperMLX(model="base", batch_size=12, quant=None) text = whisper.transcribe(audio_path="/audio.mp3")['text'] print(text) ``` -------------------------------- ### Basic Audio Transcription with Lightning Whisper MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Initializes the LightningWhisperMLX model with a specified configuration and transcribes an audio file. The model is automatically downloaded from Hugging Face Hub on its first use. This snippet demonstrates the fundamental usage for converting speech to text. ```python from lightning_whisper_mlx import LightningWhisperMLX # Initialize with standard model whisper = LightningWhisperMLX(model="distil-medium.en", batch_size=12, quant=None) # Transcribe audio file result = whisper.transcribe(audio_path="/path/to/audio.mp3") text = result['text'] print(text) # Output: "This is the transcribed text from the audio file." ``` -------------------------------- ### Batch Size Optimization for Different Models in Lightning Whisper MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Illustrates how to adjust the `batch_size` parameter when initializing the LightningWhisperMLX model. Optimal batch sizes vary depending on the model size and available unified memory, impacting overall throughput. This snippet also shows processing multiple audio files sequentially. ```python from lightning_whisper_mlx import LightningWhisperMLX # Small models can handle larger batch sizes tiny_whisper = LightningWhisperMLX(model="tiny", batch_size=24, quant=None) # Medium models need moderate batch sizes medium_whisper = LightningWhisperMLX(model="distil-medium.en", batch_size=12, quant=None) # Large models require smaller batch sizes large_whisper = LightningWhisperMLX(model="large-v3", batch_size=6, quant="4bit") # Process multiple audio files audio_files = ["file1.mp3", "file2.mp3", "file3.mp3"] for audio_file in audio_files: result = medium_whisper.transcribe(audio_path=audio_file) print(f"{audio_file}: {result['text']}") ``` -------------------------------- ### Advanced Audio Transcription with Custom Parameters in Python Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Utilize the low-level `transcribe_audio` function for fine-grained control over the transcription process. This function allows customization of parameters such as fallback temperatures, compression ratio thresholds, and initial prompts. It requires the `lightning_whisper_mlx.transcribe` module and an audio file path. ```python from lightning_whisper_mlx.transcribe import transcribe_audio # Advanced transcription with custom parameters result = transcribe_audio( audio="/path/to/audio.mp3", path_or_hf_repo="mlx-community/whisper-medium-mlx", verbose=False, temperature=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0), # Fallback temperatures compression_ratio_threshold=2.4, logprob_threshold=-1.0, no_speech_threshold=0.6, condition_on_previous_text=True, initial_prompt="Custom vocabulary: MLX, Whisper, Apple Silicon", word_timestamps=False, batch_size=12, language="en", task="transcribe" # or "translate" for translation to English ) text = result['text'] segments = result['segments'] language = result['language'] print(f"Transcription: {text}") print(f"Detected language: {language}") print(f"Number of segments: {len(segments)}") ``` -------------------------------- ### Error Handling and Validation for Audio Transcription in Python Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Provides a robust Python function `transcribe_with_validation` that includes error handling for file existence, model name validity, and transcription success. It returns a dictionary indicating success or failure with details. This function utilizes `LightningWhisperMLX` and requires standard Python libraries like `os`. ```python from lightning_whisper_mlx import LightningWhisperMLX import os def transcribe_with_validation(audio_path, model_name="distil-medium.en"): """Transcribe audio with comprehensive error handling.""" # Validate audio file exists if not os.path.exists(audio_path): raise FileNotFoundError(f"Audio file not found: {audio_path}") # Validate model name valid_models = [ "tiny", "small", "distil-small.en", "base", "medium", "distil-medium.en", "large", "large-v2", "distil-large-v2", "large-v3", "distil-large-v3" ] if model_name not in valid_models: raise ValueError(f"Invalid model: {model_name}. Must be one of {valid_models}") try: # Initialize model whisper = LightningWhisperMLX( model=model_name, batch_size=12, quant=None ) # Transcribe result = whisper.transcribe(audio_path=audio_path) # Validate result if not result or 'text' not in result: raise RuntimeError("Transcription failed: empty result") return { 'success': True, 'text': result['text'], 'language': result.get('language', 'unknown'), 'segments': result.get('segments', []) } except Exception as e: return { 'success': False, 'error': str(e), 'text': None } # Usage result = transcribe_with_validation("/path/to/audio.mp3", model_name="tiny") if result['success']: print(f"Transcription: {result['text']}") else: print(f"Error: {result['error']}") ``` -------------------------------- ### Language-Specific Transcription with Lightning Whisper MLX Source: https://context7.com/mustafaaljadery/lightning-whisper-mlx/llms.txt Performs audio transcription while explicitly specifying the source language. This can improve accuracy and performance when the language is known beforehand. The output includes the transcribed text, detailed segments, and the detected language. ```python from lightning_whisper_mlx import LightningWhisperMLX # Initialize model whisper = LightningWhisperMLX(model="medium", batch_size=8, quant=None) # Transcribe with language specification result = whisper.transcribe( audio_path="/path/to/spanish_audio.mp3", language="es" ) # Access transcription details text = result['text'] segments = result['segments'] detected_language = result['language'] print(f"Language: {detected_language}") print(f"Transcription: {text}") # Output: # Language: es # Transcription: "Hola, este es un ejemplo de transcripción en español." ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.