2026-05-26 01:35:10 +08:00
|
|
|
from transformers import AutoProcessor, CohereAsrForConditionalGeneration
|
|
|
|
|
from transformers.audio_utils import load_audio
|
|
|
|
|
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
# Load model
|
|
|
|
|
print("Loading model...")
|
|
|
|
|
processor = AutoProcessor.from_pretrained("CohereLabs/cohere-transcribe-03-2026")
|
|
|
|
|
model = CohereAsrForConditionalGeneration.from_pretrained(
|
|
|
|
|
"CohereLabs/cohere-transcribe-03-2026",
|
|
|
|
|
device_map="auto"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-26 01:49:52 +08:00
|
|
|
def transcribe_audio(audio, language="en"):
|
|
|
|
|
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", language=language)
|
|
|
|
|
inputs.to(model.device, dtype=model.dtype)
|
|
|
|
|
|
|
|
|
|
outputs = model.generate(**inputs, max_new_tokens=256)
|
|
|
|
|
text = processor.decode(outputs, skip_special_tokens=True)
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
# Use demo audio file from Hugging Face
|
|
|
|
|
print("Loading demo audio...")
|
2026-05-26 01:35:10 +08:00
|
|
|
audio_file = hf_hub_download(
|
|
|
|
|
repo_id="CohereLabs/cohere-transcribe-03-2026",
|
|
|
|
|
filename="demo/voxpopuli_test_en_demo.wav",
|
|
|
|
|
)
|
|
|
|
|
audio = load_audio(audio_file, sampling_rate=16000)
|
|
|
|
|
|
|
|
|
|
print("Transcribing...")
|
2026-05-26 01:49:52 +08:00
|
|
|
text = transcribe_audio(audio)
|
2026-05-26 01:35:10 +08:00
|
|
|
print(f"\nTranscription:\n{text}\n")
|