vdwow commited on
Commit
9976f01
·
1 Parent(s): 998e8ac
tasks/audio.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
@@ -53,9 +57,21 @@ async def evaluate_audio(request: AudioEvaluationRequest):
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.
54
  #--------------------------------------------------------------------------------------------
55
 
56
- # Make random predictions (placeholder for actual model inference)
57
- true_labels = test_dataset["label"]
58
- predictions = [random.randint(0, 1) for _ in range(len(true_labels))]
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  #--------------------------------------------------------------------------------------------
61
  # YOUR MODEL INFERENCE STOPS HERE
@@ -65,6 +81,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
65
  emissions_data = tracker.stop_task()
66
 
67
  # Calculate accuracy
 
68
  accuracy = accuracy_score(true_labels, predictions)
69
 
70
  # Prepare results dictionary
 
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
 
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"
72
+ clf = joblib.load(model_filename)
73
+
74
+ predictions = predict_new_audio(clf, test_dataset, 12000)
75
 
76
  #--------------------------------------------------------------------------------------------
77
  # YOUR MODEL INFERENCE STOPS HERE
 
81
  emissions_data = tracker.stop_task()
82
 
83
  # Calculate accuracy
84
+ true_labels = test_dataset["label"]
85
  accuracy = accuracy_score(true_labels, predictions)
86
 
87
  # Prepare results dictionary
tasks/models/lightgbm_baseline_87_acc.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2f6ffd6404837979e258257c20667274785d1803ee107dc82967ce9d41cd4ced
3
+ size 3388964