Spaces:
Running
on
A10G
Running
on
A10G
IsaacGHX
commited on
Commit
Β·
37003e0
1
Parent(s):
d12a6df
update
Browse files
app.py
CHANGED
|
@@ -36,6 +36,14 @@ DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
|
| 36 |
global QUERY_ID
|
| 37 |
QUERY_ID = None
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Enable scheduler to record data to HuggingFace dataset
|
| 40 |
# scheduler = None
|
| 41 |
scheduler = CommitScheduler(
|
|
@@ -266,13 +274,20 @@ class Solver:
|
|
| 266 |
|
| 267 |
# [Step 4] Query Analysis
|
| 268 |
query_analysis = self.planner.analyze_query(user_query, img_path)
|
| 269 |
-
json_data["query_analysis"] = query_analysis
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
messages.append(ChatMessage(role="assistant",
|
| 275 |
-
content=f"{
|
| 276 |
metadata={"title": "### π Step 0: Query Analysis"}))
|
| 277 |
yield messages
|
| 278 |
|
|
@@ -297,7 +312,7 @@ class Solver:
|
|
| 297 |
next_step = self.planner.generate_next_step(
|
| 298 |
user_query, img_path, query_analysis, self.memory, step_count, self.max_steps, json_data
|
| 299 |
)
|
| 300 |
-
context, sub_goal, tool_name = self.planner.extract_context_subgoal_and_tool(next_step)
|
| 301 |
step_data = {
|
| 302 |
"step_count": step_count,
|
| 303 |
"context": context,
|
|
@@ -308,17 +323,27 @@ class Solver:
|
|
| 308 |
save_module_data(QUERY_ID, f"step_{step_count}_action_prediction", step_data)
|
| 309 |
|
| 310 |
# Display the step information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
messages.append(ChatMessage(
|
| 312 |
role="assistant",
|
| 313 |
-
content=f"**Context:** {
|
| 314 |
-
metadata={"title": f"### π― Step {step_count}: Action Prediction ({
|
| 315 |
yield messages
|
| 316 |
|
| 317 |
# Handle tool execution or errors
|
| 318 |
if tool_name not in self.planner.available_tools:
|
|
|
|
| 319 |
messages.append(ChatMessage(
|
| 320 |
-
role="assistant",
|
| 321 |
-
content=f"β οΈ Error: Tool '{
|
| 322 |
yield messages
|
| 323 |
continue
|
| 324 |
|
|
@@ -331,10 +356,11 @@ class Solver:
|
|
| 331 |
result = make_json_serializable_truncated(result)
|
| 332 |
|
| 333 |
# Display the ommand generation information
|
|
|
|
| 334 |
messages.append(ChatMessage(
|
| 335 |
role="assistant",
|
| 336 |
content=f"**Command:**\n```python\n{command}\n```",
|
| 337 |
-
metadata={"title": f"### π Step {step_count}: Command Generation ({
|
| 338 |
yield messages
|
| 339 |
|
| 340 |
# Save the command generation data
|
|
@@ -347,11 +373,18 @@ class Solver:
|
|
| 347 |
save_module_data(QUERY_ID, f"step_{step_count}_command_generation", command_generation_data)
|
| 348 |
|
| 349 |
# Display the command execution result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
messages.append(ChatMessage(
|
| 351 |
role="assistant",
|
| 352 |
-
content=f"**Result:**\n```json\n{
|
| 353 |
# content=f"**Result:**\n```json\n{result}\n```",
|
| 354 |
-
metadata={"title": f"### β‘ Step {step_count}: Command Execution ({
|
| 355 |
yield messages
|
| 356 |
|
| 357 |
# Save the command execution data
|
|
@@ -362,7 +395,7 @@ class Solver:
|
|
| 362 |
save_module_data(QUERY_ID, f"step_{step_count}_command_execution", command_execution_data)
|
| 363 |
|
| 364 |
# [Step 8] Memory update and stopping condition
|
| 365 |
-
self.memory.add_action(step_count, tool_name, sub_goal, command, result)
|
| 366 |
stop_verification = self.planner.verificate_context(user_query, img_path, query_analysis, self.memory, step_count, json_data)
|
| 367 |
context_verification, conclusion = self.planner.extract_conclusion(stop_verification)
|
| 368 |
|
|
@@ -372,13 +405,18 @@ class Solver:
|
|
| 372 |
"conclusion": conclusion,
|
| 373 |
"time": round(time.time() - start_time, 5)
|
| 374 |
}
|
| 375 |
-
save_module_data(QUERY_ID, f"step_{step_count}_context_verification", context_verification_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
-
# Display the context verification result
|
| 378 |
conclusion_emoji = "β
" if conclusion == 'STOP' else "π"
|
| 379 |
messages.append(ChatMessage(
|
| 380 |
role="assistant",
|
| 381 |
-
content=f"**Analysis:**\n{
|
| 382 |
metadata={"title": f"### π€ Step {step_count}: Context Verification"}))
|
| 383 |
yield messages
|
| 384 |
|
|
@@ -388,8 +426,14 @@ class Solver:
|
|
| 388 |
# Step 7: Generate Final Output (if needed)
|
| 389 |
if 'direct' in self.output_types:
|
| 390 |
messages.append(ChatMessage(role="assistant", content="<br>"))
|
| 391 |
-
direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory)
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
yield messages
|
| 394 |
|
| 395 |
# Save the direct output data
|
|
@@ -545,7 +589,9 @@ def main(args):
|
|
| 545 |
[Paper](https://arxiv.org/abs/2510.05592) |
|
| 546 |
[GitHub](https://github.com/lupantech/AgentFlow) |
|
| 547 |
|
| 548 |
-
> β³ **Note:** The first query may take ~20 seconds to initialize AgentFlow. Subsequent queries will be
|
|
|
|
|
|
|
| 549 |
""")
|
| 550 |
|
| 551 |
with gr.Row():
|
|
|
|
| 36 |
global QUERY_ID
|
| 37 |
QUERY_ID = None
|
| 38 |
|
| 39 |
+
TOOL_NAME_MAPPING = {
|
| 40 |
+
"Generalist_Solution_Generator_Tool": "Base_Generator_Tool",
|
| 41 |
+
"Ground_Google_Search_Tool": "Google_Search_Tool",
|
| 42 |
+
"Python_Code_Generator_Tool": "Python_Coder_Tool",
|
| 43 |
+
"Web_RAG_Search_Tool": "Web_Search_Tool",
|
| 44 |
+
"Wikipedia_RAG_Search_Tool": "Wikipedia_Search_Tool"
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
# Enable scheduler to record data to HuggingFace dataset
|
| 48 |
# scheduler = None
|
| 49 |
scheduler = CommitScheduler(
|
|
|
|
| 274 |
|
| 275 |
# [Step 4] Query Analysis
|
| 276 |
query_analysis = self.planner.analyze_query(user_query, img_path)
|
| 277 |
+
json_data["query_analysis"] = query_analysis # TODO: update
|
| 278 |
+
|
| 279 |
+
# Format the query analysis for display
|
| 280 |
+
query_analysis_display = query_analysis.replace("Concise Summary:", "**Concise Summary:**\n")
|
| 281 |
+
query_analysis_display = query_analysis_display.replace("Required Skills:", "**Required Skills:**")
|
| 282 |
+
query_analysis_display = query_analysis_display.replace("Relevant Tools:", "**Relevant Tools:**")
|
| 283 |
+
query_analysis_display = query_analysis_display.replace("Additional Considerations:", "**Additional Considerations:**")
|
| 284 |
+
|
| 285 |
+
# Map tool names in query analysis for display
|
| 286 |
+
for original_name, display_name in TOOL_NAME_MAPPING.items():
|
| 287 |
+
query_analysis_display = query_analysis_display.replace(original_name, display_name)
|
| 288 |
+
|
| 289 |
messages.append(ChatMessage(role="assistant",
|
| 290 |
+
content=f"{query_analysis_display}",
|
| 291 |
metadata={"title": "### π Step 0: Query Analysis"}))
|
| 292 |
yield messages
|
| 293 |
|
|
|
|
| 312 |
next_step = self.planner.generate_next_step(
|
| 313 |
user_query, img_path, query_analysis, self.memory, step_count, self.max_steps, json_data
|
| 314 |
)
|
| 315 |
+
context, sub_goal, tool_name = self.planner.extract_context_subgoal_and_tool(next_step) # TODO: update
|
| 316 |
step_data = {
|
| 317 |
"step_count": step_count,
|
| 318 |
"context": context,
|
|
|
|
| 323 |
save_module_data(QUERY_ID, f"step_{step_count}_action_prediction", step_data)
|
| 324 |
|
| 325 |
# Display the step information
|
| 326 |
+
display_tool_name = TOOL_NAME_MAPPING.get(tool_name, tool_name)
|
| 327 |
+
|
| 328 |
+
# Map tool names in context and sub_goal for display
|
| 329 |
+
context_display = context if context else ""
|
| 330 |
+
sub_goal_display = sub_goal if sub_goal else ""
|
| 331 |
+
for original_name, display_name in TOOL_NAME_MAPPING.items():
|
| 332 |
+
context_display = context_display.replace(original_name, display_name)
|
| 333 |
+
sub_goal_display = sub_goal_display.replace(original_name, display_name)
|
| 334 |
+
|
| 335 |
messages.append(ChatMessage(
|
| 336 |
role="assistant",
|
| 337 |
+
content=f"**Context:** {context_display}\n\n**Sub-goal:** {sub_goal_display}\n\n**Tool:** `{display_tool_name}`",
|
| 338 |
+
metadata={"title": f"### π― Step {step_count}: Action Prediction ({display_tool_name})"}))
|
| 339 |
yield messages
|
| 340 |
|
| 341 |
# Handle tool execution or errors
|
| 342 |
if tool_name not in self.planner.available_tools:
|
| 343 |
+
display_tool_name = TOOL_NAME_MAPPING.get(tool_name, tool_name)
|
| 344 |
messages.append(ChatMessage(
|
| 345 |
+
role="assistant",
|
| 346 |
+
content=f"β οΈ Error: Tool '{display_tool_name}' is not available."))
|
| 347 |
yield messages
|
| 348 |
continue
|
| 349 |
|
|
|
|
| 356 |
result = make_json_serializable_truncated(result)
|
| 357 |
|
| 358 |
# Display the ommand generation information
|
| 359 |
+
display_tool_name = TOOL_NAME_MAPPING.get(tool_name, tool_name)
|
| 360 |
messages.append(ChatMessage(
|
| 361 |
role="assistant",
|
| 362 |
content=f"**Command:**\n```python\n{command}\n```",
|
| 363 |
+
metadata={"title": f"### π Step {step_count}: Command Generation ({display_tool_name})"}))
|
| 364 |
yield messages
|
| 365 |
|
| 366 |
# Save the command generation data
|
|
|
|
| 373 |
save_module_data(QUERY_ID, f"step_{step_count}_command_generation", command_generation_data)
|
| 374 |
|
| 375 |
# Display the command execution result
|
| 376 |
+
display_tool_name = TOOL_NAME_MAPPING.get(tool_name, tool_name)
|
| 377 |
+
|
| 378 |
+
# Map tool names in result for display
|
| 379 |
+
result_json_str = json.dumps(result, indent=4)
|
| 380 |
+
for original_name, display_name in TOOL_NAME_MAPPING.items():
|
| 381 |
+
result_json_str = result_json_str.replace(original_name, display_name)
|
| 382 |
+
|
| 383 |
messages.append(ChatMessage(
|
| 384 |
role="assistant",
|
| 385 |
+
content=f"**Result:**\n```json\n{result_json_str}\n```",
|
| 386 |
# content=f"**Result:**\n```json\n{result}\n```",
|
| 387 |
+
metadata={"title": f"### β‘ Step {step_count}: Command Execution ({display_tool_name})"}))
|
| 388 |
yield messages
|
| 389 |
|
| 390 |
# Save the command execution data
|
|
|
|
| 395 |
save_module_data(QUERY_ID, f"step_{step_count}_command_execution", command_execution_data)
|
| 396 |
|
| 397 |
# [Step 8] Memory update and stopping condition
|
| 398 |
+
self.memory.add_action(step_count, tool_name, sub_goal, command, result) # TODO: do not update here
|
| 399 |
stop_verification = self.planner.verificate_context(user_query, img_path, query_analysis, self.memory, step_count, json_data)
|
| 400 |
context_verification, conclusion = self.planner.extract_conclusion(stop_verification)
|
| 401 |
|
|
|
|
| 405 |
"conclusion": conclusion,
|
| 406 |
"time": round(time.time() - start_time, 5)
|
| 407 |
}
|
| 408 |
+
save_module_data(QUERY_ID, f"step_{step_count}_context_verification", context_verification_data)
|
| 409 |
+
|
| 410 |
+
# Display the context verification result # TODO: update context_verification
|
| 411 |
+
# Map tool names in context verification for display
|
| 412 |
+
context_verification_display = context_verification if context_verification else ""
|
| 413 |
+
for original_name, display_name in TOOL_NAME_MAPPING.items():
|
| 414 |
+
context_verification_display = context_verification_display.replace(original_name, display_name)
|
| 415 |
|
|
|
|
| 416 |
conclusion_emoji = "β
" if conclusion == 'STOP' else "π"
|
| 417 |
messages.append(ChatMessage(
|
| 418 |
role="assistant",
|
| 419 |
+
content=f"**Analysis:**\n{context_verification_display}\n\n**Conclusion:** `{conclusion}` {conclusion_emoji}",
|
| 420 |
metadata={"title": f"### π€ Step {step_count}: Context Verification"}))
|
| 421 |
yield messages
|
| 422 |
|
|
|
|
| 426 |
# Step 7: Generate Final Output (if needed)
|
| 427 |
if 'direct' in self.output_types:
|
| 428 |
messages.append(ChatMessage(role="assistant", content="<br>"))
|
| 429 |
+
direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory) # TODO: update
|
| 430 |
+
|
| 431 |
+
# Map tool names in direct output for display
|
| 432 |
+
direct_output_display = direct_output if direct_output else ""
|
| 433 |
+
for original_name, display_name in TOOL_NAME_MAPPING.items():
|
| 434 |
+
direct_output_display = direct_output_display.replace(original_name, display_name)
|
| 435 |
+
|
| 436 |
+
messages.append(ChatMessage(role="assistant", content=f"### π Final Answer:\n{direct_output_display}"))
|
| 437 |
yield messages
|
| 438 |
|
| 439 |
# Save the direct output data
|
|
|
|
| 589 |
[Paper](https://arxiv.org/abs/2510.05592) |
|
| 590 |
[GitHub](https://github.com/lupantech/AgentFlow) |
|
| 591 |
|
| 592 |
+
> β³ **Note:** The first query may take ~20 seconds to initialize AgentFlow (due to heavy traffic). Subsequent queries will be super fast.
|
| 593 |
+
>
|
| 594 |
+
> π‘ **Tip:** If the wait time is too long, please try again later.
|
| 595 |
""")
|
| 596 |
|
| 597 |
with gr.Row():
|