Update README.md
Browse files
README.md
CHANGED
@@ -10,6 +10,70 @@ tags:
|
|
10 |
license: gpl-3.0
|
11 |
---
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Results
|
14 |
|
15 |
| | precision | recall | f1-score | support |
|
|
|
10 |
license: gpl-3.0
|
11 |
---
|
12 |
|
13 |
+
# Prepare and importing
|
14 |
+
|
15 |
+
```python
|
16 |
+
import torch
|
17 |
+
import torch.nn as nn
|
18 |
+
import torch.nn.functional as F
|
19 |
+
import torchaudio
|
20 |
+
from transformers import Wav2Vec2Config, AutoModelForAudioClassification, Wav2Vec2FeatureExtractor
|
21 |
+
|
22 |
+
import librosa
|
23 |
+
import numpy as np
|
24 |
+
|
25 |
+
|
26 |
+
def speech_file_to_array_fn(path, sampling_rate):
|
27 |
+
speech_array, _sampling_rate = torchaudio.load(path)
|
28 |
+
resampler = torchaudio.transforms.Resample(_sampling_rate)
|
29 |
+
speech = resampler(speech_array).squeeze().numpy()
|
30 |
+
return speech
|
31 |
+
|
32 |
+
|
33 |
+
def predict(path, sampling_rate):
|
34 |
+
speech = speech_file_to_array_fn(path, sampling_rate)
|
35 |
+
inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
|
36 |
+
inputs = {key: inputs[key].to(device) for key in inputs}
|
37 |
+
|
38 |
+
with torch.no_grad():
|
39 |
+
logits = model_(**inputs).logits
|
40 |
+
|
41 |
+
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
|
42 |
+
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
|
43 |
+
return outputs
|
44 |
+
```
|
45 |
+
|
46 |
+
# Evoking:
|
47 |
+
|
48 |
+
```python
|
49 |
+
TRUST = true
|
50 |
+
|
51 |
+
config = Wav2Vec2Config.from_pretrained('Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition', trust_remote_code=TRUST)
|
52 |
+
model_ = AutoModelForAudioClassification.from_pretrained("Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition", trust_remote_code=TRUST, config=config)
|
53 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition")
|
54 |
+
|
55 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
56 |
+
model_.to(device)
|
57 |
+
```
|
58 |
+
|
59 |
+
# Use case
|
60 |
+
|
61 |
+
```python
|
62 |
+
result = predict("/path/to/russian_audio_speech.wav", 16000)
|
63 |
+
print(result)
|
64 |
+
```
|
65 |
+
|
66 |
+
```python
|
67 |
+
# outputs
|
68 |
+
[{'Emotion': 'anger', 'Score': '0.0%'},
|
69 |
+
{'Emotion': 'disgust', 'Score': '100.0%'},
|
70 |
+
{'Emotion': 'enthusiasm', 'Score': '0.0%'},
|
71 |
+
{'Emotion': 'fear', 'Score': '0.0%'},
|
72 |
+
{'Emotion': 'happiness', 'Score': '0.0%'},
|
73 |
+
{'Emotion': 'neutral', 'Score': '0.0%'},
|
74 |
+
{'Emotion': 'sadness', 'Score': '0.0%'}]
|
75 |
+
```
|
76 |
+
|
77 |
# Results
|
78 |
|
79 |
| | precision | recall | f1-score | support |
|