Skip to content

Instantly share code, notes, and snippets.

@kavyamanohar
Created October 7, 2026 11:34
Show Gist options
  • Select an option

  • Save kavyamanohar/bd1c1852c1a8d1232367fa746973d848 to your computer and use it in GitHub Desktop.

Select an option

Save kavyamanohar/bd1c1852c1a8d1232367fa746973d848 to your computer and use it in GitHub Desktop.
Simple Malayalam speech-to-text with Koyal. Any audio format, long recordings handled. uv run transcribe.py audio.mp3
# /// script
# requires-python = ">=3.10,<3.13"
# dependencies = [
# "nemo_toolkit[asr]",
# "silero-vad",
# "av",
# "transformers>=4.48",
# ]
# ///
"""
Transcribe Malayalam audio with Koyal (adalat-ai/koyal-ml-120m-1.0).
Usage:
uv run transcribe.py my_recording.mp3
uv run transcribe.py interview.m4a lecture.wav # several files
uv run transcribe.py my_audio_folder/ # every audio file in a folder
The transcript is printed and also saved as a .txt file next to each audio file.
"""
import sys
from pathlib import Path
import av
import numpy as np
import torch
MODEL = "adalat-ai/koyal-ml-120m-1.0"
SAMPLE_RATE = 16000 # Koyal expects 16 kHz mono audio
MAX_CHUNK_SECONDS = 30 # longer audio is split into pieces of at most this length
AUDIO_EXTENSIONS = {".wav", ".mp3", ".m4a", ".ogg", ".opus", ".flac", ".aac", ".webm", ".mp4"}
def load_audio(path):
"""Read any audio (or video) file and convert it to 16 kHz mono."""
resampler = av.AudioResampler(format="flt", layout="mono", rate=SAMPLE_RATE)
pieces = []
with av.open(str(path)) as container:
for frame in container.decode(audio=0):
pieces += [f.to_ndarray()[0] for f in resampler.resample(frame)]
pieces += [f.to_ndarray()[0] for f in resampler.resample(None)] # flush
return np.concatenate(pieces)
def split_at_pauses(audio):
"""Cut long audio into chunks of up to MAX_CHUNK_SECONDS, cutting only where nobody is speaking."""
from silero_vad import get_speech_timestamps, load_silero_vad
speech = get_speech_timestamps(
torch.from_numpy(audio), load_silero_vad(), sampling_rate=SAMPLE_RATE,
max_speech_duration_s=MAX_CHUNK_SECONDS,
min_silence_duration_ms=500, # only treat pauses of half a second or more as a break
speech_pad_ms=200, # keep a little silence around speech so no word gets clipped
)
# Glue the speech pieces together (dropping the silences) until a chunk would get too long.
chunks = [[]]
for seg in speech:
piece = audio[seg["start"]:seg["end"]]
if sum(map(len, chunks[-1])) + len(piece) > MAX_CHUNK_SECONDS * SAMPLE_RATE:
chunks.append([])
chunks[-1].append(piece)
return [np.concatenate(c) for c in chunks if c]
def find_audio_files(args):
for arg in map(Path, args):
if arg.is_dir():
yield from sorted(p for p in arg.iterdir() if p.suffix.lower() in AUDIO_EXTENSIONS)
else:
yield arg
def main():
files = list(find_audio_files(sys.argv[1:]))
if not files:
sys.exit(__doc__)
import nemo.collections.asr as nemo_asr
from nemo.utils import logging
logging.setLevel(logging.ERROR) # hide NeMo's very chatty logs
print(f"Loading {MODEL} (downloaded once, ~0.5 GB) ...")
device = "cuda" if torch.cuda.is_available() else "cpu"
model = nemo_asr.models.ASRModel.from_pretrained(MODEL, map_location=device).eval()
for path in files:
audio = load_audio(path)
minutes = len(audio) / SAMPLE_RATE / 60
chunks = [audio] if minutes * 60 <= MAX_CHUNK_SECONDS else split_at_pauses(audio)
print(f"\n{path} ({minutes:.1f} min, {len(chunks)} chunk(s))")
if not chunks:
print("No speech found, skipping.")
continue
results = model.transcribe(chunks, batch_size=4, verbose=False)
text = " ".join(r.text.strip() for r in results if r.text.strip())
print(text)
out = Path(path).with_suffix(".txt")
out.write_text(text + "\n", encoding="utf-8")
print(f"-> saved to {out}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment