TanujInsane commited on
Commit ·
8999d5b
1
Parent(s): 2dcfdb1
fix hard task categories and improve keyword matching
Browse files- baseline_inference.py +173 -85
- environment.py +5 -1
baseline_inference.py
CHANGED
|
@@ -1,98 +1,186 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
Just runs it through the test set and shows scores.
|
| 4 |
"""
|
| 5 |
|
| 6 |
-
import argparse
|
| 7 |
import json
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
args = parser.parse_args()
|
| 34 |
-
|
| 35 |
-
# Pick which tasks to run
|
| 36 |
tasks = ["easy", "medium", "hard"] if args.task == "all" else [args.task]
|
| 37 |
results = {}
|
| 38 |
-
|
| 39 |
-
print("="*70)
|
| 40 |
-
print("Baseline Agent Evaluation")
|
| 41 |
-
print("="*70)
|
| 42 |
-
|
| 43 |
-
for task in tasks:
|
| 44 |
-
print(f"\n[{task.upper()}] Running...")
|
| 45 |
-
|
| 46 |
-
# Set up grader and agent
|
| 47 |
-
grader = AgentGrader(task)
|
| 48 |
-
agent = BaselineAgent(task)
|
| 49 |
-
|
| 50 |
-
# Run it
|
| 51 |
-
score, metrics = grader.grade_agent(
|
| 52 |
-
agent.decide,
|
| 53 |
-
verbose=args.verbose
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# Store results
|
| 57 |
-
results[task] = {
|
| 58 |
-
"score": float(score),
|
| 59 |
-
"metrics": {
|
| 60 |
-
"accuracy": float(metrics["accuracy"]),
|
| 61 |
-
"total_documents": int(metrics["total_documents"]),
|
| 62 |
-
"correct": int(metrics["correct_classifications"]),
|
| 63 |
-
"average_reward": float(metrics["average_reward"]),
|
| 64 |
-
"total_reward": float(metrics["total_reward"]),
|
| 65 |
-
"avg_time_ms": float(metrics["average_processing_time_ms"]),
|
| 66 |
-
"total_time_s": float(metrics["total_time_seconds"]),
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
# Optional metrics
|
| 71 |
-
for key in ["macro_precision", "macro_recall", "macro_f1"]:
|
| 72 |
-
if key in metrics:
|
| 73 |
-
results[task]["metrics"][key] = float(metrics[key])
|
| 74 |
-
|
| 75 |
-
print(f"✓ {task.upper()} - Score: {score:.4f}")
|
| 76 |
-
|
| 77 |
-
# Summary
|
| 78 |
-
print("\n" + "="*70)
|
| 79 |
-
print("RESULTS")
|
| 80 |
-
print("="*70)
|
| 81 |
-
|
| 82 |
for task in tasks:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
with open(args.output, "w") as f:
|
| 91 |
json.dump(results, f, indent=2)
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
|
|
|
|
| 1 |
"""
|
| 2 |
+
Baseline inference script - keyword matching agent
|
|
|
|
| 3 |
"""
|
| 4 |
|
|
|
|
| 5 |
import json
|
| 6 |
+
import argparse
|
| 7 |
+
import time
|
| 8 |
+
from environment import DocumentClassificationEnv
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def keyword_agent(observation: dict, task_difficulty: str) -> int:
|
| 12 |
+
"""
|
| 13 |
+
Improved keyword-based agent with priority ordering.
|
| 14 |
+
More specific patterns checked first.
|
| 15 |
+
"""
|
| 16 |
+
content = observation["content"].lower()
|
| 17 |
+
has_urgency = observation["has_urgency_markers"][0]
|
| 18 |
+
|
| 19 |
+
if task_difficulty == "easy":
|
| 20 |
+
if any(w in content for w in ["invoice", "billing", "payment", "charge", "bill", "price", "cost"]):
|
| 21 |
+
return 1 # Billing
|
| 22 |
+
elif any(w in content for w in ["technical", "bug", "error", "software", "api", "integration", "crash"]):
|
| 23 |
+
return 3 # Technical
|
| 24 |
+
elif any(w in content for w in ["support", "help", "assist", "issue", "problem", "trouble"]):
|
| 25 |
+
return 2 # Support
|
| 26 |
+
elif any(w in content for w in ["hr", "payroll", "salary", "benefits", "employee", "complaint"]):
|
| 27 |
+
return 4 # HR
|
| 28 |
+
else:
|
| 29 |
+
return 0 # General
|
| 30 |
+
|
| 31 |
+
elif task_difficulty == "medium":
|
| 32 |
+
if any(w in content for w in ["overcharged", "dispute", "incorrect charge", "should not appear", "billing error"]):
|
| 33 |
+
return 2 # Billing-Dispute
|
| 34 |
+
elif any(w in content for w in ["invoice", "billing", "payment", "charge", "bill"]):
|
| 35 |
+
return 1 # Billing
|
| 36 |
+
elif any(w in content for w in ["bug", "crash", "error", "broken", "not working", "throws"]):
|
| 37 |
+
return 5 # Technical-Bug
|
| 38 |
+
elif any(w in content for w in ["technical", "software", "api", "integration", "documentation"]):
|
| 39 |
+
return 4 # Technical
|
| 40 |
+
elif any(w in content for w in ["paycheck", "salary", "payroll", "deductions", "payroll cycle"]):
|
| 41 |
+
return 6 # HR-Payroll
|
| 42 |
+
elif any(w in content for w in ["benefits", "health", "enroll", "eligible", "insurance"]):
|
| 43 |
+
return 7 # HR-Benefits
|
| 44 |
+
elif any(w in content for w in ["legal", "contract", "terms", "compliance", "regulation"]):
|
| 45 |
+
return 8 # Legal
|
| 46 |
+
elif any(w in content for w in ["executive", "senior manager", "management", "strategic"]):
|
| 47 |
+
return 9 # Executive
|
| 48 |
+
elif any(w in content for w in ["support", "help", "assist", "issue"]):
|
| 49 |
+
return 3 # Support
|
| 50 |
+
else:
|
| 51 |
+
return 0 # General
|
| 52 |
+
|
| 53 |
+
else: # hard: 20 categories
|
| 54 |
+
# Billing
|
| 55 |
+
if any(w in content for w in ["refund", "return", "money back", "get refunded"]):
|
| 56 |
+
return 3 # Billing-Refund
|
| 57 |
+
elif any(w in content for w in ["overcharged", "dispute", "incorrect", "should not appear", "billing error", "pricing is incorrect"]):
|
| 58 |
+
return 2 # Billing-Dispute
|
| 59 |
+
elif any(w in content for w in ["invoice", "billing", "payment", "charge", "bill", "billing address"]):
|
| 60 |
+
return 1 # Billing
|
| 61 |
+
|
| 62 |
+
# Support
|
| 63 |
+
elif has_urgency or any(w in content for w in ["urgent", "critical", "emergency", "immediate", "asap", "production", "system is down"]):
|
| 64 |
+
return 4 # Support-Urgent
|
| 65 |
+
elif any(w in content for w in ["walk me through", "how do i", "how to use", "basic feature", "functionality", "understand this"]):
|
| 66 |
+
return 5 # Support-Normal
|
| 67 |
+
elif any(w in content for w in ["support", "help", "assist", "customer support"]):
|
| 68 |
+
return 5 # Support-Normal
|
| 69 |
+
|
| 70 |
+
# Technical
|
| 71 |
+
elif any(w in content for w in ["feature request", "new feature", "add functionality", "implement", "capability", "add support for"]):
|
| 72 |
+
return 8 # Technical-Feature
|
| 73 |
+
elif any(w in content for w in ["bug", "crash", "error", "broken", "not working", "throws", "crashes", "bug report"]):
|
| 74 |
+
return 7 # Technical-Bug
|
| 75 |
+
elif any(w in content for w in ["technical", "software", "api", "integration", "documentation", "system"]):
|
| 76 |
+
return 6 # Technical
|
| 77 |
+
|
| 78 |
+
# HR
|
| 79 |
+
elif any(w in content for w in ["formal complaint", "file a complaint", "report a workplace", "filing an official"]):
|
| 80 |
+
return 11 # HR-Complaint
|
| 81 |
+
elif any(w in content for w in ["paycheck", "salary", "payroll", "deductions", "payroll cycle"]):
|
| 82 |
+
return 9 # HR-Payroll
|
| 83 |
+
elif any(w in content for w in ["benefits", "health", "enroll", "eligible", "insurance", "benefits plan"]):
|
| 84 |
+
return 10 # HR-Benefits
|
| 85 |
+
|
| 86 |
+
# Legal
|
| 87 |
+
elif any(w in content for w in ["compliance", "regulation", "regulatory", "requirement", "regulatory compliance"]):
|
| 88 |
+
return 14 # Legal-Compliance
|
| 89 |
+
elif any(w in content for w in ["contract", "signing", "provisions", "obligations", "agreement", "contract terms"]):
|
| 90 |
+
return 13 # Legal-Contract
|
| 91 |
+
elif any(w in content for w in ["legal", "advice", "documentation", "legal matters"]):
|
| 92 |
+
return 12 # Legal
|
| 93 |
+
|
| 94 |
+
# Executive
|
| 95 |
+
elif any(w in content for w in ["strategic", "partnership", "corporate strategy", "initiative", "strategic-level"]):
|
| 96 |
+
return 16 # Executive-Strategic
|
| 97 |
+
elif any(w in content for w in ["executive", "senior manager", "management", "schedule a meeting"]):
|
| 98 |
+
return 15 # Executive
|
| 99 |
+
|
| 100 |
+
# Others
|
| 101 |
+
elif any(w in content for w in ["financial", "finance", "statements", "financial records", "financial matters"]):
|
| 102 |
+
return 17 # Finance
|
| 103 |
+
elif any(w in content for w in ["marketing", "marketing partnership", "marketing proposal", "collaboration"]):
|
| 104 |
+
return 18 # Marketing
|
| 105 |
+
elif any(w in content for w in ["operational", "operations", "scheduling", "procedures", "efficiency"]):
|
| 106 |
+
return 19 # Operations
|
| 107 |
+
|
| 108 |
+
else:
|
| 109 |
+
return 0 # General
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def run_task(task_difficulty: str, num_episodes: int = 1) -> dict:
|
| 113 |
+
"""Run the agent on a specific task"""
|
| 114 |
+
env = DocumentClassificationEnv(task_difficulty=task_difficulty, seed=42)
|
| 115 |
+
|
| 116 |
+
all_results = []
|
| 117 |
+
|
| 118 |
+
for episode in range(num_episodes):
|
| 119 |
+
obs, info = env.reset(seed=episode * 100)
|
| 120 |
+
episode_rewards = []
|
| 121 |
+
episode_correct = 0
|
| 122 |
+
episode_total = 0
|
| 123 |
+
|
| 124 |
+
terminated = False
|
| 125 |
+
while not terminated:
|
| 126 |
+
action = keyword_agent(obs, task_difficulty)
|
| 127 |
+
obs, reward, terminated, truncated, info = env.step(action)
|
| 128 |
+
episode_rewards.append(reward)
|
| 129 |
+
if info.get("is_correct", False):
|
| 130 |
+
episode_correct += 1
|
| 131 |
+
episode_total += 1
|
| 132 |
+
|
| 133 |
+
episode_result = {
|
| 134 |
+
"episode": episode,
|
| 135 |
+
"total_reward": sum(episode_rewards),
|
| 136 |
+
"accuracy": episode_correct / episode_total if episode_total > 0 else 0.0,
|
| 137 |
+
"num_documents": episode_total,
|
| 138 |
+
}
|
| 139 |
+
all_results.append(episode_result)
|
| 140 |
+
|
| 141 |
+
avg_accuracy = sum(r["accuracy"] for r in all_results) / len(all_results)
|
| 142 |
+
avg_reward = sum(r["total_reward"] for r in all_results) / len(all_results)
|
| 143 |
+
|
| 144 |
+
return {
|
| 145 |
+
"task": task_difficulty,
|
| 146 |
+
"num_episodes": num_episodes,
|
| 147 |
+
"average_accuracy": avg_accuracy,
|
| 148 |
+
"average_total_reward": avg_reward,
|
| 149 |
+
"episodes": all_results,
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def main():
|
| 154 |
+
parser = argparse.ArgumentParser(description="Baseline inference for document classification")
|
| 155 |
+
parser.add_argument("--task", type=str, default="all", choices=["easy", "medium", "hard", "all"])
|
| 156 |
+
parser.add_argument("--episodes", type=int, default=1)
|
| 157 |
+
parser.add_argument("--output", type=str, default="baseline_results.json")
|
| 158 |
args = parser.parse_args()
|
| 159 |
+
|
|
|
|
| 160 |
tasks = ["easy", "medium", "hard"] if args.task == "all" else [args.task]
|
| 161 |
results = {}
|
| 162 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
for task in tasks:
|
| 164 |
+
print(f"\nRunning {task.upper()} task...")
|
| 165 |
+
start = time.time()
|
| 166 |
+
task_results = run_task(task, args.episodes)
|
| 167 |
+
elapsed = time.time() - start
|
| 168 |
+
|
| 169 |
+
score = task_results["average_accuracy"]
|
| 170 |
+
results[task] = task_results
|
| 171 |
+
results[task]["score"] = score
|
| 172 |
+
|
| 173 |
+
print(f" {task.upper()} Score: {score:.4f} ({elapsed:.1f}s)")
|
| 174 |
+
|
| 175 |
with open(args.output, "w") as f:
|
| 176 |
json.dump(results, f, indent=2)
|
| 177 |
+
|
| 178 |
+
print(f"\nResults saved to {args.output}")
|
| 179 |
+
|
| 180 |
+
print("\n=== BASELINE SCORES ===")
|
| 181 |
+
for task in tasks:
|
| 182 |
+
print(f" {task.upper()} Score: {results[task]['score']:.4f}")
|
| 183 |
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
+
main()
|
environment.py
CHANGED
|
@@ -66,7 +66,9 @@ class DocumentClassificationEnv(gym.Env):
|
|
| 66 |
16: "Executive-Strategic", # Big picture stuff
|
| 67 |
17: "Finance",
|
| 68 |
18: "Marketing",
|
| 69 |
-
19: "Operations"
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
|
@@ -334,3 +336,5 @@ if __name__ == "__main__":
|
|
| 334 |
if done:
|
| 335 |
print(f"Episode complete: {info['episode_summary']}")
|
| 336 |
break
|
|
|
|
|
|
|
|
|
| 66 |
16: "Executive-Strategic", # Big picture stuff
|
| 67 |
17: "Finance",
|
| 68 |
18: "Marketing",
|
| 69 |
+
19: "Operations",
|
| 70 |
+
20: "HR",
|
| 71 |
+
21: "Support"
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
|
|
|
| 336 |
if done:
|
| 337 |
print(f"Episode complete: {info['episode_summary']}")
|
| 338 |
break
|
| 339 |
+
|
| 340 |
+
|