Spaces:
Sleeping
Sleeping
Update functions/audio.py
Browse files- functions/audio.py +147 -145
functions/audio.py
CHANGED
@@ -1,145 +1,147 @@
|
|
1 |
-
import librosa
|
2 |
-
import numpy as np
|
3 |
-
import torch
|
4 |
-
from collections import Counter
|
5 |
-
import nltk
|
6 |
-
import string
|
7 |
-
import matplotlib.pyplot as plt
|
8 |
-
from wordcloud import WordCloud
|
9 |
-
|
10 |
-
nltk.download('punkt')
|
11 |
-
nltk.download('
|
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 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
#
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
'
|
94 |
-
'
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
"
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
plt.
|
121 |
-
plt.
|
122 |
-
plt.
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
print("
|
127 |
-
print("
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
"
|
133 |
-
"
|
134 |
-
"
|
135 |
-
"
|
136 |
-
"
|
137 |
-
"
|
138 |
-
"
|
139 |
-
"
|
140 |
-
"
|
141 |
-
"
|
142 |
-
"
|
143 |
-
"
|
144 |
-
"
|
145 |
-
|
|
|
|
|
|
1 |
+
import librosa
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from collections import Counter
|
5 |
+
import nltk
|
6 |
+
import string
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
from wordcloud import WordCloud
|
9 |
+
|
10 |
+
nltk.download('punkt')
|
11 |
+
nltk.download('punkt_tab')
|
12 |
+
nltk.download('averaged_perceptron_tagger_eng')
|
13 |
+
nltk.download('averaged_perceptron_tagger')
|
14 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
15 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
16 |
+
|
17 |
+
def get_pitch_list(y,sr):
|
18 |
+
hop_length = int(sr / 30) # hop_length determines how far apart the frames are
|
19 |
+
|
20 |
+
# Extract the pitch (F0) using librosa's piptrack method
|
21 |
+
pitches, magnitudes = librosa.piptrack(y=y, sr=sr, hop_length=hop_length)
|
22 |
+
|
23 |
+
# Get the pitch frequencies from the pitch array
|
24 |
+
pitch_frequencies = []
|
25 |
+
|
26 |
+
for t in range(pitches.shape[1]):
|
27 |
+
index = magnitudes[:, t].argmax() # Get the index of the maximum magnitude
|
28 |
+
pitch = pitches[index, t]
|
29 |
+
|
30 |
+
pitch_frequencies.append(pitch)
|
31 |
+
|
32 |
+
# Convert pitch_frequencies to a NumPy array
|
33 |
+
pitch_frequencies = np.array(pitch_frequencies)
|
34 |
+
print("shape : ",pitch_frequencies.shape)
|
35 |
+
return pitch_frequencies
|
36 |
+
|
37 |
+
|
38 |
+
def extract_audio_features(audio_path, asrmodel, asrproc, sentipipe, duration, wordcloud_path):
|
39 |
+
y, sr = librosa.load(audio_path, sr=16000)
|
40 |
+
inputs = asrproc(y, sampling_rate=sr, return_tensors="pt").input_features
|
41 |
+
inputs = inputs.to(device, dtype=torch_dtype)
|
42 |
+
with torch.no_grad():
|
43 |
+
generated_ids = asrmodel.generate(inputs)
|
44 |
+
transcript = asrproc.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
45 |
+
|
46 |
+
# Sound intensity (RMS)
|
47 |
+
rms = librosa.feature.rms(y=y)
|
48 |
+
sound_intensity = np.mean(rms)
|
49 |
+
|
50 |
+
# Pitch list
|
51 |
+
pitches=get_pitch_list(y,sr)
|
52 |
+
|
53 |
+
# Fundamental frequency (F0)
|
54 |
+
f0, voiced_flag, voiced_probs = librosa.pyin(y, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'))
|
55 |
+
fundamental_frequency = np.nanmean(f0)
|
56 |
+
|
57 |
+
# Spectral energy (based on STFT)
|
58 |
+
S = np.abs(librosa.stft(y))
|
59 |
+
spectral_energy = np.mean(np.sum(S ** 2, axis=0))
|
60 |
+
|
61 |
+
# Spectral centroid
|
62 |
+
spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
|
63 |
+
avg_spectral_centroid = np.mean(spectral_centroid)
|
64 |
+
|
65 |
+
# Zero-crossing rate
|
66 |
+
zcr = librosa.feature.zero_crossing_rate(y)
|
67 |
+
zero_crossing_rate = np.mean(zcr)
|
68 |
+
|
69 |
+
# Pause detection
|
70 |
+
silence_threshold = -40
|
71 |
+
silent_intervals = librosa.effects.split(y, top_db=silence_threshold)
|
72 |
+
pause_duration = 0
|
73 |
+
for start, end in silent_intervals:
|
74 |
+
pause_duration += (end - start) / sr
|
75 |
+
|
76 |
+
total_duration = librosa.get_duration(y=y, sr=sr)
|
77 |
+
pause_rate = (pause_duration / total_duration) * 60 # Convert to pauses per minute
|
78 |
+
|
79 |
+
# Transcript processing
|
80 |
+
words = nltk.word_tokenize(transcript)
|
81 |
+
words = [word.lower() for word in words if word not in string.punctuation]
|
82 |
+
num_words = len(words)
|
83 |
+
unique_words = len(set(words))
|
84 |
+
word_frequencies = Counter(words)
|
85 |
+
|
86 |
+
# Duration in minutes
|
87 |
+
duration_minutes = total_duration / 60
|
88 |
+
avg_words_per_minute = num_words / duration_minutes
|
89 |
+
avg_unique_words_per_minute = unique_words / duration_minutes
|
90 |
+
|
91 |
+
# Filler word detection
|
92 |
+
filler_words = [
|
93 |
+
'uh', 'um', 'like', 'you know', 'ah', 'er', 'hmm', 'well', 'so',
|
94 |
+
'I mean', 'okay', 'right', 'actually', 'basically', 'you see',
|
95 |
+
'sort of', 'kind of', 'yeah', 'literally', 'just', 'I guess',
|
96 |
+
'totally', 'honestly', 'seriously', 'alright'
|
97 |
+
]
|
98 |
+
filler_word_count = sum([word_frequencies.get(filler, 0) for filler in filler_words])
|
99 |
+
filler_words_per_minute = filler_word_count / duration_minutes
|
100 |
+
|
101 |
+
# POS tagging
|
102 |
+
pos_tags = nltk.pos_tag(words)
|
103 |
+
nouns = [word for word, pos in pos_tags if pos.startswith('NN')]
|
104 |
+
adjectives = [word for word, pos in pos_tags if pos.startswith('JJ')]
|
105 |
+
verbs = [word for word, pos in pos_tags if pos.startswith('VB')]
|
106 |
+
|
107 |
+
# Sentiment analysis
|
108 |
+
sentiment = sentipipe(transcript)
|
109 |
+
sentiment_mapping = {
|
110 |
+
"LABEL_0": "Negative",
|
111 |
+
"LABEL_1": "Neutral",
|
112 |
+
"LABEL_2": "Positive"
|
113 |
+
}
|
114 |
+
sentiment[0]['label'] = sentiment_mapping[sentiment[0]['label']]
|
115 |
+
|
116 |
+
# Generate Word Cloud and Save it as an Image
|
117 |
+
wordcloud = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(word_frequencies)
|
118 |
+
|
119 |
+
# Save the Word Cloud to the provided path
|
120 |
+
plt.figure(figsize=(10, 5))
|
121 |
+
plt.imshow(wordcloud, interpolation='bilinear')
|
122 |
+
plt.axis('off')
|
123 |
+
plt.savefig(wordcloud_path, format='png')
|
124 |
+
plt.close()
|
125 |
+
|
126 |
+
print("Nouns: ", nouns)
|
127 |
+
print("Adjectives: ", adjectives)
|
128 |
+
print("Verbs: ", verbs)
|
129 |
+
print("Sentiment: ", sentiment)
|
130 |
+
|
131 |
+
return {
|
132 |
+
"transcript": transcript,
|
133 |
+
"sentiment": sentiment,
|
134 |
+
"sound_intensity": float(sound_intensity),
|
135 |
+
"fundamental_frequency": float(fundamental_frequency),
|
136 |
+
"spectral_energy": float(spectral_energy),
|
137 |
+
"spectral_centroid": float(avg_spectral_centroid),
|
138 |
+
"zero_crossing_rate": float(zero_crossing_rate),
|
139 |
+
"avg_words_per_minute": float(avg_words_per_minute),
|
140 |
+
"avg_unique_words_per_minute": float(avg_unique_words_per_minute),
|
141 |
+
"unique_word_count": int(unique_words),
|
142 |
+
"filler_words_per_minute": float(filler_words_per_minute),
|
143 |
+
"noun_count": len(nouns),
|
144 |
+
"adjective_count": len(adjectives),
|
145 |
+
"verb_count": len(verbs),
|
146 |
+
"pause_rate": float(pause_rate)
|
147 |
+
},pitches
|