Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Created December 2, 2024 22:26
Show Gist options
  • Save jefftriplett/32e5fe261b1051a2a49f61a2da7a8b86 to your computer and use it in GitHub Desktop.
Save jefftriplett/32e5fe261b1051a2a49f61a2da7a8b86 to your computer and use it in GitHub Desktop.
Python + UV demo for using mlx_whisper.
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "mlx-whisper",
# "rich",
# "typer",
# ]
# ///
import json
import typer
import mlx_whisper
from enum import Enum
from pathlib import Path
from rich import print
class ModelChoices(str, Enum):
tiny = "tiny"
base = "base"
small = "small"
medium = "medium"
large = "mlx-community/whisper-large-v3-turbo"
turbo = "mlx-community/whisper-turbo"
def main(
input_paths: list[str],
model: ModelChoices = ModelChoices.turbo,
word_timestamps: bool = False
):
model_name = model.value.split("/")[-1]
for input_path in input_paths:
filename_stem = Path(input_path).stem
output_path = Path("captions", f"{filename_stem}.{model_name}.json")
print(f"{output_path=}")
if not output_path.exists():
result = mlx_whisper.transcribe(input_path, path_or_hf_repo=model.value, response_format="srt", word_timestamps=word_timestamps)
# print(result["text"])
output_path.write_text(json.dumps(result, indent=2))
if __name__ == "__main__":
typer.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment