File size: 1,020 Bytes
1466b77
 
 
267c239
1466b77
267c239
1466b77
 
 
 
 
 
 
 
267c239
 
1466b77
 
faee479
 
 
 
1466b77
 
 
 
 
 
 
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
from transformers import Wav2Vec2ForCTC, AutoProcessor
import torchaudio
import torch
import os

hf_token = os.getenv("HUGGING_FACE_HUB_TOKEN")

def read_audio_data(file):
    speech_array, sampling_rate = torchaudio.load(file, normalize = True)
    return speech_array, sampling_rate

def load_model():
    model_id = "Lguyogiro/wav2vec2-large-mms-1b-oji-adapterft"
    target_lang = "oji"
    processor = AutoProcessor.from_pretrained(model_id, target_lang=target_lang, use_auth_token=hf_token)
    model = Wav2Vec2ForCTC.from_pretrained(model_id, target_lang=target_lang, ignore_mismatched_sizes=True, use_safetensors=True, use_auth_token=hf_token)


def inference(model, raw_data):
    # arr, rate = read_audio_data(audio_path)
    # arr.squeeze().numpy(), ...
    inputs = processor(raw_data, sampling_rate=16_000, return_tensors="pt")

    with torch.no_grad():
        outputs = model(**inputs).logits
    ids = torch.argmax(outputs, dim=-1)[0]
    transcription = processor.decode(ids)

    return transcription