Suhasdev commited on
Commit
9a000b8
·
1 Parent(s): 4f98322

Fix OptimizedResult field access - support both property-based (real backend) and attribute-based (mock backend) result formats

Browse files
Files changed (1) hide show
  1. app.py +54 -5
app.py CHANGED
@@ -351,7 +351,23 @@ def safe_optimize(seed_prompt, dataset, model, custom_model="", max_iterations=5
351
  if not result:
352
  return False, "Optimization returned no result.", None
353
 
354
- if not hasattr(result, 'optimized_prompt'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
  return False, "Optimization result is missing required fields.", None
356
 
357
  return True, "Success", result
@@ -657,14 +673,47 @@ def run_optimization_flow(seed, dataset, model, custom_model, iter_count, call_c
657
  if not result:
658
  raise gr.Error("Optimization completed but returned no result.")
659
 
660
- if not hasattr(result, 'optimized_prompt'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661
  raise gr.Error("Optimization result is missing required fields.")
662
 
663
  # Show results
664
  try:
665
- optimized_prompt = result.optimized_prompt if result.optimized_prompt else ""
666
- improvement_metrics = result.improvement_metrics if hasattr(result, 'improvement_metrics') else {}
667
- iteration_history = result.iteration_history if hasattr(result, 'iteration_history') else []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
668
 
669
  history_text = "\n".join(iteration_history) if isinstance(iteration_history, list) else str(iteration_history)
670
 
 
351
  if not result:
352
  return False, "Optimization returned no result.", None
353
 
354
+ # Check for both property-based (real backend) and attribute-based (mock backend)
355
+ # Try to access the prompt to see if it exists (works for both attributes and properties)
356
+ has_optimized_prompt = False
357
+ try:
358
+ if hasattr(result, 'optimized_prompt'):
359
+ # Mock backend - direct attribute
360
+ has_optimized_prompt = True
361
+ elif hasattr(result, 'prompt'):
362
+ # Real backend - property-based, try to access it
363
+ _ = result.prompt
364
+ has_optimized_prompt = True
365
+ elif hasattr(result, '_result') and hasattr(result._result, 'optimized_prompt'):
366
+ has_optimized_prompt = True
367
+ except Exception:
368
+ pass
369
+
370
+ if not has_optimized_prompt:
371
  return False, "Optimization result is missing required fields.", None
372
 
373
  return True, "Success", result
 
673
  if not result:
674
  raise gr.Error("Optimization completed but returned no result.")
675
 
676
+ # Check for both property-based (real backend) and attribute-based (mock backend)
677
+ # Try to access the prompt to see if it exists (works for both attributes and properties)
678
+ has_optimized_prompt = False
679
+ try:
680
+ if hasattr(result, 'optimized_prompt'):
681
+ # Mock backend - direct attribute
682
+ has_optimized_prompt = True
683
+ elif hasattr(result, 'prompt'):
684
+ # Real backend - property-based, try to access it
685
+ _ = result.prompt
686
+ has_optimized_prompt = True
687
+ elif hasattr(result, '_result') and hasattr(result._result, 'optimized_prompt'):
688
+ has_optimized_prompt = True
689
+ except Exception:
690
+ pass
691
+
692
+ if not has_optimized_prompt:
693
  raise gr.Error("Optimization result is missing required fields.")
694
 
695
  # Show results
696
  try:
697
+ # Handle both property-based (real backend) and attribute-based (mock backend)
698
+ if hasattr(result, 'optimized_prompt'):
699
+ # Mock backend - direct attribute
700
+ optimized_prompt = result.optimized_prompt or ""
701
+ improvement_metrics = getattr(result, 'improvement_metrics', {})
702
+ iteration_history = getattr(result, 'iteration_history', [])
703
+ elif hasattr(result, 'prompt'):
704
+ # Real backend - property-based
705
+ optimized_prompt = result.prompt or ""
706
+ improvement_data = result.improvement_data if hasattr(result, 'improvement_data') else {}
707
+ # improvement_metrics might be in improvement_data or as separate field
708
+ improvement_metrics = improvement_data if isinstance(improvement_data, dict) else {}
709
+ # For iteration_history, check if it exists in improvement_data or as separate attribute
710
+ iteration_history = getattr(result, 'iteration_history', [])
711
+ if not iteration_history and isinstance(improvement_data, dict):
712
+ iteration_history = improvement_data.get('iteration_history', [])
713
+ else:
714
+ optimized_prompt = ""
715
+ improvement_metrics = {}
716
+ iteration_history = []
717
 
718
  history_text = "\n".join(iteration_history) if isinstance(iteration_history, list) else str(iteration_history)
719