Spaces:
Sleeping
Sleeping
post-processing step
Browse files
app.py
CHANGED
|
@@ -20,6 +20,7 @@ from tools import (
|
|
| 20 |
file_type_detection_tool,
|
| 21 |
read_file_tool
|
| 22 |
)
|
|
|
|
| 23 |
|
| 24 |
# (Keep Constants as is)
|
| 25 |
# --- Constants ---
|
|
@@ -104,6 +105,26 @@ def process_question_with_files(question_data: dict) -> str:
|
|
| 104 |
return f"{question_text}\n\n[Note: Error processing attached file {file_name}: {str(e)}]"
|
| 105 |
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
class AgentWrapper:
|
| 108 |
def __init__(self):
|
| 109 |
print("AgentWrapper initialized.")
|
|
@@ -122,8 +143,9 @@ class AgentWrapper:
|
|
| 122 |
{"messages": [HumanMessage(content=question_text)]})
|
| 123 |
last_message = result["messages"][-1]
|
| 124 |
answer = last_message.content
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
print(f"Error in agent processing: {e}")
|
| 129 |
return f"Error processing question: {e}"
|
|
|
|
| 20 |
file_type_detection_tool,
|
| 21 |
read_file_tool
|
| 22 |
)
|
| 23 |
+
import re
|
| 24 |
|
| 25 |
# (Keep Constants as is)
|
| 26 |
# --- Constants ---
|
|
|
|
| 105 |
return f"{question_text}\n\n[Note: Error processing attached file {file_name}: {str(e)}]"
|
| 106 |
|
| 107 |
|
| 108 |
+
def extract_final_answer(text: str) -> str:
|
| 109 |
+
# Remove common prefixes
|
| 110 |
+
text = re.sub(r'(?i)(answer:|final answer:|the answer is:)', '', text)
|
| 111 |
+
# Remove repeated question lines
|
| 112 |
+
lines = [line for line in text.strip().split(
|
| 113 |
+
'\n') if not line.strip().endswith('?')]
|
| 114 |
+
# If the answer is a number at the end, return it
|
| 115 |
+
match = re.search(r'\b\d+\b$', text.strip())
|
| 116 |
+
if match:
|
| 117 |
+
return match.group(0)
|
| 118 |
+
# If the answer is a comma-separated list, return it
|
| 119 |
+
if ',' in text and len(text.split(',')) <= 10:
|
| 120 |
+
return ','.join([x.strip() for x in text.split(',') if x.strip()])
|
| 121 |
+
# Otherwise, return the last non-empty line
|
| 122 |
+
for line in reversed(lines):
|
| 123 |
+
if line.strip():
|
| 124 |
+
return line.strip()
|
| 125 |
+
return text.strip()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
class AgentWrapper:
|
| 129 |
def __init__(self):
|
| 130 |
print("AgentWrapper initialized.")
|
|
|
|
| 143 |
{"messages": [HumanMessage(content=question_text)]})
|
| 144 |
last_message = result["messages"][-1]
|
| 145 |
answer = last_message.content
|
| 146 |
+
final_answer = extract_final_answer(answer)
|
| 147 |
+
print(f"Agent returning answer: {final_answer}")
|
| 148 |
+
return final_answer
|
| 149 |
except Exception as e:
|
| 150 |
print(f"Error in agent processing: {e}")
|
| 151 |
return f"Error processing question: {e}"
|