-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_evaluation.py
More file actions
273 lines (239 loc) · 12 KB
/
Copy pathrun_evaluation.py
File metadata and controls
273 lines (239 loc) · 12 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import ast
import json
from pathlib import Path
from typing import Any
import pandas as pd
import streamlit as st
st.set_page_config(page_title="Dataset Importer Pipeline", layout="centered")
st.title("📦 Dataset Importer & Mapping Pipeline")
st.write(
"Upload any custom CSV or JSON dataset, map your columns to the evaluation framework features, and export a compatible `sample.jsonl` file instantly."
)
# --- File Upload Section ---
uploaded_file = st.file_uploader(
"Upload your dataset (CSV, JSON, or JSONL)", type=["csv", "json", "jsonl"]
)
if uploaded_file is not None:
# 1. Safely load the data into a Pandas DataFrame for mapping
try:
file_extension = Path(uploaded_file.name).suffix.lower()
if file_extension == ".csv":
df = pd.read_csv(uploaded_file)
elif file_extension == ".jsonl":
df = pd.read_json(uploaded_file, lines=True)
else:
# Standard JSON
df = pd.read_json(uploaded_file)
st.success(
f"Successfully loaded '{uploaded_file.name}' with {len(df)} records!"
)
# Preview raw structure
with st.expander("🔍 Preview Uploaded Raw Data"):
st.dataframe(df.head(3))
except Exception as e:
st.error(f"Error parsing file: {e}")
st.stop()
st.markdown("---")
st.subheader("🎯 Map Columns to Framework Fields")
# Available columns from user's dataset
columns = ["-- Select Column --"] + list(df.columns)
# Grid UI layout for mappings
col1, col2 = st.columns(2)
with col1:
id_col = st.selectbox("🔑 Unique Row Identifier (id):", options=columns)
keyword_col = st.selectbox("🔎 Search Query Term (keyword):", options=columns)
relevance_col = st.selectbox(
"📊 Ground-Truth Target Label (relevance_score):", options=columns
)
baseline_col = st.selectbox(
"📉 Baseline Score Option (baseline_relevance_score):", options=columns
)
entity_col = st.selectbox(
"🏷️ Category/Filter String (entity_type):", options=columns
)
with col2:
st.info(
"**🧩 Nested Metadata Block (`json_data` mapping)**\n\nSelect the structural text fields to combine inside the evaluator block:"
)
meta_name = st.selectbox("📌 Context Item Name:", options=columns)
meta_desc = st.selectbox("📝 Context Item Description:", options=columns)
meta_tags_cat = st.selectbox(
"📂 Category Tags Column (Array or Comma Separated):", options=columns
)
meta_tags_top = st.selectbox(
"💬 Topic Tags Column (Array or Comma Separated):", options=columns
)
meta_sponsors = st.selectbox("🤝 Sponsors Column:", options=columns)
st.markdown("---")
# --- Sample Downsampling Range Slider ---
st.subheader("⚙️ Downsample Preferences")
max_sample = len(df) if len(df) > 15 else 15
default_sample = len(df) if len(df) < 50 else 50
sample_size = st.slider(
"Select maximum rows to extract for evaluation:",
min_value=10,
max_value=max_sample,
value=default_sample,
)
# --- Processing Action Button ---
if st.button("🚀 Process, Map and Save Dataset", type="primary"):
if (
keyword_col == "-- Select Column --"
or relevance_col == "-- Select Column --"
):
st.error(
"❌ 'keyword' and 'relevance_score' mappings are absolutely required by the framework evaluation loop."
)
else:
with st.spinner("Refactoring dataset structure..."):
if len(df) > sample_size:
sampled_df = df.sample(n=sample_size, random_state=42).copy()
else:
sampled_df = df.copy()
records: list[dict[str, Any]] = sampled_df.to_dict(orient="records")
output_rows = []
for idx, row in enumerate(records, start=1):
# Core normalizer to safely extract either standalone values OR nested dictionary sub-keys
# without repeating or mapping fields recursively
def extract_field(col_name: str, subkey: str) -> Any:
if col_name == "-- Select Column --":
return None
val = row.get(col_name)
if val is None or (isinstance(val, float) and val != val):
return None
parsed_obj = None
# Check if the value is a native dict or a stringified JSON/Python dictionary literal
if isinstance(val, dict):
parsed_obj = val
elif isinstance(val, str):
stripped = val.strip()
if (
stripped.startswith("{") and stripped.endswith("}")
) or (stripped.startswith("[") and stripped.endswith("]")):
try:
parsed_obj = ast.literal_eval(stripped)
except Exception:
try:
parsed_obj = json.loads(stripped)
except Exception:
pass
# LOGIC FIX: If the target column represents an entire object structure (like mapping subfields to a single 'json_data' column)
# perform an explicit key lookup. Otherwise, return the clean raw scalar column value directly.
if isinstance(parsed_obj, dict):
if subkey in parsed_obj:
target_val = parsed_obj[subkey]
# Handle deep stringified objects nested inside parsed keys cleanly
if isinstance(
target_val, str
) and target_val.strip().startswith("{"):
try:
deep_parsed = ast.literal_eval(
target_val.strip()
)
if (
isinstance(deep_parsed, dict)
and subkey in deep_parsed
):
return deep_parsed[subkey]
return deep_parsed
except Exception:
pass
return target_val
# If we explicitly mapped to a 'json_data' column but the exact subkey isn't matched,
# return None to prevent the entire row's object from being duplicated inside a single string field.
if (
col_name == "json_data"
or "name" in parsed_obj
or "description" in parsed_obj
):
return None
return val
# Clean independent text field parsing helper
def get_field_string(
col_name: str, subkey: str, default_val: str = ""
) -> str:
val = extract_field(col_name, subkey)
if val is None:
return default_val
return str(val).strip()
# Clean independent array list parsing helper
def get_field_list(col_name: str, subkey: str) -> list[str]:
val = extract_field(col_name, subkey)
if val is None:
return []
if isinstance(val, list):
return [
str(item).strip() for item in val if str(item).strip()
]
return [
item.strip() for item in str(val).split(",") if item.strip()
]
# Clean independent integer parsing helper
def get_field_int(
col_name: str, subkey: str, default_val: int = 0
) -> int:
# For direct outer fields like relevance or baseline, map directly if not mapping to 'json_data'
val = (
extract_field(col_name, subkey)
if col_name == "json_data"
else row.get(col_name)
)
if val is None or (isinstance(val, float) and val != val):
return default_val
try:
return int(float(val))
except (ValueError, TypeError):
return default_val
# Resolve metadata attributes separately using exact distinct key extractions
name_str = get_field_string(meta_name, "name")
desc_str = get_field_string(meta_desc, "description")
if desc_str == "" and name_str != "":
desc_str = name_str
# Assemble the sub-metadata block explicitly flat
json_data = {
"name": name_str,
"description": desc_str,
"sponsors": get_field_list(meta_sponsors, "sponsors"),
"tagCategories": get_field_list(meta_tags_cat, "tagCategories"),
"tagTopics": get_field_list(meta_tags_top, "tagTopics"),
}
rel_val = get_field_int(
relevance_col, "relevance_score", default_val=0
)
base_val = (
get_field_int(
baseline_col,
"baseline_relevance_score",
default_val=rel_val,
)
if baseline_col != "-- Select Column --"
else rel_val
)
framework_row = {
"id": get_field_string(
id_col, "id", default_val=f"rec_{idx:03d}"
),
"entity_type": get_field_string(
entity_col, "entity_type", default_val="episode"
),
"json_data": json_data,
"keyword": get_field_string(keyword_col, "keyword"),
"relevance_score": rel_val,
"baseline_relevance_score": base_val,
}
output_rows.append(framework_row)
# Write cleanly to target data/sample.jsonl
target_path = Path("data/sample.jsonl")
target_path.parent.mkdir(parents=True, exist_ok=True)
with open(target_path, "w", encoding="utf-8") as f:
for r in output_rows:
f.write(json.dumps(r, ensure_ascii=False) + "\n")
st.success(
f"🎉 Success! Processed records successfully saved to `{target_path}`."
)
st.info(
"💡 You can now safely run evaluations through the Dashboard or via terminal."
)
if len(output_rows) > 0:
with st.expander("👀 View Transformed File Snapshot"):
st.json(output_rows[0])