Skip to content

Instantly share code, notes, and snippets.

@ivanfioravanti
Last active December 18, 2024 17:07
Show Gist options
  • Save ivanfioravanti/41edf3e33761e2701f6dffa3bb7324f9 to your computer and use it in GitHub Desktop.
Save ivanfioravanti/41edf3e33761e2701f6dffa3bb7324f9 to your computer and use it in GitHub Desktop.
mlx-whisper real time audio
# 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.")
@simonw
Copy link

simonw commented Nov 3, 2024

Suggestion: you can use an inline script dependencies block and then run it with uv run - see my fork https://gist.github.com/simonw/57f9c15bbd9d484f762058f83412aefb

@skeetmtp
Copy link

skeetmtp commented Nov 3, 2024

seems this is a prerequisite:
brew install portaudio

@Nicole3Thompson
Copy link

Nicole3Thompson commented Nov 14, 2024

Thank you so much for the link. I was searching for the mlx-whisper real time audio online for my buddy and while searching for it online, I am glad I found your where I found my answer. I will surely share your post with my friend and I am sure he will be so happy. My best friend recommended this https://academized.com/ platform when I was overwhelmed with assignments last semester. At first, I was sceptical, but I decided to give it a shot, and I’m so glad I did! Their work is detailed, accurate, and perfectly tailored to my requirements. From essays to coursework, they have helped me in so many ways. If you’re a student in need, this service is definitely worth checking out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment