vdwow commited on
Commit
adf940f
·
verified ·
1 Parent(s): 9976f01

Update tasks/audio.py

Browse files
Files changed (1) hide show
  1. tasks/audio.py +12 -14
tasks/audio.py CHANGED
@@ -1,13 +1,12 @@
1
- import librosa
2
- import joblib
3
- import numpy as np
4
-
5
  from fastapi import APIRouter
6
  from datetime import datetime
7
  from datasets import load_dataset
8
  from sklearn.metrics import accuracy_score
9
  import random
10
  import os
 
 
 
11
 
12
  from .utils.evaluation import AudioEvaluationRequest
13
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
@@ -17,7 +16,7 @@ load_dotenv()
17
 
18
  router = APIRouter()
19
 
20
- DESCRIPTION = "Random Baseline"
21
  ROUTE = "/audio"
22
 
23
 
@@ -28,8 +27,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
28
  """
29
  Evaluate audio classification for rainforest sound detection.
30
 
31
- Current Model: Random Baseline
32
- - Makes random predictions from the label space (0-1)
33
  - Used as a baseline for comparison
34
  """
35
  # Get space info
@@ -57,15 +55,15 @@ async def evaluate_audio(request: AudioEvaluationRequest):
57
  # 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.
58
  #--------------------------------------------------------------------------------------------
59
 
60
- def extract_features(example, sampling_rate):
61
- audio_array = example['audio']['array']
62
- mfcc = librosa.feature.mfcc(y=audio_array, sr=sampling_rate, n_mfcc=5)
63
  return np.mean(mfcc, axis=1)
64
 
65
- def predict_new_audio(model, dataset, sampling_rate):
66
- features_list = [extract_features(example, sampling_rate) for example in dataset]
67
- features_array = np.vstack(features_list)
68
- predictions = model.predict(features_array)
69
  return predictions
70
 
71
  model_filename = "models/lightgbm_baseline_87_acc.pkl"
 
 
 
 
 
1
  from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
4
  from sklearn.metrics import accuracy_score
5
  import random
6
  import os
7
+ import librosa
8
+ import joblib
9
+ import numpy as np
10
 
11
  from .utils.evaluation import AudioEvaluationRequest
12
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
 
16
 
17
  router = APIRouter()
18
 
19
+ DESCRIPTION = "LGBM Classifier Baseline on Mel-frequency cepstral coefficients"
20
  ROUTE = "/audio"
21
 
22
 
 
27
  """
28
  Evaluate audio classification for rainforest sound detection.
29
 
30
+ Current Model: LGBM
 
31
  - Used as a baseline for comparison
32
  """
33
  # Get space info
 
55
  # 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.
56
  #--------------------------------------------------------------------------------------------
57
 
58
+ def compute_mfcc(row, sr):
59
+ audio_array = row['audio']['array']
60
+ mfcc = librosa.feature.mfcc(y=audio_array, sr=sr, n_mfcc=5)
61
  return np.mean(mfcc, axis=1)
62
 
63
+ def predict_new_audio(model, dataset, sr):
64
+ list_mfcc = [extract_features(row, sr) for row in dataset]
65
+ array_mfcc = np.vstack(list_mfcc)
66
+ predictions = model.predict(array_mfcc)
67
  return predictions
68
 
69
  model_filename = "models/lightgbm_baseline_87_acc.pkl"