Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,23 +28,115 @@ def handle_agent_errors(func):
|
|
28 |
st.error(f"An unexpected error occurred: {e}")
|
29 |
st.code(traceback.format_exc(), language="text")
|
30 |
return wrapper
|
31 |
-
|
32 |
# --- Abstract Base Classes ---
|
33 |
-
|
34 |
class BaseTool(ABC):
|
35 |
@abstractmethod
|
36 |
def use(self, input_str: str) -> str:
|
37 |
"""How the tool is used."""
|
38 |
pass
|
39 |
|
|
|
40 |
class BaseModel(ABC):
|
41 |
@abstractmethod
|
42 |
def generate(self, prompt: str) -> str:
|
43 |
"""How the model generates output."""
|
44 |
pass
|
45 |
|
46 |
-
# --- Concrete Tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
|
|
48 |
class MyDuckDuckGoSearchTool(BaseTool, DuckDuckGoSearchTool):
|
49 |
"""Performs a DuckDuckGo search and returns the results."""
|
50 |
|
|
|
28 |
st.error(f"An unexpected error occurred: {e}")
|
29 |
st.code(traceback.format_exc(), language="text")
|
30 |
return wrapper
|
31 |
+
|
32 |
# --- Abstract Base Classes ---
|
|
|
33 |
class BaseTool(ABC):
|
34 |
@abstractmethod
|
35 |
def use(self, input_str: str) -> str:
|
36 |
"""How the tool is used."""
|
37 |
pass
|
38 |
|
39 |
+
|
40 |
class BaseModel(ABC):
|
41 |
@abstractmethod
|
42 |
def generate(self, prompt: str) -> str:
|
43 |
"""How the model generates output."""
|
44 |
pass
|
45 |
|
46 |
+
# --- Concrete Tool Implementations ---
|
47 |
+
|
48 |
+
class WorkspaceEmulatorTool(BaseTool):
|
49 |
+
"""Simulates a development workspace."""
|
50 |
+
|
51 |
+
def __init__(self):
|
52 |
+
self.name = "WorkspaceEmulatorTool"
|
53 |
+
|
54 |
+
def use(self, input_str: str) -> str:
|
55 |
+
try:
|
56 |
+
# Parse the input to create directories and files
|
57 |
+
commands = input_str.split(';')
|
58 |
+
for cmd in commands:
|
59 |
+
cmd = cmd.strip()
|
60 |
+
if cmd.startswith('mkdir '):
|
61 |
+
dir_name = cmd[len('mkdir '):]
|
62 |
+
os.makedirs(dir_name, exist_ok=True)
|
63 |
+
print(f"Directory '{dir_name}' created.")
|
64 |
+
elif cmd.startswith('touch '):
|
65 |
+
file_name = cmd[len('touch '):]
|
66 |
+
open(file_name, 'a').close()
|
67 |
+
print(f"File '{file_name}' created.")
|
68 |
+
else:
|
69 |
+
print(f"Unknown command: {cmd}")
|
70 |
+
return "Workspace emulated successfully."
|
71 |
+
except Exception as e:
|
72 |
+
return f"Error emulating workspace: {str(e)}"
|
73 |
+
|
74 |
+
class DeploymentSimulatorTool(BaseTool):
|
75 |
+
"""Simulates deployment processes."""
|
76 |
+
|
77 |
+
def __init__(self):
|
78 |
+
self.name = "DeploymentSimulatorTool"
|
79 |
+
|
80 |
+
def use(self, input_str: str) -> str:
|
81 |
+
try:
|
82 |
+
# Simulate deployment steps
|
83 |
+
steps = input_str.split(';')
|
84 |
+
for step in steps:
|
85 |
+
step = step.strip()
|
86 |
+
if step == 'build':
|
87 |
+
print("Building the application...")
|
88 |
+
elif step == 'test':
|
89 |
+
print("Running tests...")
|
90 |
+
elif step == 'deploy':
|
91 |
+
print("Deploying to the server...")
|
92 |
+
else:
|
93 |
+
print(f"Unknown step: {step}")
|
94 |
+
return "Deployment simulated successfully."
|
95 |
+
except Exception as e:
|
96 |
+
return f"Error simulating deployment: {str(e)}"
|
97 |
+
|
98 |
+
class ErrorSpottingTool(BaseTool):
|
99 |
+
"""Spots and corrects errors in code."""
|
100 |
+
|
101 |
+
def __init__(self):
|
102 |
+
self.name = "ErrorSpottingTool"
|
103 |
+
|
104 |
+
def use(self, input_str: str) -> str:
|
105 |
+
try:
|
106 |
+
# Write the code to a temporary file
|
107 |
+
with open('temp_code.py', 'w') as f:
|
108 |
+
f.write(input_str)
|
109 |
+
|
110 |
+
# Run pylint to check for errors
|
111 |
+
pylint_output = subprocess.check_output(['pylint', 'temp_code.py'], text=True)
|
112 |
+
os.remove('temp_code.py')
|
113 |
+
|
114 |
+
# Return the pylint output
|
115 |
+
return pylint_output
|
116 |
+
except Exception as e:
|
117 |
+
return f"Error spotting and correcting code: {str(e)}"
|
118 |
+
|
119 |
+
class GitHubRepoSpongeTool(BaseTool):
|
120 |
+
"""Fetches and analyzes GitHub repositories."""
|
121 |
+
|
122 |
+
def __init__(self):
|
123 |
+
self.name = "GitHubRepoSpongeTool"
|
124 |
+
|
125 |
+
def use(self, input_str: str) -> str:
|
126 |
+
try:
|
127 |
+
# Assume input_str is the GitHub repository in the form 'owner/repo'
|
128 |
+
owner, repo = input_str.split('/')
|
129 |
+
url = f'https://api.github.com/repos/{owner}/{repo}'
|
130 |
+
response = requests.get(url)
|
131 |
+
if response.status_code == 200:
|
132 |
+
repo_data = response.json()
|
133 |
+
return f"Repository data fetched: {repo_data}"
|
134 |
+
else:
|
135 |
+
return f"Failed to fetch repository: {response.status_code}"
|
136 |
+
except Exception as e:
|
137 |
+
return f"Error fetching GitHub repository: {str(e)}"
|
138 |
|
139 |
+
|
140 |
class MyDuckDuckGoSearchTool(BaseTool, DuckDuckGoSearchTool):
|
141 |
"""Performs a DuckDuckGo search and returns the results."""
|
142 |
|