lg language not recognized
#1
by
JLouisBiz - opened
It works pretty well.
Language lg is not recognized, it works without using language option.
Script I am using:
#!/home/data1/protected/venv/bin/python
import sys
from transformers import pipeline
from pydub import AudioSegment
from pathlib import Path
import io
import torch
def transcribe_audio_in_memory(audio_path, pipe, chunk_length_ms=30_000):
"""
Transcribe audio (any length) using Whisper Luganda model entirely in memory.
Splits into chunks internally to avoid long-form issues.
Returns full transcription as string.
"""
audio = AudioSegment.from_file(audio_path)
transcript = ""
# Process audio in chunks
for i in range(0, len(audio), chunk_length_ms):
chunk = audio[i:i+chunk_length_ms]
# Export chunk to a bytes buffer
buf = io.BytesIO()
chunk.export(buf, format="wav")
buf.seek(0)
# Transcribe directly from bytes buffer
result = pipe(buf, generate_kwargs={"task": "transcribe"})
transcript += result["text"] + " "
return transcript.strip()
def save_transcription(audio_path, transcription):
"""
Save transcription to a .txt file using the same base name as the audio file.
"""
base_name = os.path.splitext(audio_path)[0]
txt_file = base_name + ".txt"
with open(txt_file, "w", encoding="utf-8") as f:
f.write(transcription)
print(f"Transcription saved to: {txt_file}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python transcribe_multiple_in_memory.py <folder_or_audio_file1> [<audio_file2> ...]")
sys.exit(1)
# Initialize Whisper pipeline once
pipe = pipeline(
"automatic-speech-recognition",
model="allandclive/whisper-small-luganda",
device=0 # change to -1 for CPU
)
input_paths = []
for arg in sys.argv[1:]:
p = Path(arg)
if p.is_file():
input_paths.append(p)
elif p.is_dir():
input_paths.extend(p.glob("*.ogg"))
input_paths.extend(p.glob("*.wav"))
input_paths.extend(p.glob("*.mp3"))
else:
print(f"Skipping invalid path: {arg}")
if not input_paths:
print("No valid audio files found.")
sys.exit(1)
# Process each file
for audio_file in input_paths:
print(f"Processing: {audio_file}")
transcription = transcribe_audio_in_memory(str(audio_file), pipe)
save_transcription(str(audio_file), transcription)