File size: 6,151 Bytes
adb3e6b f69719d adb3e6b 536a8b2 adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b f69719d adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae adb3e6b 4469dae 75858ed adb3e6b 75858ed adb3e6b 75858ed adb3e6b 75858ed adb3e6b 75858ed 352856a adb3e6b 352856a 4469dae adb3e6b 352856a 4469dae adb3e6b f69719d adb3e6b 352856a adb3e6b f69719d |
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 |
# Importing necessary modules and tools for our application.
# These libraries help us work with code, dates, files, images, and the user interface.
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime # Helps with dates and times.
import requests # Lets us make web requests.
import yaml # Reads YAML files, which are used for configuration.
from tools.final_answer import FinalAnswerTool # Custom tool to finalize the answer.
from Gradio_UI import GradioUI # Module for creating a simple web interface.
# -----------------------------
# Tool: comment_code
# -----------------------------
# This function takes a piece of code and adds comments to explain what each part does.
@tool
def comment_code(code: str) -> str:
"""
A tool that takes a block of code and returns the same code with added comments.
The comments explain each part of the code in simple terms.
Args:
code: The input code as a string.
"""
try:
# Start with an empty string to collect our commented code.
commented_code = ""
# Split the code into lines so we can process each line separately.
for line in code.split('\n'):
stripped = line.strip() # Remove extra spaces from the beginning and end.
if stripped:
# Check if the line defines a function.
if "def " in stripped:
commented_code += "# This defines a function\n"
# Check if the line imports a module.
elif "import " in stripped:
commented_code += "# Importing necessary modules\n"
# Check if the line is a loop (for or while).
elif "for " in stripped or "while " in stripped:
commented_code += "# Looping through values\n"
# Check if the line contains a conditional statement.
elif "if " in stripped:
commented_code += "# Checking a condition\n"
# Check if the line returns a value.
elif "return " in stripped:
commented_code += "# Returning a value from the function\n"
# Add the original code line after the comment.
commented_code += line + "\n"
return commented_code
except Exception as e:
# If something goes wrong, return an error message.
return f"Error processing code: {str(e)}"
# -----------------------------
# Tool: extract_code_from_image
# -----------------------------
# This tool extracts text (code) from an image file, like a screenshot.
@tool
def extract_code_from_image(image_path: str) -> str:
"""
Extracts text from an image file (a screenshot) that contains code.
Args:
image_path: The path to the image file.
"""
try:
from PIL import Image # Module for opening and working with images.
import pytesseract # OCR tool to extract text from images.
image = Image.open(image_path) # Open the image file.
code_text = pytesseract.image_to_string(image) # Extract text from the image.
return code_text
except Exception as e:
# Return an error message if extraction fails.
return f"Error extracting code from image: {str(e)}"
# -----------------------------
# Tool: extract_code_from_file
# -----------------------------
# This tool reads code from a file and returns it as text.
@tool
def extract_code_from_file(file_path: str) -> str:
"""
Reads code text from a file.
Args:
file_path: The path to the file containing code.
"""
try:
with open(file_path, 'r') as f: # Open the file in read mode.
code_text = f.read() # Read the content of the file.
return code_text
except Exception as e:
# Return an error message if the file cannot be read.
return f"Error reading code from file: {str(e)}"
# Create an instance of the FinalAnswerTool.
final_answer = FinalAnswerTool()
# -----------------------------
# Set up the language model.
# -----------------------------
# This model will help the agent understand and process code-related tasks.
model = HfApiModel(
max_tokens=2096, # Maximum tokens the model can output.
temperature=0.5, # Controls randomness in the model's output.
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Specific model to use.
custom_role_conversions=None, # No custom role conversion needed here.
)
# -----------------------------
# Optionally, load an image generation tool.
# -----------------------------
# This tool is loaded from an external source, though it isn't used directly here.
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# -----------------------------
# Load prompt templates from a configuration file.
# -----------------------------
# This file likely contains text instructions that help guide the agent.
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# -----------------------------
# Set up the CodeAgent.
# -----------------------------
# The agent uses the model and the tools to perform tasks.
agent = CodeAgent(
model=model,
tools=[final_answer, comment_code, extract_code_from_image, extract_code_from_file],
max_steps=6, # Limit on how many steps the agent can take.
verbosity_level=1, # Level of detail in the logs/output.
grammar=None, # No special grammar rules are applied.
planning_interval=None, # No specific planning interval set.
name="Code Commenter", # Name of the agent.
description="An agent that adds explanatory comments to code in simple terms.",
prompt_templates=prompt_templates # Prompts to guide the agent's responses.
)
# -----------------------------
# Launch the Gradio user interface.
# -----------------------------
# Gradio creates a simple web interface for users to interact with the agent.
GradioUI(agent).launch()
|