soury commited on
Commit
8d30d08
·
verified ·
1 Parent(s): 998e8ac

- setup processing & initial classifier (d1276d6fedbf2879914e20609918aae06c884daf)

.gitignore CHANGED
@@ -6,7 +6,9 @@ __pycache__/
6
  .env
7
  .ipynb_checkpoints
8
  .vscode/
9
-
 
 
10
  eval-queue/
11
  eval-results/
12
  eval-queue-bk/
 
6
  .env
7
  .ipynb_checkpoints
8
  .vscode/
9
+ notebooks
10
+ Pipfile
11
+ Pipfile.lock
12
  eval-queue/
13
  eval-results/
14
  eval-queue-bk/
models/audio_classification_baseline.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7a27a9a671a920660995bc08b255e17449427f018402ceec81710a0ae93cb612
3
+ size 36073945
tasks/audio.py CHANGED
@@ -4,9 +4,12 @@ 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
10
 
11
  from dotenv import load_dotenv
12
  load_dotenv()
@@ -17,13 +20,12 @@ DESCRIPTION = "Random Baseline"
17
  ROUTE = "/audio"
18
 
19
 
20
-
21
  @router.post(ROUTE, tags=["Audio Task"],
22
  description=DESCRIPTION)
23
  async def evaluate_audio(request: AudioEvaluationRequest):
24
  """
25
  Evaluate audio classification for rainforest sound detection.
26
-
27
  Current Model: Random Baseline
28
  - Makes random predictions from the label space (0-1)
29
  - Used as a baseline for comparison
@@ -38,35 +40,58 @@ async def evaluate_audio(request: AudioEvaluationRequest):
38
  }
39
  # Load and prepare the dataset
40
  # Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
41
- dataset = load_dataset(request.dataset_name,token=os.getenv("HF_TOKEN"))
42
-
43
  # Split dataset
44
- train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
 
45
  test_dataset = train_test["test"]
46
-
47
  # Start tracking emissions
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.
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
62
- #--------------------------------------------------------------------------------------------
63
-
64
  # Stop tracking emissions
65
  emissions_data = tracker.stop_task()
66
-
67
  # Calculate accuracy
68
  accuracy = accuracy_score(true_labels, predictions)
69
-
70
  # Prepare results dictionary
71
  results = {
72
  "username": username,
@@ -84,5 +109,4 @@ async def evaluate_audio(request: AudioEvaluationRequest):
84
  "test_seed": request.test_seed
85
  }
86
  }
87
-
88
- return results
 
4
  from sklearn.metrics import accuracy_score
5
  import random
6
  import os
7
+ import joblib
8
+ import librosa
9
+ import numpy as np
10
 
11
+ from utils.evaluation import AudioEvaluationRequest
12
+ from utils.emissions import tracker, clean_emissions_data, get_space_info
13
 
14
  from dotenv import load_dotenv
15
  load_dotenv()
 
20
  ROUTE = "/audio"
21
 
22
 
 
23
  @router.post(ROUTE, tags=["Audio Task"],
24
  description=DESCRIPTION)
25
  async def evaluate_audio(request: AudioEvaluationRequest):
26
  """
27
  Evaluate audio classification for rainforest sound detection.
28
+
29
  Current Model: Random Baseline
30
  - Makes random predictions from the label space (0-1)
31
  - Used as a baseline for comparison
 
40
  }
41
  # Load and prepare the dataset
42
  # Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
43
+ dataset = load_dataset(request.dataset_name, token=os.getenv("HF_TOKEN"))
44
+
45
  # Split dataset
46
+ train_test = dataset["train"].train_test_split(
47
+ test_size=request.test_size, seed=request.test_seed)
48
  test_dataset = train_test["test"]
49
+
50
  # Start tracking emissions
51
  tracker.start()
52
  tracker.start_task("inference")
53
+
54
+ # --------------------------------------------------------------------------------------------
55
  # YOUR MODEL INFERENCE CODE HERE
56
  # 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.
57
+ # --------------------------------------------------------------------------------------------
58
+ # data formatting
59
+
60
+ def preprocess(dataset):
61
+ features = []
62
+ for row in dataset:
63
+ # Load the audio file and resample it
64
+ target_sr = 25000
65
+ audio = row['audio']['array']
66
+ audio = librosa.resample(audio, orig_sr=12000, target_sr=target_sr)
67
+
68
+ # Extract MFCC features
69
+ mfccs = librosa.feature.mfcc(y=audio, sr=target_sr, n_mfcc=40)
70
+ mfccs_scaled = np.mean(mfccs.T, axis=0)
71
+
72
+ # Append features and labels
73
+ features.append(mfccs_scaled)
74
+
75
+ return np.array(features)
76
+
77
+ X_test = preprocess(test_dataset)
78
+
79
+ classification_model = joblib.load(
80
+ "../models/audio_classification_baseline.pkl")
81
+
82
+ predictions = classification_model.predict(X_test)
83
  true_labels = test_dataset["label"]
84
+
85
+ # --------------------------------------------------------------------------------------------
 
86
  # YOUR MODEL INFERENCE STOPS HERE
87
+ # --------------------------------------------------------------------------------------------
88
+
89
  # Stop tracking emissions
90
  emissions_data = tracker.stop_task()
91
+
92
  # Calculate accuracy
93
  accuracy = accuracy_score(true_labels, predictions)
94
+
95
  # Prepare results dictionary
96
  results = {
97
  "username": username,
 
109
  "test_seed": request.test_seed
110
  }
111
  }
112
+ return results