File size: 1,021 Bytes
2859a48 6270f23 d501976 2859a48 d501976 2859a48 d501976 2859a48 9bf6d10 2859a48 d501976 2859a48 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import librosa
from transformers import Wav2Vec2ForCTC, AutoProcessor
import torch
ASR_SAMPLING_RATE = 16_000
MODEL_ID = "facebook/wav2vec2-large-960h-lv60-self"
processor = AutoProcessor.from_pretrained(MODEL_ID)
model = Wav2Vec2ForCTC.from_pretrained(MODEL_ID)
def transcribe(audio):
if audio is None:
return "ERROR: You have to either use the microphone or upload an audio file"
audio_samples = librosa.load(audio, sr=ASR_SAMPLING_RATE, mono=True)[0]
inputs = processor(audio_samples, sampling_rate=ASR_SAMPLING_RATE, return_tensors="pt")
# Set language ID for Faroese
language_id = 'fao' # ISO 639-3 code for Faroese
processor.tokenizer.set_lang(language_id)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
inputs = inputs.to(device)
with torch.no_grad():
outputs = model(**inputs).logits
ids = torch.argmax(outputs, dim=-1)[0]
transcription = processor.decode(ids)
return transcription
|