Vrishab80's picture
Upload folder using huggingface_hub
d9664a4 verified
Raw
History Blame Contribute Delete
3.64 kB
#!/usr/bin/env python3
"""
Compare an agent's ai_output.csv against ground_truth.csv and print scoring
metrics for the AI-Assisted Document Review project.
Usage:
python score_output.py ground_truth.csv ai_output.csv
"""
import csv
import sys
from collections import defaultdict
def load_csv(path, key="Doc ID"):
with open(path, newline="") as f:
return {row[key]: row for row in csv.DictReader(f)}
def main():
if len(sys.argv) != 3:
print("Usage: python score_output.py ground_truth.csv ai_output.csv")
sys.exit(1)
gt_path, ai_path = sys.argv[1], sys.argv[2]
gt = load_csv(gt_path)
ai = load_csv(ai_path)
missing = set(gt) - set(ai)
extra = set(ai) - set(gt)
if missing:
print(f"WARNING: {len(missing)} documents from ground truth are missing in ai_output.csv: {sorted(missing)[:10]}...")
if extra:
print(f"WARNING: {len(extra)} unexpected Doc IDs in ai_output.csv not present in ground_truth.csv: {sorted(extra)[:10]}...")
common = sorted(set(gt) & set(ai))
total = len(common)
correct = 0
confusion = defaultdict(int)
# For precision/recall we treat "Relevant" as the positive class of interest,
# and count "Needs Human Review" as a correct (non-miss) outcome for recall purposes
# since routing an uncertain relevant doc to a human is the desired behavior, not a miss.
tp = fp = fn = tn = 0
review_correct = 0
review_total_gt = 0
for doc_id in common:
truth = gt[doc_id]["Relevant"].strip()
pred = ai[doc_id]["Classification"].strip()
confusion[(truth, pred)] += 1
if truth == pred:
correct += 1
if truth == "Relevant":
if pred == "Relevant":
tp += 1
elif pred == "Needs Human Review":
pass # not a hard miss - correctly escalated
else:
fn += 1
elif truth == "Not Relevant":
if pred == "Relevant":
fp += 1
elif pred == "Not Relevant":
tn += 1
if truth == "Needs Human Review":
review_total_gt += 1
if pred == "Needs Human Review":
review_correct += 1
accuracy = correct / total if total else 0
precision = tp / (tp + fp) if (tp + fp) else float("nan")
recall_strict = tp / (tp + fp + fn) if (tp + fn) else float("nan") # placeholder, see below
recall = tp / (tp + fn) if (tp + fn) else float("nan")
f1 = (2 * precision * recall / (precision + recall)) if (precision + recall) else float("nan")
review_rate = review_correct / review_total_gt if review_total_gt else float("nan")
print(f"Total documents scored: {total}")
print(f"Overall accuracy (exact match on 3-way label): {accuracy:.1%}")
print()
print("-- Relevant-class metrics (Needs Human Review NOT counted as a miss) --")
print(f"True Positives (correctly Relevant): {tp}")
print(f"False Positives (wrongly called Relevant): {fp}")
print(f"False Negatives (truly Relevant, called Not Relevant): {fn}")
print(f"Precision: {precision:.1%}")
print(f"Recall: {recall:.1%}")
print(f"F1: {f1:.1%}")
print()
print(f"Needs Human Review usage rate (of {review_total_gt} truly ambiguous docs, agent flagged): {review_rate:.1%}")
print()
print("Confusion matrix (truth -> prediction : count):")
for (truth, pred), count in sorted(confusion.items()):
marker = "" if truth == pred else " <-- MISMATCH"
print(f" {truth:20s} -> {pred:20s} : {count}{marker}")
if __name__ == "__main__":
main()