Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onyedikachi-david/173283529c1c2bc75d19251cf58aa622 to your computer and use it in GitHub Desktop.
Save onyedikachi-david/173283529c1c2bc75d19251cf58aa622 to your computer and use it in GitHub Desktop.

Revisions

  1. @simonw simonw revised this gist Nov 3, 2024. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions mlx_whisper_realtime.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    # Required packages:
    # pip install SpeechRecognition mlx-whisper pyaudio
    # Note: This script requires Apple Silicon Mac for MLX Whisper
    # /// script
    # dependencies = [
    # "SpeechRecognition",
    # "mlx-whisper",
    # "pyaudio",
    # ]
    # ///

    import speech_recognition as sr
    import numpy as np
  2. @ivanfioravanti ivanfioravanti renamed this gist Nov 3, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @ivanfioravanti ivanfioravanti created this gist Nov 3, 2024.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # Required packages:
    # pip install SpeechRecognition mlx-whisper pyaudio
    # Note: This script requires Apple Silicon Mac for MLX Whisper

    import speech_recognition as sr
    import numpy as np
    import mlx_whisper

    r = sr.Recognizer()
    mic = sr.Microphone(sample_rate=16000)

    print("Listening...")

    try:
    with mic as source:
    r.adjust_for_ambient_noise(source)
    while True:
    audio = r.listen(source)
    # Convert audio to numpy array
    audio_data = np.frombuffer(audio.get_raw_data(), dtype=np.int16).astype(np.float32) / 32768.0

    # Process audio with Apple MLXWhisper model
    result = mlx_whisper.transcribe(audio_data, path_or_hf_repo="mlx-community/whisper-large-v3-turbo")["text"]

    # Print the transcribed text
    print(result)

    except KeyboardInterrupt:
    print("Stopped listening.")