Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,28 +5,37 @@ import torch
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
from transformers import AutoModelForAudioClassification, ASTFeatureExtractor
|
7 |
import random
|
|
|
8 |
|
9 |
# Model and feature extractor loading
|
10 |
model = AutoModelForAudioClassification.from_pretrained("./")
|
11 |
feature_extractor = ASTFeatureExtractor.from_pretrained("./")
|
12 |
|
13 |
def plot_waveform(waveform, sr):
|
14 |
-
plt.figure(figsize=(12, 4))
|
15 |
plt.title('Waveform')
|
16 |
plt.ylabel('Amplitude')
|
17 |
plt.plot(np.linspace(0, len(waveform) / sr, len(waveform)), waveform)
|
18 |
plt.xlabel('Time (s)')
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def plot_spectrogram(waveform, sr):
|
22 |
S = librosa.feature.melspectrogram(y=waveform, sr=sr, n_mels=128)
|
23 |
S_DB = librosa.power_to_db(S, ref=np.max)
|
24 |
-
plt.figure(figsize=(12, 6))
|
25 |
librosa.display.specshow(S_DB, sr=sr, x_axis='time', y_axis='mel')
|
26 |
plt.title('Mel Spectrogram')
|
27 |
plt.colorbar(format='%+2.0f dB')
|
28 |
plt.tight_layout()
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def custom_feature_extraction(audio, sr=16000, target_length=1024):
|
32 |
features = feature_extractor(audio, sampling_rate=sr, return_tensors="pt", padding="max_length", max_length=target_length)
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
from transformers import AutoModelForAudioClassification, ASTFeatureExtractor
|
7 |
import random
|
8 |
+
import tempfile
|
9 |
|
10 |
# Model and feature extractor loading
|
11 |
model = AutoModelForAudioClassification.from_pretrained("./")
|
12 |
feature_extractor = ASTFeatureExtractor.from_pretrained("./")
|
13 |
|
14 |
def plot_waveform(waveform, sr):
|
15 |
+
plt.figure(figsize=(12, 4))
|
16 |
plt.title('Waveform')
|
17 |
plt.ylabel('Amplitude')
|
18 |
plt.plot(np.linspace(0, len(waveform) / sr, len(waveform)), waveform)
|
19 |
plt.xlabel('Time (s)')
|
20 |
+
# Save plot to a temporary file
|
21 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png', dir='./')
|
22 |
+
plt.savefig(temp_file.name)
|
23 |
+
plt.close() # Close the figure to free memory
|
24 |
+
return temp_file.name
|
25 |
|
26 |
def plot_spectrogram(waveform, sr):
|
27 |
S = librosa.feature.melspectrogram(y=waveform, sr=sr, n_mels=128)
|
28 |
S_DB = librosa.power_to_db(S, ref=np.max)
|
29 |
+
plt.figure(figsize=(12, 6))
|
30 |
librosa.display.specshow(S_DB, sr=sr, x_axis='time', y_axis='mel')
|
31 |
plt.title('Mel Spectrogram')
|
32 |
plt.colorbar(format='%+2.0f dB')
|
33 |
plt.tight_layout()
|
34 |
+
# Save plot to a temporary file
|
35 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png', dir='./')
|
36 |
+
plt.savefig(temp_file.name)
|
37 |
+
plt.close() # Close the figure to free memory
|
38 |
+
return temp_file.name
|
39 |
|
40 |
def custom_feature_extraction(audio, sr=16000, target_length=1024):
|
41 |
features = feature_extractor(audio, sampling_rate=sr, return_tensors="pt", padding="max_length", max_length=target_length)
|