vlaurent17 commited on
Commit
0ff353f
·
verified ·
1 Parent(s): 3c1b5c3

Upload 3 files

Browse files
Files changed (3) hide show
  1. H.npy +3 -0
  2. audio.py +130 -0
  3. model.joblib +3 -0
H.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e5b7bf9ff7240c43f532650b661e94d16f6da2d0be2c6c583a5b6cc0da226b87
3
+ size 34688
audio.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
13
+
14
+ from dotenv import load_dotenv
15
+ load_dotenv()
16
+
17
+ router = APIRouter()
18
+
19
+ DESCRIPTION = "Random Baseline"
20
+ ROUTE = "/audio"
21
+
22
+ def create_spec(dataset, target_sampling_rate=3000):
23
+ spectograms = []
24
+ audio_length = int(36000/(12000/target_sampling_rate))
25
+ for d in dataset:
26
+ audio_sample = librosa.resample(
27
+ d["audio"]["array"],
28
+ orig_sr= d["audio"]["sampling_rate"],
29
+ target_sr=target_sampling_rate
30
+ )
31
+
32
+ if len(audio_sample) == 0:
33
+ continue
34
+ if len(audio_sample) < audio_length:
35
+ padding_needed = audio_length - len(audio_sample)
36
+ repeats = (padding_needed // len(audio_sample)) + 1
37
+ audio_sample = np.concatenate([audio_sample] + [audio_sample[:padding_needed]] * repeats)[:audio_length]
38
+ elif len(audio_sample) > audio_length:
39
+ audio_sample = audio_sample[:audio_length]
40
+
41
+ rms = np.sqrt(np.mean(np.square(audio_sample)))
42
+ scalar = 10 ** (-20 / 20) / (rms + 1e-8)
43
+
44
+ mel = librosa.feature.melspectrogram(
45
+ y=audio_sample*scalar,
46
+ sr=12000,
47
+ n_fft=2048,
48
+ hop_length=1024,
49
+ n_mels=12,
50
+ power=2.0,
51
+ )
52
+ mel_db = librosa.power_to_db(mel, ref=np.max)
53
+ mel_db_normalized = (mel_db - mel_db.mean()) / (mel_db.std() + 1e-8)
54
+ spectograms.append(mel_db_normalized.T.flatten())
55
+
56
+ return np.stack(spectograms)
57
+
58
+
59
+ @router.post(ROUTE, tags=["Audio Task"],
60
+ description=DESCRIPTION)
61
+ async def evaluate_audio(request: AudioEvaluationRequest):
62
+ """
63
+ Evaluate audio classification for rainforest sound detection.
64
+
65
+ Current Model: Random Baseline
66
+ - Makes random predictions from the label space (0-1)
67
+ - Used as a baseline for comparison
68
+ """
69
+ # Get space info
70
+ username, space_url = get_space_info()
71
+
72
+ # Define the label mapping
73
+ LABEL_MAPPING = {
74
+ "chainsaw": 0,
75
+ "environment": 1
76
+ }
77
+ # Load and prepare the dataset
78
+ # Because the dataset is gated, we need to use the HF_TOKEN environment variable to authenticate
79
+ dataset = load_dataset(request.dataset_name,token=os.getenv("HF_TOKEN"))
80
+
81
+ # Split dataset
82
+ train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
83
+ test_dataset = train_test["test"]
84
+
85
+ # Start tracking emissions
86
+ tracker.start()
87
+ tracker.start_task("inference")
88
+
89
+ test_spec = create_spec(test_dataset)
90
+ H = np.load("H.npy")
91
+ W_test = np.dot(test_spec, H)
92
+ model = joblib.load('model.joblib')
93
+
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.
97
+ #--------------------------------------------------------------------------------------------
98
+
99
+ # Make random predictions (placeholder for actual model inference)
100
+ true_labels = test_dataset["label"]
101
+ predictions = model.predict(W_test)
102
+ #--------------------------------------------------------------------------------------------
103
+ # YOUR MODEL INFERENCE STOPS HERE
104
+ #--------------------------------------------------------------------------------------------
105
+
106
+ # Stop tracking emissions
107
+ emissions_data = tracker.stop_task()
108
+
109
+ # Calculate accuracy
110
+ accuracy = accuracy_score(true_labels, predictions)
111
+
112
+ # Prepare results dictionary
113
+ results = {
114
+ "username": username,
115
+ "space_url": space_url,
116
+ "submission_timestamp": datetime.now().isoformat(),
117
+ "model_description": DESCRIPTION,
118
+ "accuracy": float(accuracy),
119
+ "energy_consumed_wh": emissions_data.energy_consumed * 1000,
120
+ "emissions_gco2eq": emissions_data.emissions * 1000,
121
+ "emissions_data": clean_emissions_data(emissions_data),
122
+ "api_route": ROUTE,
123
+ "dataset_config": {
124
+ "dataset_name": request.dataset_name,
125
+ "test_size": request.test_size,
126
+ "test_seed": request.test_seed
127
+ }
128
+ }
129
+
130
+ return results
model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:33688213987985901a75886484a540a423d9e0d5967fd4a49a79c74aadb8697a
3
+ size 1350138