Spaces:
Runtime error
Runtime error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -51,66 +51,71 @@ class AudioEvaluationRequest(BaseEvaluationRequest):
|
|
| 51 |
dataset_name: str = Field("rfcx/frugalai",
|
| 52 |
description="The name of the dataset on HuggingFace Hub")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
tracker.start()
|
| 68 |
-
tracker.start_task("inference")
|
| 69 |
-
|
| 70 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base")
|
| 71 |
-
|
| 72 |
-
test_dataset = test_dataset.map(preprocess_function, fn_kwargs={"feature_extractor": feature_extractor}, remove_columns="audio", batched=True, batch_size=32)
|
| 73 |
-
|
| 74 |
-
gc.collect()
|
| 75 |
-
|
| 76 |
-
model_name = "CindyDelage/Challenge_HuggingFace_DFG_FrugalAI"
|
| 77 |
-
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
#model = apply_pruning(model, amount=0.3) # Prune 30% des poids linéaires
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
dataset_name: str = Field("rfcx/frugalai",
|
| 52 |
description="The name of the dataset on HuggingFace Hub")
|
| 53 |
|
| 54 |
+
class evaluate_consumption(Tool):
|
| 55 |
+
name = "evaluate_consumption"
|
| 56 |
+
description = "Uses code carbon to evaluate the CO2 emissions from a given Python code"
|
| 57 |
+
inputs = {
|
| 58 |
+
"code": {
|
| 59 |
+
"type": "string",
|
| 60 |
+
"description": "The code to evaluate."
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
output_type = "string"
|
| 64 |
|
| 65 |
+
def forward(self, code):
|
| 66 |
+
request = AudioEvaluationRequest()
|
| 67 |
+
logging.info("Chargement des données")
|
| 68 |
+
dataset = load_dataset(request.dataset_name, streaming=True, token=os.getenv("HF_TOKEN"))
|
| 69 |
+
logging.info("Données chargées")
|
| 70 |
+
|
| 71 |
+
test_dataset = dataset["test"]
|
| 72 |
+
del dataset
|
| 73 |
+
|
| 74 |
+
# Start tracking emissions
|
| 75 |
+
tracker.start()
|
| 76 |
+
tracker.start_task("inference")
|
| 77 |
|
| 78 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
test_dataset = test_dataset.map(preprocess_function, fn_kwargs={"feature_extractor": feature_extractor}, remove_columns="audio", batched=True, batch_size=32)
|
| 81 |
+
|
| 82 |
+
gc.collect()
|
|
|
|
| 83 |
|
| 84 |
+
model_name = "CindyDelage/Challenge_HuggingFace_DFG_FrugalAI"
|
| 85 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
|
| 86 |
+
|
| 87 |
+
# Appliquer la quantification dynamique et le pruning
|
| 88 |
+
model.eval()
|
| 89 |
+
#model = torch.quantization.quantize_dynamic(model, dtype=torch.qint8)
|
| 90 |
+
#model = apply_pruning(model, amount=0.3) # Prune 30% des poids linéaires
|
| 91 |
+
|
| 92 |
+
classifier = pipeline("audio-classification", model=model, feature_extractor=feature_extractor, device=device)
|
| 93 |
+
predictions = []
|
| 94 |
+
logging.info("Début des prédictions par batch")
|
| 95 |
+
i=0
|
| 96 |
+
for data in iter(test_dataset):
|
| 97 |
+
print(i)
|
| 98 |
+
if (i<=50):
|
| 99 |
+
with torch.no_grad():
|
| 100 |
+
result = classifier(np.asarray(data["input_values"]), batch_size=64)
|
| 101 |
+
predicted_label = result[0]['label']
|
| 102 |
+
label = 1 if predicted_label == 'environment' else 0
|
| 103 |
+
predictions.append(label)
|
| 104 |
+
|
| 105 |
+
# Nettoyer la mémoire après chaque itération
|
| 106 |
+
del result
|
| 107 |
+
del label
|
| 108 |
+
torch.cuda.empty_cache()
|
| 109 |
+
gc.collect()
|
| 110 |
+
i=i+1
|
| 111 |
+
if(i>50):
|
| 112 |
+
break
|
| 113 |
+
logging.info("Fin des prédictions")
|
| 114 |
+
del classifier
|
| 115 |
+
del feature_extractor
|
| 116 |
+
|
| 117 |
+
gc.collect()
|
| 118 |
+
# Stop tracking emissions
|
| 119 |
+
emissions_data = tracker.stop_task()
|
| 120 |
+
|
| 121 |
+
return emissions_data
|