Spaces:
Sleeping
Sleeping
LAURENT Valentin
commited on
Commit
·
91689ed
1
Parent(s):
998e8ac
test_1
Browse files- requirements.txt +3 -1
- tasks/H.npy +0 -0
- tasks/audio.py +45 -3
- tasks/model.joblib +0 -0
requirements.txt
CHANGED
@@ -7,4 +7,6 @@ pydantic>=1.10.0
|
|
7 |
python-dotenv>=1.0.0
|
8 |
gradio>=4.0.0
|
9 |
requests>=2.31.0
|
10 |
-
librosa==0.10.2.post1
|
|
|
|
|
|
7 |
python-dotenv>=1.0.0
|
8 |
gradio>=4.0.0
|
9 |
requests>=2.31.0
|
10 |
+
librosa==0.10.2.post1
|
11 |
+
joblib==1.4.2
|
12 |
+
numpy==2.0.2
|
tasks/H.npy
ADDED
Binary file (318 kB). View file
|
|
tasks/audio.py
CHANGED
@@ -4,6 +4,9 @@ from datasets import load_dataset
|
|
4 |
from sklearn.metrics import accuracy_score
|
5 |
import random
|
6 |
import os
|
|
|
|
|
|
|
7 |
|
8 |
from .utils.evaluation import AudioEvaluationRequest
|
9 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
@@ -15,7 +18,42 @@ router = APIRouter()
|
|
15 |
|
16 |
DESCRIPTION = "Random Baseline"
|
17 |
ROUTE = "/audio"
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
|
21 |
@router.post(ROUTE, tags=["Audio Task"],
|
@@ -48,6 +86,11 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
48 |
tracker.start()
|
49 |
tracker.start_task("inference")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
51 |
#--------------------------------------------------------------------------------------------
|
52 |
# YOUR MODEL INFERENCE CODE HERE
|
53 |
# Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
|
@@ -55,8 +98,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
55 |
|
56 |
# Make random predictions (placeholder for actual model inference)
|
57 |
true_labels = test_dataset["label"]
|
58 |
-
predictions =
|
59 |
-
|
60 |
#--------------------------------------------------------------------------------------------
|
61 |
# YOUR MODEL INFERENCE STOPS HERE
|
62 |
#--------------------------------------------------------------------------------------------
|
|
|
4 |
from sklearn.metrics import accuracy_score
|
5 |
import random
|
6 |
import os
|
7 |
+
import numpy as np
|
8 |
+
import librosa
|
9 |
+
import joblib
|
10 |
|
11 |
from .utils.evaluation import AudioEvaluationRequest
|
12 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
|
|
18 |
|
19 |
DESCRIPTION = "Random Baseline"
|
20 |
ROUTE = "/audio"
|
21 |
+
def create_spec(dataset):
|
22 |
+
spectograms = []
|
23 |
+
y = []
|
24 |
+
|
25 |
+
for d in enumerate(dataset):
|
26 |
+
audio_sample = d["audio"]["array"] if d["audio"]["sampling_rate"] == 12000 else librosa.resample(
|
27 |
+
d["audio"]["array"],
|
28 |
+
orig_sr= d["audio"]["sampling_rate"],
|
29 |
+
target_sr=12000
|
30 |
+
)
|
31 |
+
if len(audio_sample) == 0:
|
32 |
+
continue
|
33 |
+
if len(audio_sample) < 36000:
|
34 |
+
padding_needed = 36000 - len(audio_sample)
|
35 |
+
repeats = (padding_needed // len(audio_sample)) + 1
|
36 |
+
audio_sample = np.concatenate([audio_sample] + [audio_sample[:padding_needed]] * repeats)[:36000]
|
37 |
+
elif len(audio_sample) > 36000:
|
38 |
+
audio_sample = audio_sample[:36000]
|
39 |
+
|
40 |
+
rms = np.sqrt(np.mean(np.square(audio_sample)))
|
41 |
+
scalar = 10 ** (-20 / 20) / (rms + 1e-8)
|
42 |
+
|
43 |
+
mel = librosa.feature.melspectrogram(
|
44 |
+
y=audio_sample*scalar,
|
45 |
+
sr=12000,
|
46 |
+
n_fft=2048,
|
47 |
+
hop_length=512,
|
48 |
+
n_mels=32,
|
49 |
+
power=2.0,
|
50 |
+
)
|
51 |
+
mel_db = librosa.power_to_db(mel, ref=np.max)
|
52 |
+
mel_db_normalized = (mel_db - mel_db.mean()) / (mel_db.std() + 1e-8)
|
53 |
+
spectograms.append(np.float16(mel_db_normalized).T.flatten())
|
54 |
+
y.append(d["label"])
|
55 |
+
|
56 |
+
return np.stack(spectograms),y
|
57 |
|
58 |
|
59 |
@router.post(ROUTE, tags=["Audio Task"],
|
|
|
86 |
tracker.start()
|
87 |
tracker.start_task("inference")
|
88 |
|
89 |
+
test_spec, y_test = create_spec(test_dataset)
|
90 |
+
H = np.load("array_file.npy")
|
91 |
+
W_val = np.dot(test_spec, H)
|
92 |
+
model_filename = 'model.joblib'
|
93 |
+
model = joblib.load(model_filename)
|
94 |
#--------------------------------------------------------------------------------------------
|
95 |
# YOUR MODEL INFERENCE CODE HERE
|
96 |
# Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
|
|
|
98 |
|
99 |
# Make random predictions (placeholder for actual model inference)
|
100 |
true_labels = test_dataset["label"]
|
101 |
+
predictions = model.predict(W_val)
|
|
|
102 |
#--------------------------------------------------------------------------------------------
|
103 |
# YOUR MODEL INFERENCE STOPS HERE
|
104 |
#--------------------------------------------------------------------------------------------
|
tasks/model.joblib
ADDED
Binary file (859 kB). View file
|
|