Spaces:
Sleeping
Sleeping
File size: 9,089 Bytes
d10a61a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel
import os
from dotenv import load_dotenv
class FileReaderTool(Tool):
name = "file_reader_tool"
description = """
This tool will be used by the LLM Agent to read files to help analyze files for its task.
"""
inputs = {
"file_location": {
"type": "string",
"description": "The location of the file that will be read/analyzed"
}
}
output_type = "string"
def forward(self,file_location ) -> str:
with open(file_location, "r") as file:
return file.read()
class FileWriteTool(Tool):
name = "file_write_tool"
description = """
This tool will be used by the LLM Agent to overwrite files if needed for task.
"""
inputs = {
"file_location": {
"type": "string",
"description": "The location of the file that will be read/analyzed"
},
"new_code": {
"type": "string",
"description": "This is the code that will overwrite the contents of a file. If file does not exist, it is the new content."
}
}
output_type = "string"
def forward(self,file_location, new_code) -> str:
with open(file_location, "w") as file:
return file.write(new_code)
class FileModifyTool(Tool):
name = "file_modify_tool"
description = """
This tool will be used by the LLM Agent to modify files if needed for task.
"""
inputs = {
"file_location": {
"type": "string",
"description": "The location of the file that will be read/analyzed"
},
"prompt": {
"type": "string",
"description": "This is the prompt that the LLM will use to decide how to modify the code."
}
}
output_type = "string"
def forward(self,file_location, prompt) -> str:
load_dotenv()
HF_TOKEN = os.getenv("HF_TOKEN")
file_reader_tool = FileReaderTool()
file_write_tool = FileWriteTool()
model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct", token=HF_TOKEN)
coderAgent = CodeAgent(tools=[], model=model)
file_content = file_reader_tool.forward(file_location)
if not file_content:
return "Error: File could not be read."
modified_code = coderAgent.run(f'Based on the prompt "{prompt}" \n Modify the following code:\n{file_content}\n')
if "ERROR" in modified_code:
return "Modification failed, please refine your request."
write_result = file_write_tool.forward(file_location, modified_code)
return
class FileReplaceTool(Tool):
name = "file_replace_tool"
description ="""
This tool will be used to replace the file in a given location with the provided new file location. This is not used to update files.
"""
inputs = {
"target_file_location": {
"type": "string",
"description": "the location of the file that will be replaced"
},
"new_file_location": {
"type": "string",
"description": "the location of the new file to replace target file location"
}
}
output_type = "string"
def forward(self, target_file_location, new_file_location) -> str:
import os
import shutil
if os.path.exists(new_file_location):
# Define the destination path for the saved image
# # Create the './generatedImages' directory if it doesn't exist
# # Copy the image from the temporary location to the desired directory
shutil.copy(new_file_location, target_file_location)
return print(f"Image saved to {target_file_location}")
else:
return print("Failed to generate an image or the file does not exist.")
class GetImageDimensionsTool(Tool):
name = "get_image_dimensions_tool"
description= """
This tool is used to get the width and height of a webp file.
"""
inputs = {
"file_location": {
"type": "string",
"description": "The location in which the webp file can be located"
}
}
output_type = "object"
def forward(self, file_location) -> dict:
from PIL import Image
with Image.open(file_location) as img:
width, height = img.size
return {"width": width, "height": height}
class ProcessFlowIdentifierTool(Tool):
name = "process_flow_identifier_tool"
description = """
This tool will be used to give a set of instructions depending on the purpose of the prompt. This is to aid the LLM in its decision making process.
"""
inputs = {
"prompt_objective": {
"type": "string",
"description": "This is the objective of the user's original prompt to help identify the steps needed for the llm to take."
}
}
output_type = "string"
def forward(self, prompt_objective) -> str:
match prompt_objective:
case "asset_change":
instructions = """
1) use the find files tool to get a list of files containing tsx, and find the sandpack-examples.tsx file and copy its path.
2) Use the file_modify_tool to analyze and update the file.
3) End process after sucessfully modifying the file
"""
return instructions
case "script_change":
instructions = """
1) use the find files tool to get a list of files containing tsx, and find the sandpack-examples.tsx file and copy its path.
2) Use the file_modify_tool to analyze and update the file.
3) End process after sucessfully modifying the file
"""
return instructions
case _:
instructions = """
inform user that the instructions where unclear
"""
return instructions
class GitPushTool(Tool):
name = "git_push_tool"
description = """
This tool will be triggered to create a new branch and push new changes to the repository.
"""
inputs = {
"branch_name": {
"type": "string",
"description": "the target branch that will be pushed, new or existing."
}
}
output_type = "string"
def forward(self, branch_name) -> str:
import os
import subprocess
try:
gitUsername = os.getenv("GIT_USERNAME")
gitEmail = os.getenv("GIT_EMAIL")
# new_branch = "add-generated-image-2"
# Step 1: Ensure we are in a Git repository
subprocess.run(["git", "status"], check=True)
# Step 2: Create and switch to a new branch
subprocess.run(["git", "checkout", "-b", branch_name], check=True)
print(f"Checked out to new branch: {branch_name}")
# Step 3: Add the changes
subprocess.run(["git", "add", "*"], check=True)
print("Changes added to staging.")
# Step 4: Add credentials
subprocess.run(["git", "config", "--global", "user.email", gitEmail], check=True)
print("Updated git email.")
subprocess.run(["git", "config", "--global", "user.name", gitUsername], check=True)
print("Updated git user name.")
# Step 5: Commit the changes
commit_message = "Add generated image to repository"
subprocess.run(["git", "commit", "-m", commit_message], check=True)
print("Changes committed.")
#Step 6: Push the branch to the remote repository
subprocess.run(["git", "push", "--set-upstream", "origin", branch_name], check=True)
return print(f"Branch '{branch_name}' pushed to remote repository.")
except subprocess.CalledProcessError as e:
return print(f"An error occurred while performing Git operations: {e}")
class FindFilesTool(Tool):
name = "find_files"
description = "Find files with a given extension in a directory and its subdirectories"
inputs = {"extension":{"type":"string","description":"the place from which you start your ride"}}
output_type = "string"
def forward(self, extension: str) -> str:
"""
Recursively search for files with a given extension in a directory and its subdirectories.
Args:
extension: The file extension to look for (e.g., '.txt')
"""
import os
root_dir = "./"
found_files = []
# Walk through the directory tree
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(extension):
filepath = os.path.join(dirpath, filename)
absolute_path = os.path.abspath(filepath)
found_files.append(absolute_path)
return found_files |