Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse filescompatibility issues fixed
- Gradio_UI.py +7 -14
Gradio_UI.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding=utf-8
|
| 3 |
-
import mimetypes
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
-
import shutil
|
| 7 |
from typing import Optional, List
|
| 8 |
from smolagents.agent_types import AgentText, handle_agent_output_types
|
| 9 |
from smolagents.agents import ActionStep, MultiStepAgent
|
|
@@ -33,7 +31,7 @@ def pull_messages_from_step(step_log: MemoryStep):
|
|
| 33 |
# Format tool arguments cleanly
|
| 34 |
args = tool_call.arguments
|
| 35 |
if isinstance(args, dict):
|
| 36 |
-
content = "\n".join(f"• {k}: {v}" for k, v in args.items() if v)
|
| 37 |
else:
|
| 38 |
content = str(args).strip()
|
| 39 |
|
|
@@ -93,13 +91,11 @@ def stream_to_gradio(
|
|
| 93 |
raise ModuleNotFoundError("Install gradio: `pip install 'smolagents[gradio]'`")
|
| 94 |
|
| 95 |
import gradio as gr
|
| 96 |
-
total_tokens = 0
|
| 97 |
|
| 98 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 99 |
if isinstance(step_log, ActionStep):
|
| 100 |
# Track tokens if available
|
| 101 |
if hasattr(agent.model, "last_input_token_count") and hasattr(agent.model, "last_output_token_count"):
|
| 102 |
-
total_tokens += agent.model.last_input_token_count + agent.model.last_output_token_count
|
| 103 |
step_log.input_token_count = agent.model.last_input_token_count
|
| 104 |
step_log.output_token_count = agent.model.last_output_token_count
|
| 105 |
|
|
@@ -110,15 +106,12 @@ def stream_to_gradio(
|
|
| 110 |
final = handle_agent_output_types(step_log)
|
| 111 |
if isinstance(final, AgentText):
|
| 112 |
content = final.to_string()
|
| 113 |
-
# Format catalogue nicely
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
)
|
| 120 |
-
else:
|
| 121 |
-
yield gr.ChatMessage(role="assistant", content=f"**Final Answer:**\n{content}")
|
| 122 |
else:
|
| 123 |
yield gr.ChatMessage(role="assistant", content=f"**Final Answer:** {str(final)}")
|
| 124 |
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# coding=utf-8
|
|
|
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
| 5 |
from typing import Optional, List
|
| 6 |
from smolagents.agent_types import AgentText, handle_agent_output_types
|
| 7 |
from smolagents.agents import ActionStep, MultiStepAgent
|
|
|
|
| 31 |
# Format tool arguments cleanly
|
| 32 |
args = tool_call.arguments
|
| 33 |
if isinstance(args, dict):
|
| 34 |
+
content = "\n".join(f"• {k}: {v}" for k, v in args.items() if v and k != 'self')
|
| 35 |
else:
|
| 36 |
content = str(args).strip()
|
| 37 |
|
|
|
|
| 91 |
raise ModuleNotFoundError("Install gradio: `pip install 'smolagents[gradio]'`")
|
| 92 |
|
| 93 |
import gradio as gr
|
|
|
|
| 94 |
|
| 95 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, additional_args=additional_args):
|
| 96 |
if isinstance(step_log, ActionStep):
|
| 97 |
# Track tokens if available
|
| 98 |
if hasattr(agent.model, "last_input_token_count") and hasattr(agent.model, "last_output_token_count"):
|
|
|
|
| 99 |
step_log.input_token_count = agent.model.last_input_token_count
|
| 100 |
step_log.output_token_count = agent.model.last_output_token_count
|
| 101 |
|
|
|
|
| 106 |
final = handle_agent_output_types(step_log)
|
| 107 |
if isinstance(final, AgentText):
|
| 108 |
content = final.to_string()
|
| 109 |
+
# Format catalogue nicely with Markdown rendering
|
| 110 |
+
yield gr.ChatMessage(
|
| 111 |
+
role="assistant",
|
| 112 |
+
content=content,
|
| 113 |
+
metadata={"react": True}
|
| 114 |
+
)
|
|
|
|
|
|
|
|
|
|
| 115 |
else:
|
| 116 |
yield gr.ChatMessage(role="assistant", content=f"**Final Answer:** {str(final)}")
|
| 117 |
|