Spaces:
Sleeping
Sleeping
Update backend
Browse files
main.py
CHANGED
|
@@ -215,6 +215,8 @@ def load_dataset():
|
|
| 215 |
|
| 216 |
if not dataset_path:
|
| 217 |
print("Warning: Dataset file not found. Using hardcoded examples.")
|
|
|
|
|
|
|
| 218 |
return None
|
| 219 |
|
| 220 |
try:
|
|
@@ -222,7 +224,7 @@ def load_dataset():
|
|
| 222 |
with open(dataset_path, 'r') as f:
|
| 223 |
data = json.load(f)
|
| 224 |
|
| 225 |
-
# Group examples by ground_truth label
|
| 226 |
dataset_by_label = defaultdict(list)
|
| 227 |
for item in data:
|
| 228 |
label = item.get('ground_truth', 'Unknown')
|
|
@@ -230,9 +232,13 @@ def load_dataset():
|
|
| 230 |
dataset_by_label[label].append(item)
|
| 231 |
|
| 232 |
print(f"Loaded {len(data)} examples. Examples per label: {dict((k, len(v)) for k, v in dataset_by_label.items())}")
|
|
|
|
| 233 |
return dataset_by_label
|
| 234 |
except Exception as e:
|
| 235 |
print(f"Error loading dataset: {e}")
|
|
|
|
|
|
|
|
|
|
| 236 |
return None
|
| 237 |
|
| 238 |
|
|
@@ -307,12 +313,16 @@ async def get_examples():
|
|
| 307 |
|
| 308 |
# Load dataset if not already loaded
|
| 309 |
if dataset_by_label is None:
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
examples = []
|
| 313 |
|
| 314 |
# If dataset is loaded, get random examples from each label
|
| 315 |
-
if dataset_by_label:
|
|
|
|
| 316 |
# Get one random example from each label
|
| 317 |
for label in LABEL_MAP.values():
|
| 318 |
if label in dataset_by_label and len(dataset_by_label[label]) > 0:
|
|
@@ -323,22 +333,28 @@ async def get_examples():
|
|
| 323 |
try:
|
| 324 |
api_format = convert_dataset_example_to_api_format(random_example)
|
| 325 |
|
| 326 |
-
# Create example entry
|
| 327 |
example_entry = {
|
| 328 |
"name": f"{label} Example",
|
| 329 |
"description": f"Example of {label.replace('_', ' ').title()}",
|
| 330 |
-
"data": api_format
|
|
|
|
| 331 |
}
|
| 332 |
examples.append(example_entry)
|
| 333 |
except Exception as e:
|
| 334 |
print(f"Error converting example for label {label}: {e}")
|
|
|
|
|
|
|
| 335 |
continue
|
|
|
|
| 336 |
else:
|
| 337 |
# Fallback to hardcoded examples if dataset not available
|
|
|
|
| 338 |
examples = [
|
| 339 |
{
|
| 340 |
"name": "Correct Example",
|
| 341 |
"description": "A properly formed tool call",
|
|
|
|
| 342 |
"data": {
|
| 343 |
"query": "What's the weather in New York?",
|
| 344 |
"enabled_tools": [
|
|
@@ -368,6 +384,7 @@ async def get_examples():
|
|
| 368 |
|
| 369 |
# Shuffle examples to randomize order
|
| 370 |
random.shuffle(examples)
|
|
|
|
| 371 |
|
| 372 |
return {"examples": examples}
|
| 373 |
|
|
|
|
| 215 |
|
| 216 |
if not dataset_path:
|
| 217 |
print("Warning: Dataset file not found. Using hardcoded examples.")
|
| 218 |
+
print(f"Checked paths: {possible_paths}")
|
| 219 |
+
dataset_by_label = None
|
| 220 |
return None
|
| 221 |
|
| 222 |
try:
|
|
|
|
| 224 |
with open(dataset_path, 'r') as f:
|
| 225 |
data = json.load(f)
|
| 226 |
|
| 227 |
+
# Group examples by ground_truth label - MUST declare global here!
|
| 228 |
dataset_by_label = defaultdict(list)
|
| 229 |
for item in data:
|
| 230 |
label = item.get('ground_truth', 'Unknown')
|
|
|
|
| 232 |
dataset_by_label[label].append(item)
|
| 233 |
|
| 234 |
print(f"Loaded {len(data)} examples. Examples per label: {dict((k, len(v)) for k, v in dataset_by_label.items())}")
|
| 235 |
+
print(f"Global dataset_by_label is now set: {dataset_by_label is not None}")
|
| 236 |
return dataset_by_label
|
| 237 |
except Exception as e:
|
| 238 |
print(f"Error loading dataset: {e}")
|
| 239 |
+
import traceback
|
| 240 |
+
traceback.print_exc()
|
| 241 |
+
dataset_by_label = None
|
| 242 |
return None
|
| 243 |
|
| 244 |
|
|
|
|
| 313 |
|
| 314 |
# Load dataset if not already loaded
|
| 315 |
if dataset_by_label is None:
|
| 316 |
+
print("Dataset not loaded, attempting to load...")
|
| 317 |
+
result = load_dataset()
|
| 318 |
+
if result is None:
|
| 319 |
+
print("Failed to load dataset, using fallback examples")
|
| 320 |
|
| 321 |
examples = []
|
| 322 |
|
| 323 |
# If dataset is loaded, get random examples from each label
|
| 324 |
+
if dataset_by_label and len(dataset_by_label) > 0:
|
| 325 |
+
print(f"Using dataset with {len(dataset_by_label)} label categories")
|
| 326 |
# Get one random example from each label
|
| 327 |
for label in LABEL_MAP.values():
|
| 328 |
if label in dataset_by_label and len(dataset_by_label[label]) > 0:
|
|
|
|
| 333 |
try:
|
| 334 |
api_format = convert_dataset_example_to_api_format(random_example)
|
| 335 |
|
| 336 |
+
# Create example entry with expected_output (ground_truth)
|
| 337 |
example_entry = {
|
| 338 |
"name": f"{label} Example",
|
| 339 |
"description": f"Example of {label.replace('_', ' ').title()}",
|
| 340 |
+
"data": api_format,
|
| 341 |
+
"expected_output": label # Add ground truth label
|
| 342 |
}
|
| 343 |
examples.append(example_entry)
|
| 344 |
except Exception as e:
|
| 345 |
print(f"Error converting example for label {label}: {e}")
|
| 346 |
+
import traceback
|
| 347 |
+
traceback.print_exc()
|
| 348 |
continue
|
| 349 |
+
print(f"Generated {len(examples)} random examples from dataset")
|
| 350 |
else:
|
| 351 |
# Fallback to hardcoded examples if dataset not available
|
| 352 |
+
print("Using hardcoded fallback examples")
|
| 353 |
examples = [
|
| 354 |
{
|
| 355 |
"name": "Correct Example",
|
| 356 |
"description": "A properly formed tool call",
|
| 357 |
+
"expected_output": "Correct",
|
| 358 |
"data": {
|
| 359 |
"query": "What's the weather in New York?",
|
| 360 |
"enabled_tools": [
|
|
|
|
| 384 |
|
| 385 |
# Shuffle examples to randomize order
|
| 386 |
random.shuffle(examples)
|
| 387 |
+
print(f"Returning {len(examples)} examples (shuffled)")
|
| 388 |
|
| 389 |
return {"examples": examples}
|
| 390 |
|