Spaces:
Sleeping
Sleeping
cleaner
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
if result and isinstance(result, str):
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 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 |
|