728x90
반응형
SMALL
허깅페이스 로그인
from huggingface_hub import login
login(token="hf_")
라이브러리 및 모델 로드
import torch
import librosa
import numpy as np
from scipy.io import wavfile
from IPython.display import Audio
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
# 오디오 파일 로드
file_name = 'test.wav'
Audio(file_name)
토큰화 (tokenize)
data = wavfile.read(file_name)
sampling_rate = data[0]
sound_data = data[1]
Facebook 모델은 16000Hz 샘플링 속도를 허용하므로 샘플링 속도를 16000Hz로 변환한다.
input_audio, _ = librosa.load(file_name, sr=16000)
디코딩
# retrieve logits
input_values = tokenizer(input_audio, return_tensors="pt").input_values
logits = model(input_values).logits
# take argmax and decode
predicted_ids = torch.argmax(logits, dim=-1)
transcription = tokenizer.batch_decode(predicted_ids)[0]
print(transcription)
ALICE WAS BEGINNING TO GET VERY TIRED OF SITTING BY HER SISTER ON THE BANK AND OF HAVING NOTHING TO DO ONCE OR TWICE SHE PEEPED INTO THE BOOK HER SISTER WAS READING BUT IT HAD NO PICTURES OR CONVERSATIONS IN IT AND WHAT'S THE USE OF A BOOK THOUGHT ALICE WITHOUT PICTURES OR CONVERSATION SO SHE WAS CONSIDERING IN HER OWN MIND AS WELL AS SHE COULD FOR THE HOT DAY MADE HER FEEL VERY SLEEPY AND STUPID WHETHER THE PLEASURE OF MAKING A DAISY CHAIN WOULD BE WORTH THE TROUBLE OF GETTING UP AND PICKING THE DAISIES WHEN SUDDENLY A WHITE RABBIT WITH PINK EYES RAN CLOSE BY HER THERE WAS NOTHING SO VERY REMARKABLE IN THAT NOR DID ALICE THINK IT SO VERY MUCH OUT OF THE WAY TO HEAR THE RABBIT SAY TO ITSELF OH DEAR OH DEAR I SHALL BE LATE WHEN SHE THOUGHT IT OVER AFTERWARDS IT OCCURRED TO HER THAT SHE OUGHT TO HAVE WONDERED AT THIS BUT AT THE TIME IT ALL SEEMED QUITE NATURAL
https://www.linkedin.com/pulse/ok-google-speech-text-python-deep-learning-2-minutes-kumar-
728x90
반응형
LIST
'Linguistic Intelligence > Speech Recognition' 카테고리의 다른 글
[Speech Recognition] wav2vec2 (0) | 2024.08.06 |
---|---|
[Speech Recognition] 연결주의 시간 분류 (Connectionist Temporal Classification) (0) | 2024.07.16 |
[Speech Recognition] 레거시 음성 모델 (Legacy Acoustic Model) (0) | 2024.07.03 |
자동 음성 인식 (Automatic Speech Recognition) (0) | 2024.07.03 |
[Speech Recognition] 변환기 (Transducer) (0) | 2024.06.25 |