Spaces:
Sleeping
Sleeping
Anas Benalla
commited on
update inference code
Browse files- tasks/audio.py +33 -4
tasks/audio.py
CHANGED
|
@@ -4,7 +4,8 @@ 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 |
|
|
@@ -43,6 +44,23 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
| 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()
|
|
@@ -52,10 +70,21 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
| 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 |
-
|
| 57 |
true_labels = test_dataset["label"]
|
| 58 |
-
predictions = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
#--------------------------------------------------------------------------------------------
|
| 61 |
# YOUR MODEL INFERENCE STOPS HERE
|
|
|
|
| 4 |
from sklearn.metrics import accuracy_score
|
| 5 |
import random
|
| 6 |
import os
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
import numpy as np
|
| 9 |
from .utils.evaluation import AudioEvaluationRequest
|
| 10 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
| 11 |
|
|
|
|
| 44 |
# Split dataset
|
| 45 |
train_test = dataset["train"].train_test_split(test_size=request.test_size, seed=request.test_seed)
|
| 46 |
test_dataset = train_test["test"]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def compute_spectrogram(audio_array, sample_rate=16000, frame_length=256, frame_step=128):
|
| 50 |
+
spectrogram = tf.signal.stft(audio_array, frame_length=frame_length, frame_step=frame_step)
|
| 51 |
+
spectrogram = tf.abs(spectrogram)
|
| 52 |
+
return tf.expand_dims(spectrogram, axis=-1)
|
| 53 |
+
|
| 54 |
+
def preprocess(item, max_length=16000):
|
| 55 |
+
audio_array = item["audio"]["array"]
|
| 56 |
+
audio_array = tf.convert_to_tensor(audio_array, dtype=tf.float32)
|
| 57 |
+
if len(audio_array) < max_length:
|
| 58 |
+
pad_size = max_length - len(audio_array)
|
| 59 |
+
audio_array = tf.concat([audio_array, tf.zeros(pad_size)], axis=0)
|
| 60 |
+
else:
|
| 61 |
+
audio_array = audio_array[:max_length]
|
| 62 |
+
spectrogram = compute_spectrogram(audio_array)
|
| 63 |
+
return spectrogram
|
| 64 |
|
| 65 |
# Start tracking emissions
|
| 66 |
tracker.start()
|
|
|
|
| 70 |
# YOUR MODEL INFERENCE CODE HERE
|
| 71 |
# 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.
|
| 72 |
#--------------------------------------------------------------------------------------------
|
| 73 |
+
MODEL_PATH = './model'
|
| 74 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 75 |
true_labels = test_dataset["label"]
|
| 76 |
+
predictions = []
|
| 77 |
+
|
| 78 |
+
for item in test_dataset:
|
| 79 |
+
spectrogram = preprocess(item)
|
| 80 |
+
spectrogram = tf.expand_dims(spectrogram, axis=0) # Add batch dimension
|
| 81 |
+
pred_probs = model.predict(spectrogram, verbose=0)
|
| 82 |
+
predicted_label = np.argmax(pred_probs)
|
| 83 |
+
predictions.append(predicted_label)
|
| 84 |
+
|
| 85 |
+
# Make random predictions (placeholder for actual model inference)
|
| 86 |
+
#true_labels = test_dataset["label"]
|
| 87 |
+
#predictions = [random.randint(0, 1) for _ in range(len(true_labels))]
|
| 88 |
|
| 89 |
#--------------------------------------------------------------------------------------------
|
| 90 |
# YOUR MODEL INFERENCE STOPS HERE
|