Spaces:
Running
Running
File size: 6,136 Bytes
e59dbdb |
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import json
from pathlib import Path
import sqlite3
import pickle
from functools import lru_cache
import threading
import pandas as pd
class TracePreprocessor:
def __init__(self, db_path='preprocessed_traces.db'):
self.db_path = db_path
self.local = threading.local()
self.create_tables()
def get_conn(self):
if not hasattr(self.local, 'conn'):
self.local.conn = sqlite3.connect(self.db_path)
return self.local.conn
def create_tables(self):
with self.get_conn() as conn:
conn.execute('''
CREATE TABLE IF NOT EXISTS preprocessed_traces (
benchmark_name TEXT,
agent_name TEXT,
raw_logging_results BLOB,
PRIMARY KEY (benchmark_name, agent_name)
)
''')
conn.execute('''
CREATE TABLE IF NOT EXISTS failure_reports (
benchmark_name TEXT,
agent_name TEXT,
failure_report BLOB,
PRIMARY KEY (benchmark_name, agent_name)
)
''')
conn.execute('''
CREATE TABLE IF NOT EXISTS parsed_results (
benchmark_name TEXT,
agent_name TEXT,
date TEXT,
total_cost REAL,
accuracy REAL,
precision REAL,
recall REAL,
f1_score REAL,
auc REAL,
PRIMARY KEY (benchmark_name, agent_name)
)
''')
def preprocess_traces(self, processed_dir="evals_live"):
processed_dir = Path(processed_dir)
for file in processed_dir.glob('*.json'):
with open(file, 'r') as f:
data = json.load(f)
agent_name = data['config']['agent_name']
benchmark_name = data['config']['benchmark_name']
try:
raw_logging_results = pickle.dumps(data['raw_logging_results'])
with self.get_conn() as conn:
conn.execute('''
INSERT OR REPLACE INTO preprocessed_traces
(benchmark_name, agent_name, raw_logging_results)
VALUES (?, ?, ?)
''', (benchmark_name, agent_name, raw_logging_results))
except Exception as e:
print(f"Error preprocessing raw_logging_results in {file}: {e}")
try:
failure_report = pickle.dumps(data['failure_report'])
with self.get_conn() as conn:
conn.execute('''
INSERT OR REPLACE INTO failure_reports
(benchmark_name, agent_name, failure_report)
VALUES (?, ?, ?)
''', (benchmark_name, agent_name, failure_report))
except Exception as e:
print(f"Error preprocessing failure_report in {file}: {e}")
try:
config = data['config']
results = data['results']
with self.get_conn() as conn:
conn.execute('''
INSERT OR REPLACE INTO parsed_results
(benchmark_name, agent_name, date, total_cost, accuracy, precision, recall, f1_score, auc)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
benchmark_name,
agent_name,
config['date'],
results.get('total_cost'),
results.get('accuracy'),
results.get('precision'),
results.get('recall'),
results.get('f1_score'),
results.get('auc')
))
except Exception as e:
print(f"Error preprocessing parsed results in {file}: {e}")
@lru_cache(maxsize=100)
def get_analyzed_traces(self, agent_name, benchmark_name):
with self.get_conn() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT raw_logging_results FROM preprocessed_traces
WHERE benchmark_name = ? AND agent_name = ?
''', (benchmark_name, agent_name))
result = cursor.fetchone()
if result:
return pickle.loads(result[0])
return None
@lru_cache(maxsize=100)
def get_failure_report(self, agent_name, benchmark_name):
with self.get_conn() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT failure_report FROM failure_reports
WHERE benchmark_name = ? AND agent_name = ?
''', (benchmark_name, agent_name))
result = cursor.fetchone()
if result:
return pickle.loads(result[0])
return None
def get_parsed_results(self, benchmark_name):
with self.get_conn() as conn:
query = '''
SELECT * FROM parsed_results
WHERE benchmark_name = ?
ORDER BY accuracy DESC
'''
df = pd.read_sql_query(query, conn, params=(benchmark_name,))
# Round float columns to 3 decimal places
float_columns = ['total_cost', 'accuracy', 'precision', 'recall', 'f1_score', 'auc']
for column in float_columns:
if column in df.columns:
df[column] = df[column].round(3)
# Rename columns
df = df.rename(columns={
'agent_name': 'Agent Name',
'date': 'Date',
'total_cost': 'Total Cost',
'accuracy': 'Accuracy',
'precision': 'Precision',
'recall': 'Recall',
'f1_score': 'F1 Score',
'auc': 'AUC',
})
return df
if __name__ == '__main__':
preprocessor = TracePreprocessor()
preprocessor.preprocess_traces() |