|
|
from smolagents import PythonInterpreterTool, tool |
|
|
import requests |
|
|
import json |
|
|
|
|
|
@tool |
|
|
def TextInverterTool(input_string: str) -> str: |
|
|
""" |
|
|
Inverts the order of characters in a given text string. |
|
|
|
|
|
Args: |
|
|
input_string: Text string to be inverted |
|
|
|
|
|
Returns: |
|
|
str: Character-reversed version of the input text |
|
|
""" |
|
|
return input_string[::-1] |
|
|
|
|
|
|
|
|
@tool |
|
|
def PythonScriptExecutor(script_location: str) -> str: |
|
|
""" |
|
|
Loads and executes Python code from a specified file path using interpreter tools. |
|
|
|
|
|
Args: |
|
|
script_location: Complete file system path to the Python script (.py extension) |
|
|
|
|
|
Returns: |
|
|
str: Execution results or error description if the operation fails |
|
|
""" |
|
|
try: |
|
|
|
|
|
with open(script_location, "r", encoding='utf-8') as file_handle: |
|
|
python_code = file_handle.read() |
|
|
|
|
|
|
|
|
code_interpreter = PythonInterpreterTool() |
|
|
execution_result = code_interpreter.run({"code": python_code}) |
|
|
|
|
|
return execution_result.get("output", "Execution completed without output.") |
|
|
except FileNotFoundError: |
|
|
return f"File not found: {script_location}" |
|
|
except Exception as error: |
|
|
return f"Script execution error: {str(error)}" |
|
|
|
|
|
|
|
|
@tool |
|
|
def WebFileDownloader(source_url: str, destination_path: str) -> str: |
|
|
""" |
|
|
Retrieves a file from a web URL and stores it locally at the specified path. |
|
|
|
|
|
Args: |
|
|
source_url: Web address of the file to download |
|
|
destination_path: Local filesystem path for saving the downloaded content |
|
|
|
|
|
Returns: |
|
|
str: Status message describing the download operation result |
|
|
""" |
|
|
try: |
|
|
|
|
|
headers = { |
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' |
|
|
} |
|
|
|
|
|
web_response = requests.get(source_url, headers=headers, timeout=45) |
|
|
web_response.raise_for_status() |
|
|
|
|
|
|
|
|
with open(destination_path, "wb") as output_file: |
|
|
output_file.write(web_response.content) |
|
|
|
|
|
file_size = len(web_response.content) |
|
|
return f"Successfully downloaded {file_size} bytes to {destination_path}" |
|
|
|
|
|
except requests.exceptions.RequestException as req_error: |
|
|
return f"Download request failed: {str(req_error)}" |
|
|
except IOError as io_error: |
|
|
return f"File save operation failed: {str(io_error)}" |
|
|
except Exception as general_error: |
|
|
return f"Unexpected download error: {str(general_error)}" |