Spaces:
Sleeping
Sleeping
Simplify CustomGradioUI to display raw agent output without parsing
Browse files- Removed parse_agent_response() call and source parsing logic
- Added _format_raw_answer() to do minimal HTML formatting only
- Sources section now empty (sources are already in the answer)
- No more extraction/reformatting of the agent's output
- Agent's formatted answer displays exactly as generated
- Tools Used section still populated from agent logs
src/second_brain_online/application/ui/custom_gradio_ui.py
CHANGED
|
@@ -168,45 +168,76 @@ class CustomGradioUI:
|
|
| 168 |
result = self.agent.run(query)
|
| 169 |
|
| 170 |
# Quick post-processing steps
|
| 171 |
-
progress(0.8, desc="β¨
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
answer, sources, tools_used = self.parse_agent_response(result, agent_logs)
|
| 176 |
|
| 177 |
# Debug information
|
| 178 |
print("\n" + "="*80)
|
| 179 |
print("DEBUG: RAW AGENT RESULT")
|
| 180 |
print("="*80)
|
| 181 |
print(f"Type: {type(result)}")
|
| 182 |
-
print(f"Full Content:\n{
|
| 183 |
-
print("="*80)
|
| 184 |
-
|
| 185 |
-
print("\n" + "="*80)
|
| 186 |
-
print("DEBUG: PARSED RESULTS")
|
| 187 |
-
print("="*80)
|
| 188 |
-
print(f"Answer: {answer}")
|
| 189 |
-
print(f"Sources ({len(sources)}): {sources}")
|
| 190 |
-
print(f"Tools Used: {tools_used}")
|
| 191 |
print("="*80)
|
| 192 |
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
tools_html = self.format_tools(tools_used)
|
| 197 |
-
debug_text = str(result)
|
| 198 |
|
| 199 |
-
#
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
progress(1.0, desc="β
Complete!")
|
| 204 |
-
return answer_html, sources_html, tools_html, debug_text,
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
error_msg = f"<div style='color: #dc3545; padding: 12px; border: 1px solid #f5c6cb; border-radius: 4px; background-color: #f8d7da;'>Error: {str(e)}</div>"
|
| 208 |
return error_msg, "", "", str(e), self.load_conversations()
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
def _parse_sources_from_text(self, sources_text: str) -> List[Dict]:
|
| 211 |
"""Parse sources from the formatted text output.
|
| 212 |
|
|
|
|
| 168 |
result = self.agent.run(query)
|
| 169 |
|
| 170 |
# Quick post-processing steps
|
| 171 |
+
progress(0.8, desc="β¨ Displaying results...")
|
| 172 |
|
| 173 |
+
# Convert result to string
|
| 174 |
+
result_str = str(result)
|
|
|
|
| 175 |
|
| 176 |
# Debug information
|
| 177 |
print("\n" + "="*80)
|
| 178 |
print("DEBUG: RAW AGENT RESULT")
|
| 179 |
print("="*80)
|
| 180 |
print(f"Type: {type(result)}")
|
| 181 |
+
print(f"Full Content:\n{result_str}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
print("="*80)
|
| 183 |
|
| 184 |
+
# Extract tools used from agent logs (for Tools Used section)
|
| 185 |
+
agent_logs = getattr(self.agent, 'logs', []) if hasattr(self.agent, 'logs') else []
|
| 186 |
+
tools_used = []
|
| 187 |
+
if agent_logs:
|
| 188 |
+
for step in agent_logs:
|
| 189 |
+
if hasattr(step, 'tool_calls') and step.tool_calls:
|
| 190 |
+
for tool_call in step.tool_calls:
|
| 191 |
+
if hasattr(tool_call, 'name'):
|
| 192 |
+
tools_used.append(tool_call.name)
|
| 193 |
+
tools_used = list(set(tools_used)) # Remove duplicates
|
| 194 |
+
|
| 195 |
+
# Format the raw answer with proper HTML structure (no parsing, just formatting)
|
| 196 |
+
answer_html = self._format_raw_answer(result_str)
|
| 197 |
+
|
| 198 |
+
# Leave Sources section empty (already in the answer)
|
| 199 |
+
sources_html = ""
|
| 200 |
+
|
| 201 |
+
# Format tools
|
| 202 |
tools_html = self.format_tools(tools_used)
|
|
|
|
| 203 |
|
| 204 |
+
# Debug text
|
| 205 |
+
debug_text = result_str
|
| 206 |
+
|
| 207 |
+
# Show all conversations (no filtering since we're not parsing sources)
|
| 208 |
+
progress(0.95, desc="π Loading conversations...")
|
| 209 |
+
all_conversations = self.load_conversations()
|
| 210 |
|
| 211 |
progress(1.0, desc="β
Complete!")
|
| 212 |
+
return answer_html, sources_html, tools_html, debug_text, all_conversations
|
| 213 |
|
| 214 |
except Exception as e:
|
| 215 |
error_msg = f"<div style='color: #dc3545; padding: 12px; border: 1px solid #f5c6cb; border-radius: 4px; background-color: #f8d7da;'>Error: {str(e)}</div>"
|
| 216 |
return error_msg, "", "", str(e), self.load_conversations()
|
| 217 |
|
| 218 |
+
def _format_raw_answer(self, answer: str) -> str:
|
| 219 |
+
"""Format the raw answer with basic HTML structure without parsing.
|
| 220 |
+
|
| 221 |
+
Just converts markdown-style formatting to HTML and preserves the structure.
|
| 222 |
+
"""
|
| 223 |
+
if not answer:
|
| 224 |
+
return "<div class='answer-section'><p>No answer provided.</p></div>"
|
| 225 |
+
|
| 226 |
+
# Convert markdown bold to HTML bold
|
| 227 |
+
answer = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', answer)
|
| 228 |
+
|
| 229 |
+
# Convert line breaks to HTML
|
| 230 |
+
answer = answer.replace('\n', '<br>')
|
| 231 |
+
|
| 232 |
+
# Clean up multiple line breaks
|
| 233 |
+
answer = re.sub(r'(<br>){3,}', '<br><br>', answer)
|
| 234 |
+
|
| 235 |
+
return f"""
|
| 236 |
+
<div class='answer-section'>
|
| 237 |
+
<div style='line-height: 1.8; font-size: 16px; white-space: pre-wrap;'>{answer}</div>
|
| 238 |
+
</div>
|
| 239 |
+
"""
|
| 240 |
+
|
| 241 |
def _parse_sources_from_text(self, sources_text: str) -> List[Dict]:
|
| 242 |
"""Parse sources from the formatted text output.
|
| 243 |
|