Spaces:
Sleeping
Sleeping
add progress bar to gradio space
Browse files- app.py +22 -15
- evaluation.py +24 -15
app.py
CHANGED
|
@@ -102,22 +102,29 @@ def run_full_evaluation_gradio():
|
|
| 102 |
test_dataloader_full = DataLoader(tokenized_imdb_test_full, batch_size=batch_size)
|
| 103 |
yield "Dataset tokenized and DataLoader prepared. Starting model evaluation on the test set..."
|
| 104 |
|
| 105 |
-
# The 'evaluate' function from evaluation.py
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
else:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
import traceback
|
|
|
|
| 102 |
test_dataloader_full = DataLoader(tokenized_imdb_test_full, batch_size=batch_size)
|
| 103 |
yield "Dataset tokenized and DataLoader prepared. Starting model evaluation on the test set..."
|
| 104 |
|
| 105 |
+
# The 'evaluate' function from evaluation.py is now a generator.
|
| 106 |
+
# Iterate through its yielded updates and results.
|
| 107 |
+
final_results_str = ""
|
| 108 |
+
for update in evaluate(model, test_dataloader_full, device):
|
| 109 |
+
if isinstance(update, dict):
|
| 110 |
+
# This is the final results dictionary
|
| 111 |
+
results_str = "--- Full Evaluation Results ---\n"
|
| 112 |
+
for key, value in update.items():
|
| 113 |
+
if isinstance(value, float):
|
| 114 |
+
results_str += f"{key.capitalize()}: {value:.4f}\n"
|
| 115 |
+
else:
|
| 116 |
+
results_str += f"{key.capitalize()}: {value}\n"
|
| 117 |
+
results_str += "\nEvaluation finished."
|
| 118 |
+
final_results_str = results_str # Store to yield last
|
| 119 |
+
yield results_str # Optionally yield intermediate dict if needed, or just final string
|
| 120 |
+
break # Stop after getting the results dict
|
| 121 |
else:
|
| 122 |
+
# This is a progress string
|
| 123 |
+
yield update
|
| 124 |
+
|
| 125 |
+
# Ensure the final formatted results string is yielded if not already (e.g., if loop broke early)
|
| 126 |
+
# However, the logic above should yield it before breaking.
|
| 127 |
+
# If evaluate could end without yielding a dict, this might be needed.
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
import traceback
|
evaluation.py
CHANGED
|
@@ -10,9 +10,14 @@ def evaluate(model, dataloader, device):
|
|
| 10 |
all_labels = []
|
| 11 |
all_probs_for_auc = []
|
| 12 |
total_loss = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
with torch.no_grad():
|
| 15 |
-
for batch in dataloader:
|
|
|
|
| 16 |
# Move batch to device, ensure all model inputs are covered
|
| 17 |
input_ids = batch['input_ids'].to(device)
|
| 18 |
attention_mask = batch['attention_mask'].to(device)
|
|
@@ -49,15 +54,11 @@ def evaluate(model, dataloader, device):
|
|
| 49 |
all_preds.extend(preds.cpu().numpy())
|
| 50 |
|
| 51 |
all_labels.extend(labels.cpu().numpy())
|
| 52 |
-
|
| 53 |
-
if
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
probs = torch.sigmoid(logits)
|
| 58 |
-
all_probs_for_auc.extend(probs.squeeze().cpu().numpy())
|
| 59 |
-
|
| 60 |
-
avg_loss = total_loss / len(dataloader)
|
| 61 |
accuracy = accuracy_score(all_labels, all_preds)
|
| 62 |
f1 = f1_score(all_labels, all_preds, average='weighted', zero_division=0)
|
| 63 |
precision = precision_score(all_labels, all_preds, average='weighted', zero_division=0)
|
|
@@ -70,15 +71,18 @@ def evaluate(model, dataloader, device):
|
|
| 70 |
print(f"Could not calculate AUC-ROC: {e}. Labels: {list(set(all_labels))[:10]}. Probs example: {all_probs_for_auc[:5]}. Setting to 0.0")
|
| 71 |
roc_auc = 0.0
|
| 72 |
|
| 73 |
-
|
| 74 |
-
'loss': avg_loss,
|
| 75 |
'accuracy': accuracy,
|
| 76 |
'f1': f1,
|
| 77 |
'roc_auc': roc_auc,
|
| 78 |
'precision': precision,
|
| 79 |
'recall': recall,
|
| 80 |
-
'mcc': mcc
|
|
|
|
| 81 |
}
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
import argparse
|
|
@@ -177,9 +181,14 @@ if __name__ == "__main__":
|
|
| 177 |
test_dataloader = DataLoader(tokenized_imdb_test, batch_size=args.batch_size)
|
| 178 |
|
| 179 |
print("Starting evaluation...")
|
| 180 |
-
progress_bar = tqdm(test_dataloader, desc="Evaluating")
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
print("\n--- Evaluation Results ---")
|
| 185 |
for key, value in results.items():
|
|
|
|
| 10 |
all_labels = []
|
| 11 |
all_probs_for_auc = []
|
| 12 |
total_loss = 0
|
| 13 |
+
num_batches = len(dataloader)
|
| 14 |
+
processed_batches = 0
|
| 15 |
+
|
| 16 |
+
yield "Starting evaluation..."
|
| 17 |
|
| 18 |
with torch.no_grad():
|
| 19 |
+
for batch in dataloader: # dataloader here should not be pre-wrapped with tqdm by the caller if we yield progress
|
| 20 |
+
processed_batches += 1
|
| 21 |
# Move batch to device, ensure all model inputs are covered
|
| 22 |
input_ids = batch['input_ids'].to(device)
|
| 23 |
attention_mask = batch['attention_mask'].to(device)
|
|
|
|
| 54 |
all_preds.extend(preds.cpu().numpy())
|
| 55 |
|
| 56 |
all_labels.extend(labels.cpu().numpy())
|
| 57 |
+
# Yield progress update
|
| 58 |
+
if processed_batches % (num_batches // 20) == 0 or processed_batches == num_batches: # Update roughly 20 times + final
|
| 59 |
+
yield f"Processed {processed_batches}/{num_batches} batches ({processed_batches/num_batches*100:.2f}%)"
|
| 60 |
+
|
| 61 |
+
avg_loss = total_loss / num_batches
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
accuracy = accuracy_score(all_labels, all_preds)
|
| 63 |
f1 = f1_score(all_labels, all_preds, average='weighted', zero_division=0)
|
| 64 |
precision = precision_score(all_labels, all_preds, average='weighted', zero_division=0)
|
|
|
|
| 71 |
print(f"Could not calculate AUC-ROC: {e}. Labels: {list(set(all_labels))[:10]}. Probs example: {all_probs_for_auc[:5]}. Setting to 0.0")
|
| 72 |
roc_auc = 0.0
|
| 73 |
|
| 74 |
+
results = {
|
|
|
|
| 75 |
'accuracy': accuracy,
|
| 76 |
'f1': f1,
|
| 77 |
'roc_auc': roc_auc,
|
| 78 |
'precision': precision,
|
| 79 |
'recall': recall,
|
| 80 |
+
'mcc': mcc,
|
| 81 |
+
'average_loss': avg_loss
|
| 82 |
}
|
| 83 |
+
yield f"Processed {processed_batches}/{num_batches} batches (100.00%)" # Ensure final progress update
|
| 84 |
+
yield "Evaluation complete. Compiling results..."
|
| 85 |
+
yield results
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
import argparse
|
|
|
|
| 181 |
test_dataloader = DataLoader(tokenized_imdb_test, batch_size=args.batch_size)
|
| 182 |
|
| 183 |
print("Starting evaluation...")
|
| 184 |
+
progress_bar = tqdm(evaluate(model, test_dataloader, device), desc="Evaluating")
|
| 185 |
|
| 186 |
+
for update in progress_bar:
|
| 187 |
+
if isinstance(update, dict):
|
| 188 |
+
results = update
|
| 189 |
+
break
|
| 190 |
+
else:
|
| 191 |
+
progress_bar.set_postfix_str(update)
|
| 192 |
|
| 193 |
print("\n--- Evaluation Results ---")
|
| 194 |
for key, value in results.items():
|