CindyDelage commited on
Commit
286ffcb
·
verified ·
1 Parent(s): e532e7c

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +64 -59
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
- def evaluate_audio(request: AudioEvaluationRequest):
55
- """
56
- Evaluate audio classification for rainforest sound detection.
57
- """
58
- # Get space info
59
- logging.info("Chargement des données")
60
- dataset = load_dataset(request.dataset_name, streaming=True, token=os.getenv("HF_TOKEN"))
61
- logging.info("Données chargées")
 
 
62
 
63
- test_dataset = dataset["test"]
64
- del dataset
 
 
 
 
 
 
 
 
 
 
65
 
66
- # Start tracking emissions
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
- # Appliquer la quantification dynamique et le pruning
80
- model.eval()
81
- #model = torch.quantization.quantize_dynamic(model, dtype=torch.qint8)
82
- #model = apply_pruning(model, amount=0.3) # Prune 30% des poids linéaires
83
 
84
- classifier = pipeline("audio-classification", model=model, feature_extractor=feature_extractor, device=device)
85
- predictions = []
86
- logging.info("Début des prédictions par batch")
87
- i=0
88
- for data in iter(test_dataset):
89
- print(i)
90
- if (i<=50):
91
- with torch.no_grad():
92
- result = classifier(np.asarray(data["input_values"]), batch_size=64)
93
- predicted_label = result[0]['label']
94
- label = 1 if predicted_label == 'environment' else 0
95
- predictions.append(label)
96
-
97
- # Nettoyer la mémoire après chaque itération
98
- del result
99
- del label
100
- torch.cuda.empty_cache()
101
- gc.collect()
102
- i=i+1
103
- if(i>50):
104
- break
105
- logging.info("Fin des prédictions")
106
- del classifier
107
- del feature_extractor
108
-
109
- gc.collect()
110
- # Stop tracking emissions
111
- emissions_data = tracker.stop_task()
112
-
113
- return emissions_data
114
-
115
- request = AudioEvaluationRequest()
116
- print(evaluate_audio(request))
 
 
 
 
 
 
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