Spaces:
Sleeping
Sleeping
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
import datetime | |
import pytz | |
import yaml | |
from tools.final_answer import FinalAnswerTool | |
from Gradio_UI import GradioUI | |
import random | |
# Enhanced custom tool that performs a creative transformation (magic) | |
def my_custom_tool(arg1: str, arg2: int) -> str: | |
""" | |
A creatively enhanced tool that transforms a given message based on a magic factor. | |
Args: | |
arg1: The input message to transform. | |
arg2: The magic factor (an integer) that determines the level of transformation. | |
Returns: | |
A magically transformed message. | |
""" | |
# Reverse the input string as a simple transformation | |
reversed_message = arg1[::-1] | |
# Define a few inspirational magic phrases | |
magic_phrases = [ | |
"abracadabra", | |
"hocus pocus", | |
"alakazam", | |
"voila", | |
"presto chango" | |
] | |
# Select a phrase based on the magic factor (cycling through the phrases) | |
phrase = magic_phrases[arg2 % len(magic_phrases)] | |
# Optionally, add a random flourish | |
flourish = random.choice(["✨", "🌟", "🔮", "🎩"]) | |
return f"{flourish} {reversed_message} {phrase}! Your magic factor is {arg2}." | |
# Tool to fetch the current local time in a specified timezone | |
def get_current_time_in_timezone(timezone: str) -> str: | |
""" | |
A tool that fetches the current local time in a specified timezone. | |
Args: | |
timezone: A string representing a valid timezone (e.g., 'America/New_York'). | |
Returns: | |
A string with the current local time or an error message if unsuccessful. | |
""" | |
try: | |
tz = pytz.timezone(timezone) | |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
return f"The current local time in {timezone} is: {local_time}" | |
except Exception as e: | |
return f"Error fetching time for timezone '{timezone}': {str(e)}" | |
# Tool that performs a magic trick by predicting a random number | |
def perform_magic() -> str: | |
""" | |
A tool that performs a magic trick by predicting a random number. | |
Returns: | |
A string with the magical prediction. | |
""" | |
# Simulate inviting the user to think of a number | |
print("Please think of a number between 1 and 10...") | |
predicted_number = random.randint(1, 10) | |
magic_message = f"✨ Abracadabra! Your magical prediction is: {predicted_number} ✨" | |
print(magic_message) | |
return magic_message | |
# Initialize the final_answer tool and HfApiModel | |
final_answer = FinalAnswerTool() | |
model = HfApiModel( | |
max_tokens=2096, | |
temperature=0.5, | |
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud', | |
custom_role_conversions=None, | |
) | |
# Import an image generation tool from the Hub (example usage; not added to agent's tools) | |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
# Load prompt templates from a YAML file | |
with open("prompts.yaml", 'r') as stream: | |
prompt_templates = yaml.safe_load(stream) | |
# Instantiate the CodeAgent with the required tools. The final_answer tool must always be included. | |
agent = CodeAgent( | |
model=model, | |
tools=[final_answer, my_custom_tool, get_current_time_in_timezone, perform_magic], | |
max_steps=6, | |
verbosity_level=1, | |
grammar=None, | |
planning_interval=None, | |
name=None, | |
description=None, | |
prompt_templates=prompt_templates | |
) | |
# Launch the Gradio UI to interact with the agent. | |
GradioUI(agent).launch() | |