Spaces:
Sleeping
Sleeping
Update streaming.py
Browse files- streaming.py +64 -0
streaming.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.agents.agent_types import AgentAudio, AgentImage, AgentText, AgentType
|
| 2 |
+
from transformers.agents import ReactAgent
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def pull_message(step_log: dict):
|
| 6 |
+
try:
|
| 7 |
+
from gradio import ChatMessage
|
| 8 |
+
except ImportError:
|
| 9 |
+
raise ImportError("Gradio should be installed in order to launch a gradio demo.")
|
| 10 |
+
|
| 11 |
+
if step_log.get("rationale"):
|
| 12 |
+
yield ChatMessage(role="assistant", content=step_log["rationale"])
|
| 13 |
+
if step_log.get("tool_call"):
|
| 14 |
+
used_code = step_log["tool_call"]["tool_name"] == "code interpreter"
|
| 15 |
+
content = step_log["tool_call"]["tool_arguments"]
|
| 16 |
+
if used_code:
|
| 17 |
+
content = f"```py\n{content}\n```"
|
| 18 |
+
yield ChatMessage(
|
| 19 |
+
role="assistant",
|
| 20 |
+
metadata={"title": f"🛠️ Used tool {step_log['tool_call']['tool_name']}"},
|
| 21 |
+
content=content,
|
| 22 |
+
)
|
| 23 |
+
if step_log.get("observation"):
|
| 24 |
+
yield ChatMessage(role="assistant", content=f"```\n{step_log['observation']}\n```")
|
| 25 |
+
if step_log.get("error"):
|
| 26 |
+
yield ChatMessage(
|
| 27 |
+
role="assistant",
|
| 28 |
+
content=str(step_log["error"]),
|
| 29 |
+
metadata={"title": "💥 Error"},
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def stream_to_gradio(agent: ReactAgent, task: str, **kwargs):
|
| 34 |
+
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
from gradio import ChatMessage
|
| 38 |
+
except ImportError:
|
| 39 |
+
raise ImportError("Gradio should be installed in order to launch a gradio demo.")
|
| 40 |
+
|
| 41 |
+
class Output:
|
| 42 |
+
output: AgentType | str = None
|
| 43 |
+
|
| 44 |
+
for step_log in agent.run(task, stream=True, **kwargs):
|
| 45 |
+
if isinstance(step_log, dict):
|
| 46 |
+
for message in pull_message(step_log):
|
| 47 |
+
print("message", message)
|
| 48 |
+
yield message
|
| 49 |
+
|
| 50 |
+
Output.output = step_log
|
| 51 |
+
if isinstance(Output.output, AgentText):
|
| 52 |
+
yield ChatMessage(role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```")
|
| 53 |
+
elif isinstance(Output.output, AgentImage):
|
| 54 |
+
yield ChatMessage(
|
| 55 |
+
role="assistant",
|
| 56 |
+
content={"path": Output.output.to_string(), "mime_type": "image/png"},
|
| 57 |
+
)
|
| 58 |
+
elif isinstance(Output.output, AgentAudio):
|
| 59 |
+
yield ChatMessage(
|
| 60 |
+
role="assistant",
|
| 61 |
+
content={"path": Output.output.to_string(), "mime_type": "audio/wav"},
|
| 62 |
+
)
|
| 63 |
+
else:
|
| 64 |
+
yield ChatMessage(role="assistant", content=Output.output)
|