NeoPy commited on
Commit
d68874f
·
verified ·
1 Parent(s): 475a9ce

Delete utils.py

Browse files
Files changed (1) hide show
  1. utils.py +0 -64
utils.py DELETED
@@ -1,64 +0,0 @@
1
- # type: ignore
2
- from __future__ import annotations
3
-
4
- from gradio import ChatMessage
5
- from transformers.agents import ReactCodeAgent, agent_types
6
- from typing import Generator
7
-
8
- def pull_message(step_log: dict):
9
- if step_log.get("rationale"):
10
- yield ChatMessage(
11
- role="assistant", content=step_log["rationale"]
12
- )
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(
25
- role="assistant", content=f"```\n{step_log['observation']}\n```"
26
- )
27
- if step_log.get("error"):
28
- yield ChatMessage(
29
- role="assistant",
30
- content=str(step_log["error"]),
31
- metadata={"title": "💥 Error"},
32
- )
33
-
34
- def stream_from_transformers_agent(
35
- agent: ReactCodeAgent, prompt: str
36
- ) -> Generator[ChatMessage, None, ChatMessage | None]:
37
- """Runs an agent with the given prompt and streams the messages from the agent as ChatMessages."""
38
-
39
- class Output:
40
- output: agent_types.AgentType | str = None
41
-
42
- step_log = None
43
- for step_log in agent.run(prompt, stream=True):
44
- if isinstance(step_log, dict):
45
- for message in pull_message(step_log):
46
- print("message", message)
47
- yield message
48
-
49
- Output.output = step_log
50
- if isinstance(Output.output, agent_types.AgentText):
51
- yield ChatMessage(
52
- role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```") # type: ignore
53
- elif isinstance(Output.output, agent_types.AgentImage):
54
- yield ChatMessage(
55
- role="assistant",
56
- content={"path": Output.output.to_string(), "mime_type": "image/png"}, # type: ignore
57
- )
58
- elif isinstance(Output.output, agent_types.AgentAudio):
59
- yield ChatMessage(
60
- role="assistant",
61
- content={"path": Output.output.to_string(), "mime_type": "audio/wav"}, # type: ignore
62
- )
63
- else:
64
- return ChatMessage(role="assistant", content=Output.output)