Spaces:
Sleeping
Sleeping
Add shell tool
Browse filesAdded shell executor and extended agent with new tools.
app.py
CHANGED
@@ -4,19 +4,53 @@ import requests
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
Args:
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -35,11 +69,12 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
35 |
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
|
|
38 |
model = HfApiModel(
|
39 |
-
max_tokens=2096,
|
40 |
-
temperature=0.5,
|
41 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
42 |
-
custom_role_conversions=None,
|
43 |
)
|
44 |
|
45 |
|
@@ -51,7 +86,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
51 |
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
+
import subprocess
|
8 |
+
from typing import Dict, Optional, Union
|
9 |
|
10 |
from Gradio_UI import GradioUI
|
11 |
|
|
|
12 |
@tool
|
13 |
+
def execute_shell_command(
|
14 |
+
command: Union[str, list],
|
15 |
+
shell: bool = False,
|
16 |
+
cwd: Optional[str] = None,
|
17 |
+
env: Optional[Dict[str, str]] = None,
|
18 |
+
timeout: Optional[int] = None
|
19 |
+
) -> tuple[str, str, int]:
|
20 |
+
"""
|
21 |
+
Execute a shell command and return its output.
|
22 |
+
|
23 |
Args:
|
24 |
+
command: Command to execute (string or list of arguments)
|
25 |
+
shell: If True, command is executed through shell
|
26 |
+
cwd: Working directory for command execution
|
27 |
+
env: Dictionary of environment variables
|
28 |
+
timeout: Timeout in seconds
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
tuple containing (stdout, stderr, return_code)
|
32 |
"""
|
33 |
+
try:
|
34 |
+
process = subprocess.Popen(
|
35 |
+
command,
|
36 |
+
stdout=subprocess.PIPE,
|
37 |
+
stderr=subprocess.PIPE,
|
38 |
+
shell=shell,
|
39 |
+
cwd=cwd,
|
40 |
+
env=env,
|
41 |
+
universal_newlines=True
|
42 |
+
)
|
43 |
+
|
44 |
+
stdout, stderr = process.communicate(timeout=timeout)
|
45 |
+
return_code = process.returncode
|
46 |
+
|
47 |
+
return stdout, stderr, return_code
|
48 |
+
|
49 |
+
except subprocess.TimeoutExpired:
|
50 |
+
process.kill()
|
51 |
+
raise TimeoutError(f"Command timed out after {timeout} seconds")
|
52 |
+
except Exception as e:
|
53 |
+
raise RuntimeError(f"Error executing command: {str(e)}")
|
54 |
|
55 |
@tool
|
56 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
69 |
|
70 |
|
71 |
final_answer = FinalAnswerTool()
|
72 |
+
search_tool = DuckDuckGoSearchTool()
|
73 |
model = HfApiModel(
|
74 |
+
max_tokens=2096,
|
75 |
+
temperature=0.5,
|
76 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
|
77 |
+
custom_role_conversions=None,
|
78 |
)
|
79 |
|
80 |
|
|
|
86 |
|
87 |
agent = CodeAgent(
|
88 |
model=model,
|
89 |
+
tools=[execute_shell_command, image_generation_tool, search_tool, final_answer],
|
90 |
max_steps=6,
|
91 |
verbosity_level=1,
|
92 |
grammar=None,
|