Spaces:
Runtime error
Runtime error
File size: 1,155 Bytes
98a69c9 5759877 98a69c9 5759877 98a69c9 5759877 |
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 |
import torch import torchaudio from transformers import Wav2Vec2ForCTC,Wav2Vec2Processor,pipeline processor = Wav2Vec2Processor.from_pretrained(model_name_or_path) model = Wav2Vec2ForCTC.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-persian") def speech_file_to_array_fn(path, sampling_rate): speech_array, _sampling_rate = torchaudio.load(path) resampler = torchaudio.transforms.Resample(_sampling_rate) speech = resampler(speech_array).squeeze().numpy() return speech def predict(path, sampling_rate): speech = speech_file_to_array_fn(path, sampling_rate) inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True) inputs = {key: inputs[key].to(device) for key in inputs} with torch.no_grad(): logits = model(**inputs).logits scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0] outputs = [{"Label": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)] return outputs def SER(Audio): return predict(Audio,16000) iface = gr.Interface(fn=SER, inputs="audio", outputs="text") iface.launch(share=False) |