Spaces:
Sleeping
Sleeping
File size: 1,745 Bytes
be1b9b7 1fbfcdb 3a714b7 be1b9b7 3a714b7 be1b9b7 1fbfcdb 3a714b7 be1b9b7 1fbfcdb be1b9b7 f4212a0 be1b9b7 1fbfcdb be1b9b7 1fbfcdb be1b9b7 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import torch
import librosa
import os
from model import Wav2Vec2BertForSequenceClassification
from transformers import AutoFeatureExtractor
# from optimum.bettertransformer import BetterTransformer
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
# os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1'
# os.environ['TRANSFORMERS_VERBOSITY'] = 'error'
torch.random.manual_seed(0);
# protobuf==3.20.0
model_name = "arslanarjumand/wav2vec-reptiles"
processor = AutoFeatureExtractor.from_pretrained(model_name)
model = Wav2Vec2BertForSequenceClassification.from_pretrained(model_name).to(device)
# model = BetterTransformer.transform(model)
def load_audio(audio_path, processor):
audio, sr = librosa.load(audio_path, sr=16000)
input_values = processor(audio, sampling_rate=16000, return_tensors="pt").input_features
return input_values
@torch.inference_mode()
def get_emissions(input_values, model):
results = model(input_values,).logits[0]
return results
def speaker_pronunciation_assesment(audio_path):
input_values = load_audio(audio_path, processor)
result_scores = get_emissions(input_values, model)
pronunciation_score = round(result_scores[0].cpu().item())
fluency_score = round(result_scores[1].cpu().item())
total_score = round(result_scores[2].cpu().item())
content_scores = round(result_scores[3].cpu().item())
result = {'pronunciation_accuracy': pronunciation_score,
'content_scores': content_scores,
'total_score': total_score,
'fluency_score': fluency_score}
return result
if __name__ == '__main__':
pass
|