Kackle commited on
Commit
8e19d1a
·
verified ·
1 Parent(s): ff90411
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -471,23 +471,41 @@ class SlpMultiAgent:
471
  if result is None:
472
  return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
473
 
474
-
475
- # Extract clean answer from result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  if result and isinstance(result, str):
477
- # Look for final_answer pattern
478
- import re
479
- final_answer_match = re.search(r'final_answer\(["\']([^"\']*)["\'\)]', result) # Fixed regex
480
- if final_answer_match:
481
- clean_answer = final_answer_match.group(1)
482
- return clean_answer
483
-
484
- # If no final_answer found, try to extract the last meaningful line
485
- lines = result.strip().split('\n')
486
- for line in reversed(lines):
487
- line = line.strip()
488
- if line and not line.startswith('#') and not line.startswith('###') and len(line) < 200:
489
- return line
490
-
491
  # Return the result from the agent
492
  return result if result else "Unable to determine answer."
493
 
 
471
  if result is None:
472
  return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
473
 
474
+ # --- Robust post-processing to extract final_answer ---
475
+ import re
476
+ def strip_code_blocks(text):
477
+ # Remove triple backtick code blocks
478
+ text = re.sub(r"```[\s\S]*?```", "", text)
479
+ # Remove <code>...</code> blocks
480
+ text = re.sub(r"<code>[\s\S]*?</code>", "", text)
481
+ # Remove markdown headers and print statements
482
+ text = re.sub(r"^#+.*$", "", text, flags=re.MULTILINE)
483
+ text = re.sub(r"print\(.*?\)", "", text)
484
+ return text.strip()
485
+ def extract_final_answer(text):
486
+ # Try to find final_answer('...') or final_answer("...")
487
+ match = re.search(r"final_answer\(['\"](.*?)['\"]\)", text, re.DOTALL)
488
+ if match:
489
+ return match.group(1).strip()
490
+ # Try to find final_answer(...) with any content
491
+ match = re.search(r"final_answer\((.*?)\)", text, re.DOTALL)
492
+ if match:
493
+ return match.group(1).strip().strip("'\"")
494
+ # As a last resort, return the first non-empty line
495
+ for line in text.splitlines():
496
+ if line.strip():
497
+ return line.strip()
498
+ return text.strip()
499
+ # Post-process the result to remove code blocks and extract the answer
500
  if result and isinstance(result, str):
501
+ cleaned = strip_code_blocks(result)
502
+ answer = extract_final_answer(cleaned)
503
+ if answer:
504
+ return answer
505
+ # Fallback: try to extract from the original result
506
+ answer = extract_final_answer(result)
507
+ if answer:
508
+ return answer
 
 
 
 
 
 
509
  # Return the result from the agent
510
  return result if result else "Unable to determine answer."
511